Class: Decidim::Messaging::Message

Inherits:
ApplicationRecord show all
Includes:
FriendlyDates
Defined in:
app/models/decidim/messaging/message.rb

Overview

Holds a single message in a conversation. A message has a body, and sender and a set of receipts, which correspond to each user that will receive the message, namely, the interlocutors of the sender in the conversation.

Instance Method Summary collapse

Methods included from FriendlyDates

#friendly_created_at

Instance Method Details

Public: Returns the comment body with links



62
63
64
# File 'app/models/decidim/messaging/message.rb', line 62

def body_with_links
  Decidim::ContentRenderers::LinkRenderer.new(body).render
end

#envelope_for(recipients:, from: nil) ⇒ Object

Associates receipts for this message for each of the given users, including also a receipt for the remitent (sender) of the message. Receipts are unread by default, except for the sender’s receipt.

If the sender is a UserGroup then receipts will be created for its managers a “from” user can be specified to avoid create a receipt for the real user sending the message

Parameters:

  • recipients (Array<Decidim::UserBaseEntity>)

    Users or groups receiving the message

  • from (Array<Decidim::User>) (defaults to: nil)

    the user sending the message in case sender is a group



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/decidim/messaging/message.rb', line 47

def envelope_for(recipients:, from: nil)
  @from = sender.is_a?(User) ? sender : from
  @already_notified = [@from]

  receipts.build(recipient: @from, read_at: Time.current) if @from.is_a?(User)

  all_recipients(recipients).each do |recipient|
    next if @already_notified.include?(recipient)

    receipts.build(recipient: recipient)
    @already_notified.push(recipient)
  end
end