Module: Decidim::Messaging::ConversationHelper

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:



111
112
113
114
115
116
117
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 111

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:



143
144
145
146
147
148
149
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 143

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

#conversation_label_for(participants) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 16

def conversation_label_for(participants)
  return t("title", scope: "decidim.messaging.conversations.show", usernames: username_list(participants)) unless participants.count == 1

  chat_with_user = if participants.first.deleted?
                     t("decidim.profile.deleted")
                   else
                     "#{participants.first.name} (@#{participants.first.nickname})"
                   end

  "#{t("chat_with", scope: "decidim.messaging.conversations.show")} #{chat_with_user}"
end

#conversation_name_for(users) ⇒ Object

deprecated



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

def conversation_name_for(users)
  return (:span, t("decidim.profile.deleted"), class: "label label--small label--basic") if users.first.deleted?

   = (:strong, users.first.name)
   << tag.br
   << (:span, "@#{users.first.nickname}", class: "muted")
  
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 is no current user, it returns to the login form path.

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

  • If there is 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



92
93
94
95
96
97
98
99
100
101
102
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 92

def current_or_new_conversation_path_with(user)
  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, opts = {}) ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 122

def current_or_new_conversation_path_with_multiple(users, opts = {})
  return decidim_routes.new_user_session_path unless user_signed_in?

  active_participant = opts[:nickname].present? ? Decidim::UserBaseEntity.find_by(nickname: opts[:nickname]) : current_user
  participants = users.to_a.prepend(active_participant)
  conversation = conversation_between_multiple(participants)

  if opts[:nickname].present?
    current_or_new_profile_conversation_path(opts[:nickname], users, conversation)
  else
    current_or_new_user_conversation_path(users, conversation)
  end
end

#current_or_new_profile_conversation_path(nickname, users, conversation = nil) ⇒ Object



151
152
153
154
155
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 151

def current_or_new_profile_conversation_path(nickname, users, conversation = nil)
  return decidim_routes.profile_conversation_path(conversation, nickname:) if conversation.present?

  decidim_routes.new_profile_conversation_path(nickname:, recipient_id: users.pluck(:id))
end

#current_or_new_user_conversation_path(users, conversation = nil) ⇒ Object



157
158
159
160
161
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 157

def current_or_new_user_conversation_path(users, conversation = nil)
  return decidim_routes.conversation_path(conversation) if conversation.present?

  decidim_routes.new_conversation_path(recipient_id: users.pluck(:id))
end

Links to the conversation between the current user and another user



48
49
50
51
52
53
54
55
56
57
58
59
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 48

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:) 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

Same as #link_to_current_or_new_conversation_with, but with a text body instead of an icon

Links to the conversation between the current user and another user

deprecated ?



67
68
69
70
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 67

def text_link_to_current_or_new_conversation_with(user, body = t("decidim.profiles.show.send_private_message"))
  conversation_path = current_or_new_conversation_path_with(user)
  link_to body, conversation_path, title: body if conversation_path
end

#username_list(users, shorten: false) ⇒ Object

Generates a visualization of users for listing conversations threads



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'decidim-core/app/helpers/decidim/messaging/conversation_helper.rb', line 31

def username_list(users, shorten: false)
   = []
  first_users = shorten ? users.first(3) : users
  first_users.each do |u|
    .push(u.deleted? ? t("decidim.profile.deleted") : u.name)
  end

  return .join(", ") unless shorten
  return .join(", ") unless users.count > 3

  .push(" + #{users.count - 3}")
  .join(", ")
end