Class: Decidim::Messaging::Conversation

Inherits:
ApplicationRecord show all
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

Instance Method Summary collapse

Class Method Details

.export_serializerObject



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.

Parameters:

  • originator (Decidim::UserBaseEntity)

    The user or group starting the conversation

  • interlocutors (Array<Decidim::User>)

    The set of interlocutors in the conversation (not including the originator).

  • body (String)

    The content of the initial message

  • user (Decidim::User) (defaults to: nil)

    The user starting the conversation in case originator is a group

Returns:



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.add_message(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.

Parameters:

  • originator (Decidim::UserBaseEntity)

    The user or group starting the conversation

  • interlocutors (Array<Decidim::User>)

    The set of interlocutors in the conversation (not including the originator).

  • body (String)

    The content of the initial message

  • user (Decidim::User) (defaults to: nil)

    The user starting the conversation in case originator is a group

Returns:



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

Returns:

  • (Boolean)

    Boolean



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

Parameters:

  • sender (Decidim::UserBaseEntity)

    The sender of the message

  • body (String)

    The content of the message

  • user (Decidim::User) (defaults to: nil)

    The user sending the message in case sender is a group

Returns:



104
105
106
107
108
109
110
# File 'app/models/decidim/messaging/conversation.rb', line 104

def add_message(sender:, body:, user: nil)
  message = messages.build(sender: sender, body: body)

  message.envelope_for(recipients: interlocutors(sender), from: user)

  message
end

#add_message!(sender:, body:, user: nil) ⇒ Decidim::Messaging::Message

Appends a message to this conversation and saves everything to DB.

Parameters:

  • sender (Decidim::UserBaseEntity)

    The sender of the message

  • body (String)

    The content of the message

  • user (Decidim::User) (defaults to: nil)

    The user sending the message in case sender is a group

Returns:



89
90
91
92
93
# File 'app/models/decidim/messaging/conversation.rb', line 89

def add_message!(sender:, body:, user: nil)
  add_message(sender: sender, body: body, user: user)

  save!
end

#interlocutors(user) ⇒ Array<Decidim::User>

Given a user, returns their interlocutors in this conversation

Parameters:

Returns:



119
120
121
# File 'app/models/decidim/messaging/conversation.rb', line 119

def interlocutors(user)
  participants.reject { |participant| participant.id == user.id }
end

#last_messageDecidim::Messaging::Message

The most recent message in this conversation



158
159
160
# File 'app/models/decidim/messaging/conversation.rb', line 158

def last_message
  messages.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

Returns:

  • (Boolean)

    Boolean



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

Returns:

  • (Integer)


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

Returns:

  • (Boolean)

    Boolean



139
140
141
# File 'app/models/decidim/messaging/conversation.rb', line 139

def with_deleted_users?(user)
  interlocutors(user).all?(&:deleted?)
end