Class: Decidim::Messaging::ConversationsController

Inherits:
ApplicationController show all
Includes:
FormFactory, ConversationHelper
Defined in:
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, #current_or_new_conversation_path_with, #current_or_new_conversation_path_with_multiple, #link_to_current_or_new_conversation_with, #username_list

Methods included from NeedsSnippets

#snippets

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#check_multipleObject



89
90
91
92
93
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 89

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

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 37

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: conversation,
        form: MessageForm.new
      }
    end

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

#indexObject



55
56
57
58
59
60
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 55

def index
  enforce_permission_to :list, :conversation

  @conversations = UserConversations.for(current_user)
  @form = MessageForm.new
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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 20

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



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

def show
  enforce_permission_to :read, :conversation, conversation: conversation

  @conversation.mark_as_read(current_user)

  @form = MessageForm.new
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 70

def update
  enforce_permission_to :update, :conversation, 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: message }
    end

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