Class: Decidim::UserConversationsController

Inherits:
ApplicationController show all
Includes:
FormFactory, HasProfileBreadcrumb, Messaging::ConversationHelper, Paginable, UserGroups
Defined in:
app/controllers/decidim/user_conversations_controller.rb

Overview

The controller to show all the conversations for a user or group This controller places conversations in the public profile page. Only for groups at the moment but it will make conversations_controller obsolete in the future

Constant Summary

Constants included from Paginable

Paginable::OPTIONS

Instance Method Summary collapse

Methods included from Messaging::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 UserGroups

#enforce_user_groups_enabled

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 TranslatableAttributes

#default_locale?

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/decidim/user_conversations_controller.rb', line 72

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

  enforce_permission_to :create, :conversation, interlocutor: user, conversation: @conversation

  # do not allow to create multiple conversation to the same actors if already started
  if @conversation.id
    flash[:alert] = I18n.t("user_conversations.create.existing_error", scope: "decidim")
    return redirect_to profile_conversation_path(nickname: user.nickname, id: @conversation.id)
  end
  Messaging::StartConversation.call(@form) do
    on(:ok) do |_conversation|
      flash[:notice] = I18n.t("user_conversations.create.success", scope: "decidim")
      return redirect_to profile_conversations_path(nickname: user.nickname)
    end
    on(:invalid) do
      flash[:alert] = I18n.t("user_conversations.create.error", scope: "decidim")
      render action: :show
    end
  end
end

#indexObject



24
25
26
# File 'app/controllers/decidim/user_conversations_controller.rb', line 24

def index
  enforce_permission_to :list, :conversation, interlocutor: user
end

#newObject

Shows the form to initiate a conversation with one or more users/groups (the recipients) recipients are passed via GET parameters:

- if no recipient are valid, redirects back to the users profile page
- if the user already has a conversation with the user(s), redirects to the initiated conversation


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/decidim/user_conversations_controller.rb', line 57

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

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

  @conversation = new_conversation(@form.recipient)

  # redirect to existing conversation if already started
  return redirect_to profile_conversation_path(nickname: user.nickname, id: @conversation.id) if @conversation.id

  enforce_permission_to :create, :conversation, interlocutor: user, conversation: @conversation

  render :show
end

#showObject

shows a conversation thread and presents a form to reply in ajax



29
30
31
32
33
# File 'app/controllers/decidim/user_conversations_controller.rb', line 29

def show
  enforce_permission_to(:show, :conversation, interlocutor: user, conversation:)

  conversation.mark_as_read(current_user)
end

#updateObject

receive replies in ajax, messages are returned through the view update.js.erb and printed over the form instead of using flash messages



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

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

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

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

    on(:invalid) do
      render_unprocessable_entity I18n.t("user_conversations.update.error", scope: "decidim")
    end
  end
end