Sha256: 0523bbd212e206993e20e6f1ce8a888c405b382580451e2402cb2b48c0698119

Contents?: true

Size: 1.14 KB

Versions: 13

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Decidim
  module Messaging
    #
    # Represents the reception of a message by a user. This model is supposed to
    # hold any information about a message that is specific to each user, for
    # example, the read/unread status, the deleted/undeleted status, and so on.
    #
    class Receipt < ApplicationRecord
      self.table_name = "decidim_messaging_receipts"

      belongs_to :recipient, foreign_key: "decidim_recipient_id", class_name: "Decidim::User"
      belongs_to :message, foreign_key: "decidim_message_id", class_name: "Decidim::Messaging::Message"

      scope :recipient, ->(recipient) { where(recipient:) }
      scope :unread_by, ->(user) { recipient(user).unread }
      scope :unread, -> { where(read_at: nil) }

      # rubocop:disable Rails/SkipsModelValidations
      def self.mark_as_read(user)
        recipient(user).update_all(read_at: Time.current)
      end
      # rubocop:enable Rails/SkipsModelValidations

      # The number of messages unread by a user
      #
      # @return [Integer]
      #
      def self.unread_count(user)
        unread_by(user).count
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.4 app/models/decidim/messaging/receipt.rb
decidim-core-0.29.0 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.3 app/models/decidim/messaging/receipt.rb
decidim-core-0.29.0.rc4 app/models/decidim/messaging/receipt.rb
decidim-core-0.29.0.rc3 app/models/decidim/messaging/receipt.rb
decidim-core-0.29.0.rc2 app/models/decidim/messaging/receipt.rb
decidim-core-0.29.0.rc1 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.2 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.1 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.0 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.0.rc5 app/models/decidim/messaging/receipt.rb
decidim-core-0.28.0.rc4 app/models/decidim/messaging/receipt.rb