The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

105 rivejä
3.1 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. TYPE_CLASS_MAP = {
  18. mention: 'Mention',
  19. reblog: 'Status',
  20. follow: 'Follow',
  21. follow_request: 'FollowRequest',
  22. favourite: 'Favourite',
  23. }.freeze
  24. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
  25. belongs_to :account
  26. belongs_to :from_account, class_name: 'Account'
  27. belongs_to :activity, polymorphic: true
  28. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id'
  29. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id'
  30. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  31. belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id'
  32. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  33. validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
  34. validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
  35. scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) }
  36. scope :browserable, ->(exclude_types = []) {
  37. types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types + [:follow_request])
  38. where(activity_type: types)
  39. }
  40. cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
  41. def activity(eager_loaded = true)
  42. eager_loaded ? send(activity_type.downcase) : super
  43. end
  44. def type
  45. @type ||= TYPE_CLASS_MAP.invert[activity_type].to_sym
  46. end
  47. def target_status
  48. case type
  49. when :reblog
  50. activity&.reblog
  51. when :favourite, :mention
  52. activity&.status
  53. end
  54. end
  55. def browserable?
  56. type != :follow_request
  57. end
  58. class << self
  59. def reload_stale_associations!(cached_items)
  60. account_ids = cached_items.map(&:from_account_id).uniq
  61. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  62. cached_items.each do |item|
  63. item.from_account = accounts[item.from_account_id]
  64. end
  65. end
  66. private
  67. def activity_types_from_types(types)
  68. types.map { |type| TYPE_CLASS_MAP[type.to_sym] }.compact
  69. end
  70. end
  71. after_initialize :set_from_account
  72. before_validation :set_from_account
  73. private
  74. def set_from_account
  75. return unless new_record?
  76. case activity_type
  77. when 'Status', 'Follow', 'Favourite', 'FollowRequest'
  78. self.from_account_id = activity(false)&.account_id
  79. when 'Mention'
  80. self.from_account_id = activity(false)&.status&.account_id
  81. end
  82. end
  83. end