Class: Decidim::Messaging::ConversationsController

Inherits:
ApplicationController show all
Includes:
FormFactory, HasSpecificBreadcrumb, ConversationHelper
Defined in:
decidim-core/app/controllers/decidim/messaging/conversations_controller.rb

Overview

The controller to handle the user’s conversations.

Instance Method Summary collapse

Methods included from ConversationHelper

#conversation_between, #conversation_between_multiple, #conversation_label_for, #conversation_name_for, #current_or_new_conversation_path_with, #current_or_new_conversation_path_with_multiple, #current_or_new_profile_conversation_path, #current_or_new_user_conversation_path, #link_to_current_or_new_conversation_with, #text_link_to_current_or_new_conversation_with, #username_list

Methods included from UserBlockedChecker

#check_user_block_status, #check_user_not_blocked

Methods included from NeedsSnippets

#snippets

Methods included from Headers::HttpCachingDisabler

#disable_http_caching

Methods included from HasStoredPath

#skip_store_location?, #store_current_location

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#check_multipleObject



97
98
99
100
101
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 97

def check_multiple
  @form = form(ConversationForm).from_params(params, sender: current_user)
  redirect_link = current_or_new_conversation_path_with_multiple(@form.recipient, nickname: params[:nickname])
  redirect_to redirect_link
end

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 40

def create
  @form = form(ConversationForm).from_params(params, sender: current_user)
  enforce_permission_to :create, :conversation, conversation: new_conversation(@form.recipient)

  StartConversation.call(@form) do
    on(:ok) do |conversation|
      render action: :create, locals: {
        conversation:,
        form: MessageForm.new
      }
    end

    on(:invalid) do |messages|
      render action: :error, locals: {
        error: I18n.t("messaging.conversations.create.error", scope: "decidim"),
        messages:
      }, status: :unprocessable_entity
    end
  end
end

#indexObject



61
62
63
64
65
66
67
68
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 61

def index
  enforce_permission_to :list, :conversation

  @conversations = UserConversations.for(current_user)
  @form = MessageForm.new

  validation_messages << t("decidim.messaging.conversations.index.no_conversations") if @conversations.blank?
end

#newObject

Shows the form to initiate a conversation with an user (the recipient) recipient is passed via GET parameter:

- if the recipient does not exists, goes back to the users profile page
- if the user already has a conversation with the user, redirects to the initiated conversation


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 23

def new
  @form = form(ConversationForm).from_params(params, sender: current_user)

  if @form.recipient.is_a? Enumerable
    participants = @form.recipient.to_a.prepend(current_user)
    conversation = conversation_between_multiple(participants)
  else
    conversation = conversation_between(current_user, @form.recipient)
  end

  return redirect_back(fallback_location: profile_path(current_user.nickname)) if @form.recipient.empty?

  return redirect_to conversation_path(conversation) if conversation

  enforce_permission_to :create, :conversation, conversation: new_conversation(@form.recipient)
end

#showObject



70
71
72
73
74
75
76
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 70

def show
  enforce_permission_to(:read, :conversation, conversation:)

  @conversation.mark_as_read(current_user)

  @form = MessageForm.new
end

#updateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'decidim-core/app/controllers/decidim/messaging/conversations_controller.rb', line 78

def update
  enforce_permission_to(:update, :conversation, conversation:)

  @form = form(MessageForm).from_params(params, sender: current_user)

  ReplyToConversation.call(conversation, @form) do
    on(:ok) do |message|
      render action: :update, locals: { message: }
    end

    on(:invalid) do |messages|
      render action: :error, locals: {
        error: I18n.t("messaging.conversations.update.error", scope: "decidim"),
        messages:
      }, status: :unprocessable_entity
    end
  end
end