Class: Decidim::Messaging::ConversationsController

Inherits:
ApplicationController show all
Includes:
FormFactory
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 NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject



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

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



40
41
42
43
44
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 40

def index
  authorize! :index, Conversation

  @conversations = UserConversations.for(current_user)
end

#newObject



15
16
17
18
19
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 15

def new
  authorize! :create, Conversation

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

#showObject



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

def show
  authorize! :show, conversation

  @conversation.mark_as_read(current_user)

  @form = MessageForm.new
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 54

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