闭社主体 forked from https://github.com/tootsuite/mastodon
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.

103 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: notifications
  5. #
  6. # id :integer not null, primary key
  7. # account_id :integer
  8. # activity_id :integer
  9. # activity_type :string
  10. # created_at :datetime not null
  11. # updated_at :datetime not null
  12. # from_account_id :integer
  13. #
  14. class Notification < ApplicationRecord
  15. include Paginable
  16. include Cacheable
  17. belongs_to :account
  18. belongs_to :from_account, class_name: 'Account'
  19. belongs_to :activity, polymorphic: true
  20. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id'
  21. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id'
  22. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  23. belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id'
  24. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  25. validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
  26. TYPE_CLASS_MAP = {
  27. mention: 'Mention',
  28. reblog: 'Status',
  29. follow: 'Follow',
  30. follow_request: 'FollowRequest',
  31. favourite: 'Favourite',
  32. }.freeze
  33. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
  34. scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) }
  35. cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
  36. def activity(eager_loaded = true)
  37. eager_loaded ? send(activity_type.downcase) : super
  38. end
  39. def type
  40. @type ||= TYPE_CLASS_MAP.invert[activity_type].to_sym
  41. end
  42. def target_status
  43. case type
  44. when :reblog
  45. activity&.reblog
  46. when :favourite, :mention
  47. activity&.status
  48. end
  49. end
  50. def browserable?
  51. type != :follow_request
  52. end
  53. class << self
  54. def browserable(types = [])
  55. types.concat([:follow_request])
  56. where.not(activity_type: activity_types_from_types(types))
  57. end
  58. def reload_stale_associations!(cached_items)
  59. account_ids = cached_items.map(&:from_account_id).uniq
  60. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  61. cached_items.each do |item|
  62. item.from_account = accounts[item.from_account_id]
  63. end
  64. end
  65. private
  66. def activity_types_from_types(types)
  67. types.map { |type| TYPE_CLASS_MAP[type.to_sym] }.compact
  68. end
  69. end
  70. after_initialize :set_from_account
  71. before_validation :set_from_account
  72. private
  73. def set_from_account
  74. return unless new_record?
  75. case activity_type
  76. when 'Status', 'Follow', 'Favourite', 'FollowRequest'
  77. self.from_account_id = activity(false)&.account_id
  78. when 'Mention'
  79. self.from_account_id = activity(false)&.status&.account_id
  80. end
  81. end
  82. end