status_policy.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. class StatusPolicy < ApplicationPolicy
  3. def initialize(current_account, record, preloaded_relations = {})
  4. super(current_account, record)
  5. @preloaded_relations = preloaded_relations
  6. end
  7. def show?
  8. return false if local_only? && (current_account.nil? || !current_account.local?)
  9. return false if author.suspended?
  10. if requires_mention?
  11. owned? || mention_exists?
  12. elsif private?
  13. owned? || following_author? || mention_exists?
  14. else
  15. current_account.nil? || (!author_blocking? && !author_blocking_domain?)
  16. end
  17. end
  18. def reblog?
  19. !requires_mention? && (!private? || owned?) && show? && !blocking_author?
  20. end
  21. def favourite?
  22. show? && !blocking_author?
  23. end
  24. def destroy?
  25. owned?
  26. end
  27. alias unreblog? destroy?
  28. def update?
  29. owned?
  30. end
  31. private
  32. def requires_mention?
  33. record.direct_visibility? || record.limited_visibility?
  34. end
  35. def owned?
  36. author.id == current_account&.id
  37. end
  38. def private?
  39. record.private_visibility?
  40. end
  41. def mention_exists?
  42. return false if current_account.nil?
  43. if record.mentions.loaded?
  44. record.mentions.any? { |mention| mention.account_id == current_account.id }
  45. else
  46. record.mentions.where(account: current_account).exists?
  47. end
  48. end
  49. def author_blocking_domain?
  50. return false if current_account.nil? || current_account.domain.nil?
  51. author.domain_blocking?(current_account.domain)
  52. end
  53. def blocking_author?
  54. return false if current_account.nil?
  55. @preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
  56. end
  57. def author_blocking?
  58. return false if current_account.nil?
  59. @preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
  60. end
  61. def following_author?
  62. return false if current_account.nil?
  63. @preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
  64. end
  65. def author
  66. record.account
  67. end
  68. def local_only?
  69. record.local_only?
  70. end
  71. end