status_policy.rb 810 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # frozen_string_literal: true
  2. class StatusPolicy
  3. attr_reader :account, :status
  4. def initialize(account, status)
  5. @account = account
  6. @status = status
  7. end
  8. def show?
  9. if direct?
  10. owned? || status.mentions.where(account: account).exists?
  11. elsif private?
  12. owned? || account&.following?(status.account) || status.mentions.where(account: account).exists?
  13. else
  14. account.nil? || !status.account.blocking?(account)
  15. end
  16. end
  17. def reblog?
  18. !direct? && !private? && show?
  19. end
  20. def destroy?
  21. admin? || owned?
  22. end
  23. alias unreblog? destroy?
  24. private
  25. def admin?
  26. account&.user&.admin?
  27. end
  28. def direct?
  29. status.direct_visibility?
  30. end
  31. def owned?
  32. status.account.id == account&.id
  33. end
  34. def private?
  35. status.private_visibility?
  36. end
  37. end