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:



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

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:



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

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



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

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



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

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



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

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



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

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



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

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 "mail-send-line", aria_label: title, class: "icon--small"
    end
  else
     :span, title: t("decidim.user_contact_disabled"), data: { tooltip: true } do
      icon "mail-send-line", 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 ?



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

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
# 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.map do |u|
    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