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
-
#conversation_between(*participants) ⇒ Decidim::Messaging::Conversation
Finds the conversation between the given participants.
-
#conversation_between_multiple(participants) ⇒ Decidim::Messaging::Conversation
Finds the conversation between the given participants.
- #conversation_label_for(participants) ⇒ Object
- #conversation_name_for(users) ⇒ Object
-
#current_or_new_conversation_path_with(user) ⇒ String
Finds the right path to the conversation the current user and another user (the interlocutor).
-
#current_or_new_conversation_path_with_multiple(users) ⇒ Object
Links to the conversation between the current user and another users group.
-
#link_to_current_or_new_conversation_with(user, title = t("decidim.contact")) ⇒ Object
Links to the conversation between the current user and another user.
-
#text_link_to_current_or_new_conversation_with(user, body = t("decidim.profiles.show.send_private_message")) ⇒ Object
Same as #link_to_current_or_new_conversation_with, but with a text body instead of an icon.
-
#username_list(users, shorten: false) ⇒ Object
Generates a visualization of users for listing conversations threads.
Instance Method Details
#conversation_between(*participants) ⇒ Decidim::Messaging::Conversation
Finds the conversation between the given participants
111 112 113 114 115 116 117 |
# File '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
143 144 145 146 147 148 149 |
# File '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
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 15 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
6 7 8 9 10 11 12 13 |
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 6 def conversation_name_for(users) return content_tag(:span, t("decidim.profile.deleted"), class: "label label--small label--basic") if users.first.deleted? content_tag = content_tag(:strong, users.first.name) content_tag << tag.br content_tag << content_tag(:span, "@#{users.first.nickname}", class: "muted") content_tag 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
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 91 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
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 122 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 |
#link_to_current_or_new_conversation_with(user, title = t("decidim.contact")) ⇒ Object
Links to the conversation between the current user and another user
48 49 50 51 52 53 54 55 56 57 58 59 |
# File '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: title do icon "envelope-closed", aria_label: title, class: "icon--small" end else content_tag :span, title: t("decidim.user_contact_disabled"), data: { tooltip: true } do icon "envelope-closed", aria_label: title, class: "icon--small muted" end end end |
#text_link_to_current_or_new_conversation_with(user, body = t("decidim.profiles.show.send_private_message")) ⇒ Object
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
66 67 68 69 |
# File '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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/helpers/decidim/messaging/conversation_helper.rb', line 30 def username_list(users, shorten: false) = [] first_users = shorten ? users.first(3) : users deleted_user_tag = content_tag(:span, t("decidim.profile.deleted"), class: "label label--small label--basic") first_users.each do |u| .push(u.deleted? ? deleted_user_tag : content_tag(:strong, u.name)) end return .join(", ") unless shorten return .join(", ") unless users.count > 3 .push(content_tag(:strong, " + #{users.count - 3}")) .join(", ") end |