Module: Decidim::Messaging::ConversationHelper

Included in:
AuthorCell, ConversationsController, ProfileSidebarCell, UserConversationCell, UserConversationsCell, UserConversationsController
Defined in:
app/helpers/decidim/messaging/conversation_helper.rb

Instance Method Summary collapse

Instance Method Details

#conversation_between(*participants) ⇒ Decidim::Messaging::Conversation

Finds the conversation between the given participants

Parameters:

  • participants (Array<Decidim::User>)

    The participants to find a conversation between.

Returns:



72
73
74
75
76
77
78
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 72

def conversation_between(*participants)
  return if participants.to_set.length <= 1

  UserConversations.for(participants.first).find do |conversation|
    conversation.participants.to_set == participants.to_set
  end
end

#conversation_between_multiple(participants) ⇒ Decidim::Messaging::Conversation

Finds the conversation between the given participants

Parameters:

  • participants (Array<Decidim::User>)

    The participants to find a conversation between.

Returns:



104
105
106
107
108
109
110
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 104

def conversation_between_multiple(participants)
  return if participants.to_set.length <= 1

  UserConversations.for(participants.first).find do |conversation|
    conversation.participants.to_set == participants.to_set
  end
end

#current_or_new_conversation_path_with(user) ⇒ String

Finds the right path to the conversation the current user and another user (the interlocutor).

  • If there’s no current user, it returns to the login form path.

  • If there’s a prior existing conversation between the users it returns the path to the existing conversation.

  • If there’s no prior conversation between the users, it checks if the the interlocutor accepts the current user to new conversation. If affirmative, it returns the new conversation form path.

  • Otherwise returns nil, meaning that no conversation can be established with the interlocutor

Parameters:

  • user (Decidim::User)

    The user to link to a conversation with

Returns:

  • (String)

    The resulting route



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 52

def current_or_new_conversation_path_with(user)
  decidim_routes = Decidim::Core::Engine.routes.url_helpers
  return decidim_routes.new_user_session_path unless user_signed_in?

  conversation = conversation_between(current_user, user)

  if conversation
    decidim_routes.conversation_path(conversation)
  elsif user.accepts_conversation?(current_user)
    decidim_routes.new_conversation_path(recipient_id: user.id)
  end
end

#current_or_new_conversation_path_with_multiple(users) ⇒ Object

Links to the conversation between the current user and another users group



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 83

def current_or_new_conversation_path_with_multiple(users)
  decidim_routes = Decidim::Core::Engine.routes.url_helpers
  return decidim_routes.new_user_session_path unless user_signed_in?

  participants = users.to_a.prepend(current_user)
  conversation = conversation_between_multiple(participants)

  if conversation
    decidim_routes.conversation_path(conversation)
  else
    decidim_routes.new_conversation_path(recipient_id: users.pluck(:id))
  end
end

Links to the conversation between the current user and another user



19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 19

def link_to_current_or_new_conversation_with(user, title = t("decidim.contact"))
  conversation_path = current_or_new_conversation_path_with(user)
  if conversation_path
    link_to conversation_path, title: title do
      icon "envelope-closed", aria_label: title, class: "icon--small"
    end
  else
     :span, title: t("decidim.user_contact_disabled"), data: { tooltip: true } do
      icon "envelope-closed", aria_label: title, class: "icon--small muted"
    end
  end
end

#username_list(users, shorten = false) ⇒ Object

Generates a visualization of users for listing conversations threads



9
10
11
12
13
14
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 9

def username_list(users, shorten = false)
  return users.pluck(:name).join(", ") unless shorten
  return users.pluck(:name).join(", ") unless users.count > 3

  "#{users.first(3).pluck(:name).join(", ")} + #{users.count - 3}"
end