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



136
137
138
# File 'app/models/decidim/messaging/conversation.rb', line 136

def self.export_serializer
  Decidim::DataPortabilitySerializers::DataPortabilityConversationSerializer
end

.start(originator:, interlocutors:, body:) ⇒ Decidim::Messaging::Conversation

Initiates a conversation between a user and a set of interlocutors with an initial message.

Parameters:

  • originator (Decidim::User)

    The user 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

Returns:



67
68
69
70
71
72
73
# File 'app/models/decidim/messaging/conversation.rb', line 67

def self.start(originator:, interlocutors:, body:)
  conversation = new(participants: [originator] + interlocutors)

  conversation.add_message(sender: originator, body: body)

  conversation
end

.start!(originator:, interlocutors:, body:) ⇒ 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::User)

    The user 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

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/decidim/messaging/conversation.rb', line 44

def self.start!(originator:, interlocutors:, body:)
  conversation = start(
    originator: originator,
    interlocutors: interlocutors,
    body: body
  )

  conversation.save!

  conversation
end

.user_collection(user) ⇒ Object



132
133
134
# File 'app/models/decidim/messaging/conversation.rb', line 132

def self.user_collection(user)
  Decidim::Messaging::UserConversations.for(user)
end

Instance Method Details

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

Appends a message to this conversation

Parameters:

  • sender (Decidim::User)

    The sender of the message

  • body (String)

    The content of the message

Returns:



95
96
97
98
99
100
101
# File 'app/models/decidim/messaging/conversation.rb', line 95

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

  message.envelope_for(interlocutors(sender))

  message
end

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

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

Parameters:

  • sender (Decidim::User)

    The sender of the message

  • body (String)

    The content of the message

Returns:



81
82
83
84
85
# File 'app/models/decidim/messaging/conversation.rb', line 81

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

  save!
end

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

Given a user, returns her interlocutors in this conversation

Parameters:

Returns:



110
111
112
# File 'app/models/decidim/messaging/conversation.rb', line 110

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

#last_messageDecidim::Messaging::Message

The most recent message in this conversation



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

def last_message
  messages.last
end

#unread_count(user) ⇒ Integer

The number of messages in this conversation a user has not yet read

Returns:

  • (Integer)


128
129
130
# File 'app/models/decidim/messaging/conversation.rb', line 128

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