You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Notification < ApplicationRecord
  3. include Paginable
  4. include Cacheable
  5. belongs_to :account
  6. belongs_to :from_account, class_name: 'Account'
  7. belongs_to :activity, polymorphic: true
  8. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id'
  9. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id'
  10. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  11. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  12. validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
  13. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
  14. cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
  15. def activity
  16. send(activity_type.downcase)
  17. end
  18. def type
  19. case activity_type
  20. when 'Status'
  21. :reblog
  22. else
  23. activity_type.downcase.to_sym
  24. end
  25. end
  26. def target_status
  27. case type
  28. when :reblog
  29. activity.reblog
  30. when :favourite, :mention
  31. activity.status
  32. end
  33. end
  34. class << self
  35. def reload_stale_associations!(cached_items)
  36. account_ids = cached_items.map(&:from_account_id).uniq
  37. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  38. cached_items.each do |item|
  39. item.from_account = accounts[item.from_account_id]
  40. end
  41. end
  42. end
  43. end