Sha256: 48fac97cb4d07973d4c631a3746e047db264b9d113d42723a4754e67f21e5fe9

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true
module Thredded
  # Delivery records for Thredded::Post notifications.
  class UserPostNotification < ActiveRecord::Base
    belongs_to :user, class_name: Thredded.user_class, inverse_of: :thredded_post_notifications
    belongs_to :post, class_name: 'Thredded::Post', inverse_of: :user_notifications

    # @param post [Thredded::Post]
    # @return [Array<Integer>] The IDs of users who were already notified about the given post.
    def self.notified_user_ids(post)
      where(post_id: post.id).pluck(:user_id)
    end

    # Create a user-post notification record for a given post and a user.
    # @param post [Thredded::Post]
    # @param user [Thredded.user_class]
    # @return [Boolean] true if a new record was created, false otherwise (e.g. if a record had already existed).
    def self.create_from_post_and_user(post, user)
      create(
        post_id: post.id,
        user_id: user.id,
        notified_at: Time.zone.now
      )
    rescue ActiveRecord::RecordNotUnique
      false
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
thredded-0.12.1 app/models/thredded/user_post_notification.rb
thredded-0.12.0 app/models/thredded/user_post_notification.rb
thredded-0.11.1 app/models/thredded/user_post_notification.rb
thredded-0.11.0 app/models/thredded/user_post_notification.rb
thredded-0.10.1 app/models/thredded/user_post_notification.rb
thredded-0.10.0 app/models/thredded/user_post_notification.rb