block.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # created_at :datetime not null
  8. # updated_at :datetime not null
  9. # account_id :bigint(8) not null
  10. # target_account_id :bigint(8) not null
  11. # uri :string
  12. #
  13. class Block < ApplicationRecord
  14. include Paginable
  15. include RelationshipCacheable
  16. belongs_to :account
  17. belongs_to :target_account, class_name: 'Account'
  18. validates :account_id, uniqueness: { scope: :target_account_id }
  19. def local?
  20. false # Force uri_for to use uri attribute
  21. end
  22. before_validation :set_uri, only: :create
  23. after_commit :invalidate_blocking_cache
  24. after_commit :invalidate_follow_recommendations_cache
  25. private
  26. def invalidate_blocking_cache
  27. Rails.cache.delete("exclude_account_ids_for:#{account_id}")
  28. Rails.cache.delete("exclude_account_ids_for:#{target_account_id}")
  29. end
  30. def invalidate_follow_recommendations_cache
  31. Rails.cache.delete("follow_recommendations/#{account_id}")
  32. end
  33. def set_uri
  34. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
  35. end
  36. end