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

Methods included from HttpCachingDisabler

#disable_http_caching

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
# File 'app/controllers/decidim/messaging/conversations_controller.rb', line 26

def create
  enforce_permission_to :create, :conversation

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

  StartConversation.call(@form) do
    on(:ok) do |conversation|
      redirect_to conversation_path(conversation)
    end

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

#indexObject



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

def index
  enforce_permission_to :list, :conversation

  @conversations = UserConversations.for(current_user)
end

#newObject



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

def new
  enforce_permission_to :create, :conversation
  @form = form(ConversationForm).from_params(params)

  redirect_back(fallback_location: profile_path(current_user.nickname)) && return unless @form.recipient

  conversation = conversation_between(current_user, @form.recipient)
  redirect_to conversation_path(conversation) if conversation
end

#showObject



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

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

  @conversation.mark_as_read(current_user)
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
  enforce_permission_to :update, :conversation, conversation: 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