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.

50 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: lists
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8) not null
  8. # title :string default(""), not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # replies_policy :integer default("list"), not null
  12. #
  13. class List < ApplicationRecord
  14. include Paginable
  15. PER_ACCOUNT_LIMIT = 50
  16. enum replies_policy: [:list, :followed, :none], _prefix: :show
  17. belongs_to :account, optional: true
  18. has_many :list_accounts, inverse_of: :list, dependent: :destroy
  19. has_many :accounts, through: :list_accounts
  20. validates :title, presence: true
  21. validates_each :account_id, on: :create do |record, _attr, value|
  22. record.errors.add(:base, I18n.t('lists.errors.limit')) if List.where(account_id: value).count >= PER_ACCOUNT_LIMIT
  23. end
  24. before_destroy :clean_feed_manager
  25. private
  26. def clean_feed_manager
  27. reblog_key = FeedManager.instance.key(:list, id, 'reblogs')
  28. reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
  29. Redis.current.pipelined do
  30. Redis.current.del(FeedManager.instance.key(:list, id))
  31. Redis.current.del(reblog_key)
  32. reblogged_id_set.each do |reblogged_id|
  33. reblog_set_key = FeedManager.instance.key(:list, id, "reblogs:#{reblogged_id}")
  34. Redis.current.del(reblog_set_key)
  35. end
  36. end
  37. end
  38. end