Sha256: d23a7ffecacd18777fd36aba568c97355180852a7d0eb513e42fc59defd2454e

Contents?: true

Size: 1.61 KB

Versions: 16

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Decidim
  module Messaging
    #
    # 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.
    #
    class Message < ApplicationRecord
      include Decidim::FriendlyDates

      belongs_to :sender,
                 foreign_key: :decidim_sender_id,
                 class_name: "Decidim::User"

      belongs_to :conversation,
                 foreign_key: :decidim_conversation_id,
                 class_name: "Decidim::Messaging::Conversation"

      has_many :receipts,
               dependent: :destroy,
               foreign_key: :decidim_message_id,
               inverse_of: :message

      validates :sender, :body, presence: true
      validates :body, length: { maximum: 1_000 }

      default_scope { order(created_at: :asc) }

      validate :sender_is_participant

      #
      # 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.
      #
      # @param recipients [Array<Decidim::User>]
      #
      def envelope_for(recipients)
        receipts.build(recipient: sender, read_at: Time.zone.now)

        recipients.each { |recipient| receipts.build(recipient: recipient) }
      end

      private

      def sender_is_participant
        errors.add(:sender, :invalid) unless conversation.participants.include?(sender)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-core-0.13.1 app/models/decidim/messaging/message.rb
decidim-core-0.12.2 app/models/decidim/messaging/message.rb
decidim-core-0.13.0 app/models/decidim/messaging/message.rb
decidim-core-0.12.1 app/models/decidim/messaging/message.rb
decidim-core-0.13.0.pre1 app/models/decidim/messaging/message.rb
decidim-core-0.12.0 app/models/decidim/messaging/message.rb
decidim-core-0.11.2 app/models/decidim/messaging/message.rb
decidim-core-0.12.0.pre app/models/decidim/messaging/message.rb
decidim-core-0.11.1 app/models/decidim/messaging/message.rb
decidim-core-0.11.0.pre1 app/models/decidim/messaging/message.rb
decidim-core-0.10.1 app/models/decidim/messaging/message.rb
decidim-core-0.10.0 app/models/decidim/messaging/message.rb
decidim-core-0.9.3 app/models/decidim/messaging/message.rb
decidim-core-0.9.2 app/models/decidim/messaging/message.rb
decidim-core-0.9.1 app/models/decidim/messaging/message.rb
decidim-core-0.9.0 app/models/decidim/messaging/message.rb