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, #link_to_current_or_new_conversation_with

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 26

def create
  authorize! :create, Conversation

  @form = form(ConversationForm).from_params(params)

  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: 422
    end
  end
end

#indexObject



45
46
47
48
49
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 45

def index
  authorize! :index, Conversation

  @conversations = UserConversations.for(current_user)
end

#newObject



17
18
19
20
21
22
23
24
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 17

def new
  authorize! :create, Conversation
  @form = form(ConversationForm).from_params(params)

  conversation = conversation_between(current_user, @form.recipient)

  redirect_to conversation_path(conversation) if conversation
end

#showObject



51
52
53
54
55
56
57
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 51

def show
  authorize! :show, conversation

  @conversation.mark_as_read(current_user)

  @form = MessageForm.new
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 59

def update
  authorize! :update, conversation

  @form = form(MessageForm).from_params(params)

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

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