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:



82
83
84
85
86
87
88
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 82

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:



114
115
116
117
118
119
120
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 114

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 62

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 93

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

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



37
38
39
40
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 37

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



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