notification.rb 2.99 KB
Newer Older
1
# frozen_string_literal: true
yhirano's avatar
yhirano committed
2 3 4 5
# == Schema Information
#
# Table name: notifications
#
Eugen Rochko's avatar
Eugen Rochko committed
6 7
#  id              :integer          not null, primary key
#  activity_id     :integer
yhirano's avatar
yhirano committed
8 9 10
#  activity_type   :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
Eugen Rochko's avatar
Eugen Rochko committed
11 12
#  account_id      :integer
#  from_account_id :integer
yhirano's avatar
yhirano committed
13
#
14 15 16

class Notification < ApplicationRecord
  include Paginable
17
  include Cacheable
18

19 20 21 22 23 24 25 26 27 28
  TYPE_CLASS_MAP = {
    mention:        'Mention',
    reblog:         'Status',
    follow:         'Follow',
    follow_request: 'FollowRequest',
    favourite:      'Favourite',
  }.freeze

  STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze

29
  belongs_to :account
30
  belongs_to :from_account, class_name: 'Account'
31 32
  belongs_to :activity, polymorphic: true

33 34 35 36 37
  belongs_to :mention,        foreign_type: 'Mention',       foreign_key: 'activity_id'
  belongs_to :status,         foreign_type: 'Status',        foreign_key: 'activity_id'
  belongs_to :follow,         foreign_type: 'Follow',        foreign_key: 'activity_id'
  belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id'
  belongs_to :favourite,      foreign_type: 'Favourite',     foreign_key: 'activity_id'
38

39
  validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
40
  validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
41

42 43
  scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) }

44 45 46 47 48
  scope :browserable, ->(exclude_types = []) {
    types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types + [:follow_request])
    where(activity_type: types)
  }

49
  cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
50 51

  def type
52
    @type ||= TYPE_CLASS_MAP.invert[activity_type].to_sym
53 54 55 56 57
  end

  def target_status
    case type
    when :reblog
58
      activity&.reblog
59
    when :favourite, :mention
60
      activity&.status
61 62
    end
  end
63

64 65 66 67
  def browserable?
    type != :follow_request
  end

68 69 70
  class << self
    def reload_stale_associations!(cached_items)
      account_ids = cached_items.map(&:from_account_id).uniq
71 72 73 74

      return if account_ids.empty?

      accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
75 76 77 78 79

      cached_items.each do |item|
        item.from_account = accounts[item.from_account_id]
      end
    end
80 81 82 83 84 85

    private

    def activity_types_from_types(types)
      types.map { |type| TYPE_CLASS_MAP[type.to_sym] }.compact
    end
86
  end
87 88 89 90 91 92 93

  after_initialize :set_from_account
  before_validation :set_from_account

  private

  def set_from_account
94 95
    return unless new_record?

96
    case activity_type
97
    when 'Status', 'Follow', 'Favourite', 'FollowRequest'
98
      self.from_account_id = activity&.account_id
99
    when 'Mention'
100
      self.from_account_id = activity&.status&.account_id
101 102
    end
  end
103
end