list.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #
  12. class List < ApplicationRecord
  13. include Paginable
  14. PER_ACCOUNT_LIMIT = 50
  15. belongs_to :account, optional: true
  16. has_many :list_accounts, inverse_of: :list, dependent: :destroy
  17. has_many :accounts, through: :list_accounts
  18. validates :title, presence: true
  19. validates_each :account_id, on: :create do |record, _attr, value|
  20. record.errors.add(:base, I18n.t('lists.errors.limit')) if List.where(account_id: value).count >= PER_ACCOUNT_LIMIT
  21. end
  22. before_destroy :clean_feed_manager
  23. private
  24. def clean_feed_manager
  25. reblog_key = FeedManager.instance.key(:list, id, 'reblogs')
  26. reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
  27. Redis.current.pipelined do
  28. Redis.current.del(FeedManager.instance.key(:list, id))
  29. Redis.current.del(reblog_key)
  30. reblogged_id_set.each do |reblogged_id|
  31. reblog_set_key = FeedManager.instance.key(:list, id, "reblogs:#{reblogged_id}")
  32. Redis.current.del(reblog_set_key)
  33. end
  34. end
  35. end
  36. end