Class: Decidim::Messaging::Conversation
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Messaging::Conversation
- Includes:
- DownloadYourData
- Defined in:
- app/models/decidim/messaging/conversation.rb
Overview
Holds a conversation between a number of participants. Each conversation would be equivalent to an entry in your Telegram conversation list, be it a group or a one-to-one conversation.
Class Method Summary collapse
- .export_serializer ⇒ Object
-
.start(originator:, interlocutors:, body:, user: nil) ⇒ Decidim::Messaging::Conversation
Initiates a conversation between a user and a set of interlocutors with an initial message.
-
.start!(originator:, interlocutors:, body:, user: nil) ⇒ Decidim::Messaging::Conversation
Initiates a conversation between a user and a set of interlocutors with an initial message.
- .user_collection(user) ⇒ Object
Instance Method Summary collapse
-
#accept_user?(user) ⇒ Boolean
Given a user, returns if ALL the interlocutors allow the user to join the conversation.
-
#add_message(sender:, body:, user: nil) ⇒ Decidim::Messaging::Message
Appends a message to this conversation.
-
#add_message!(sender:, body:, user: nil) ⇒ Decidim::Messaging::Message
Appends a message to this conversation and saves everything to DB.
-
#interlocutors(user) ⇒ Array<Decidim::User>
Given a user, returns their interlocutors in this conversation.
-
#last_message ⇒ Decidim::Messaging::Message
The most recent message in this conversation.
-
#participating?(user) ⇒ Boolean
Given a user, returns if the user is participating in the conversation for groups being part of a conversation all their admin member are accepted.
-
#unread_count(user) ⇒ Integer
The number of messages in this conversation a user has not yet read.
-
#with_deleted_users?(user) ⇒ Boolean
Given a user, returns if ALL the interlocutors have their accounts deleted.
Class Method Details
.export_serializer ⇒ Object
175 176 177 |
# File 'app/models/decidim/messaging/conversation.rb', line 175 def self.export_serializer Decidim::DownloadYourDataSerializers::DownloadYourDataConversationSerializer end |
.start(originator:, interlocutors:, body:, user: nil) ⇒ Decidim::Messaging::Conversation
Initiates a conversation between a user and a set of interlocutors with an initial message.
75 76 77 78 79 80 81 |
# File 'app/models/decidim/messaging/conversation.rb', line 75 def self.start(originator:, interlocutors:, body:, user: nil) conversation = new(participants: [originator] + interlocutors) conversation.(sender: originator, body: body, user: user) conversation end |
.start!(originator:, interlocutors:, body:, user: nil) ⇒ Decidim::Messaging::Conversation
Initiates a conversation between a user and a set of interlocutors with an initial message. Works just like .start, but saves all the dependent objects into DB.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/decidim/messaging/conversation.rb', line 50 def self.start!(originator:, interlocutors:, body:, user: nil) conversation = start( originator: originator, interlocutors: interlocutors, body: body, user: user ) conversation.save! conversation end |
.user_collection(user) ⇒ Object
171 172 173 |
# File 'app/models/decidim/messaging/conversation.rb', line 171 def self.user_collection(user) Decidim::Messaging::UserConversations.for(user) end |
Instance Method Details
#accept_user?(user) ⇒ Boolean
Given a user, returns if ALL the interlocutors allow the user to join the conversation
128 129 130 131 132 |
# File 'app/models/decidim/messaging/conversation.rb', line 128 def accept_user?(user) # if user is a group, members are accepted blocked = interlocutors(user).detect { |participant| !participant.accepts_conversation?(user) } blocked.blank? end |
#add_message(sender:, body:, user: nil) ⇒ Decidim::Messaging::Message
Appends a message to this conversation
104 105 106 107 108 109 110 |
# File 'app/models/decidim/messaging/conversation.rb', line 104 def (sender:, body:, user: nil) = .build(sender: sender, body: body) .envelope_for(recipients: interlocutors(sender), from: user) end |
#add_message!(sender:, body:, user: nil) ⇒ Decidim::Messaging::Message
Appends a message to this conversation and saves everything to DB.
89 90 91 92 93 |
# File 'app/models/decidim/messaging/conversation.rb', line 89 def (sender:, body:, user: nil) (sender: sender, body: body, user: user) save! end |
#interlocutors(user) ⇒ Array<Decidim::User>
Given a user, returns their interlocutors in this conversation
119 120 121 |
# File 'app/models/decidim/messaging/conversation.rb', line 119 def interlocutors(user) participants.reject { |participant| participant.id == user.id } end |
#last_message ⇒ Decidim::Messaging::Message
The most recent message in this conversation
158 159 160 |
# File 'app/models/decidim/messaging/conversation.rb', line 158 def .last end |
#participating?(user) ⇒ Boolean
Given a user, returns if the user is participating in the conversation for groups being part of a conversation all their admin member are accepted
149 150 151 |
# File 'app/models/decidim/messaging/conversation.rb', line 149 def participating?(user) participants.include?(user) end |
#unread_count(user) ⇒ Integer
The number of messages in this conversation a user has not yet read
167 168 169 |
# File 'app/models/decidim/messaging/conversation.rb', line 167 def unread_count(user) receipts.unread_by(user).count end |
#with_deleted_users?(user) ⇒ Boolean
Given a user, returns if ALL the interlocutors have their accounts deleted
139 140 141 |
# File 'app/models/decidim/messaging/conversation.rb', line 139 def with_deleted_users?(user) interlocutors(user).all?(&:deleted?) end |