status_policy.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 author.unavailable?
  9. if requires_mention?
  10. owned? || mention_exists?
  11. elsif private?
  12. owned? || following_author? || mention_exists?
  13. else
  14. current_account.nil? || (!author_blocking? && !author_blocking_domain?)
  15. end
  16. end
  17. def reblog?
  18. !requires_mention? && (!private? || owned?) && show? && !blocking_author?
  19. end
  20. def favourite?
  21. show? && !blocking_author?
  22. end
  23. def destroy?
  24. owned?
  25. end
  26. alias unreblog? destroy?
  27. def update?
  28. owned?
  29. end
  30. private
  31. def requires_mention?
  32. record.direct_visibility? || record.limited_visibility?
  33. end
  34. def owned?
  35. author.id == current_account&.id
  36. end
  37. def private?
  38. record.private_visibility?
  39. end
  40. def mention_exists?
  41. return false if current_account.nil?
  42. if record.mentions.loaded?
  43. record.mentions.any? { |mention| mention.account_id == current_account.id }
  44. else
  45. record.mentions.where(account: current_account).exists?
  46. end
  47. end
  48. def author_blocking_domain?
  49. return false if current_account.nil? || current_account.domain.nil?
  50. author.domain_blocking?(current_account.domain)
  51. end
  52. def blocking_author?
  53. return false if current_account.nil?
  54. @preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
  55. end
  56. def author_blocking?
  57. return false if current_account.nil?
  58. @preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
  59. end
  60. def following_author?
  61. return false if current_account.nil?
  62. @preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
  63. end
  64. def author
  65. record.account
  66. end
  67. end