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.

113 lines
3.8 KiB

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