Class: Decidim::Messaging::Conversation

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

Instance Method Summary collapse

Class Method Details

.export_serializerObject



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.

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:



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.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:



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

Returns:

  • (Boolean)

    Boolean



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

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:



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

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:



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

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 her interlocutors in this conversation

Parameters:

Returns:



117
118
119
# File 'app/models/decidim/messaging/conversation.rb', line 117

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

#last_messageDecidim::Messaging::Message

The most recent message in this conversation



147
148
149
# File 'app/models/decidim/messaging/conversation.rb', line 147

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



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

Returns:

  • (Integer)


156
157
158
# File 'app/models/decidim/messaging/conversation.rb', line 156

def unread_count(user)
  receipts.unread_by(user).count
end