notification.rb 3.39 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
#  id              :integer          not null, primary key
7 8
#  activity_id     :integer          not null
#  activity_type   :string           not null
yhirano's avatar
yhirano committed
9 10
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
11 12
#  account_id      :integer          not null
#  from_account_id :integer          not null
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
  TYPE_CLASS_MAP = {
    mention:        'Mention',
    reblog:         'Status',
    follow:         'Follow',
    follow_request: 'FollowRequest',
    favourite:      'Favourite',
  }.freeze

abcang's avatar
abcang committed
27
  STATUS_INCLUDES = [:account, :application, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :application, :media_attachments, :tags, mentions: :account]].freeze
28

29 30 31 32 33 34 35 36 37
  belongs_to :account, optional: true
  belongs_to :from_account, class_name: 'Account', optional: true
  belongs_to :activity, polymorphic: true, optional: true

  belongs_to :mention,        foreign_type: 'Mention',       foreign_key: 'activity_id', optional: true
  belongs_to :status,         foreign_type: 'Status',        foreign_key: 'activity_id', optional: true
  belongs_to :follow,         foreign_type: 'Follow',        foreign_key: 'activity_id', optional: true
  belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id', optional: true
  belongs_to :favourite,      foreign_type: 'Favourite',     foreign_key: 'activity_id', optional: true
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
abcang's avatar
abcang committed
58 59 60 61 62
      status&.reblog
    when :favourite
      favourite&.status
    when :mention
      mention&.status
63 64
    end
  end
65

66 67 68 69
  def browserable?
    type != :follow_request
  end

70 71
  class << self
    def reload_stale_associations!(cached_items)
72
      account_ids = (cached_items.map(&:from_account_id) + cached_items.map { |item| item.target_status&.account_id }.compact).uniq
73 74 75 76

      return if account_ids.empty?

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

      cached_items.each do |item|
        item.from_account = accounts[item.from_account_id]
80
        item.target_status.account = accounts[item.target_status.account_id] if item.target_status
81 82
      end
    end
83 84 85 86

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

  after_initialize :set_from_account
  before_validation :set_from_account

  private

  def set_from_account
95 96
    return unless new_record?

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