Class: Decidim::Messaging::Conversation
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Messaging::Conversation
- Includes:
- DataPortability
- 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 her 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 her 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.
Class Method Details
.export_serializer ⇒ Object
164 165 166 |
# File 'app/models/decidim/messaging/conversation.rb', line 164 def self.export_serializer Decidim::DataPortabilitySerializers::DataPortabilityConversationSerializer 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.
73 74 75 76 77 78 79 |
# File 'app/models/decidim/messaging/conversation.rb', line 73 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.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/decidim/messaging/conversation.rb', line 48 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
160 161 162 |
# File 'app/models/decidim/messaging/conversation.rb', line 160 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 her to join the conversation
126 127 128 129 130 |
# File 'app/models/decidim/messaging/conversation.rb', line 126 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
102 103 104 105 106 107 108 |
# File 'app/models/decidim/messaging/conversation.rb', line 102 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.
87 88 89 90 91 |
# File 'app/models/decidim/messaging/conversation.rb', line 87 def (sender:, body:, user: nil) (sender: sender, body: body, user: user) save! end |
#interlocutors(user) ⇒ Array<Decidim::User>
Given a user, returns her interlocutors in this conversation
117 118 119 |
# File 'app/models/decidim/messaging/conversation.rb', line 117 def interlocutors(user) participants.reject { |participant| participant.id == user.id } end |
#last_message ⇒ Decidim::Messaging::Message
The most recent message in this conversation
147 148 149 |
# File 'app/models/decidim/messaging/conversation.rb', line 147 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
138 139 140 |
# File 'app/models/decidim/messaging/conversation.rb', line 138 def participating?(user) participants.include?(user) end |
#unread_count(user) ⇒ Integer
The number of messages in this conversation a user has not yet read
156 157 158 |
# File 'app/models/decidim/messaging/conversation.rb', line 156 def unread_count(user) receipts.unread_by(user).count end |