1
0

notification_policy.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: notification_policies
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8) not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # for_not_following :integer default("accept"), not null
  11. # for_not_followers :integer default("accept"), not null
  12. # for_new_accounts :integer default("accept"), not null
  13. # for_private_mentions :integer default("filter"), not null
  14. # for_limited_accounts :integer default("filter"), not null
  15. #
  16. class NotificationPolicy < ApplicationRecord
  17. self.ignored_columns += %w(
  18. filter_not_following
  19. filter_not_followers
  20. filter_new_accounts
  21. filter_private_mentions
  22. )
  23. belongs_to :account
  24. has_many :notification_requests, primary_key: :account_id, foreign_key: :account_id, dependent: nil, inverse_of: false
  25. attr_reader :pending_requests_count, :pending_notifications_count
  26. MAX_MEANINGFUL_COUNT = 100
  27. enum :for_not_following, { accept: 0, filter: 1, drop: 2 }, suffix: :not_following
  28. enum :for_not_followers, { accept: 0, filter: 1, drop: 2 }, suffix: :not_followers
  29. enum :for_new_accounts, { accept: 0, filter: 1, drop: 2 }, suffix: :new_accounts
  30. enum :for_private_mentions, { accept: 0, filter: 1, drop: 2 }, suffix: :private_mentions
  31. enum :for_limited_accounts, { accept: 0, filter: 1, drop: 2 }, suffix: :limited_accounts
  32. def summarize!
  33. @pending_requests_count = pending_notification_requests.first
  34. @pending_notifications_count = pending_notification_requests.last
  35. end
  36. # Compat helpers with V1
  37. def filter_not_following=(value)
  38. self.for_not_following = value ? :filter : :accept
  39. end
  40. def filter_not_followers=(value)
  41. self.for_not_followers = value ? :filter : :accept
  42. end
  43. def filter_new_accounts=(value)
  44. self.for_new_accounts = value ? :filter : :accept
  45. end
  46. def filter_private_mentions=(value)
  47. self.for_private_mentions = value ? :filter : :accept
  48. end
  49. private
  50. def pending_notification_requests
  51. @pending_notification_requests ||= notification_requests.without_suspended.limit(MAX_MEANINGFUL_COUNT).pick(Arel.sql('count(*), coalesce(sum(notifications_count), 0)::bigint'))
  52. end
  53. end