status_policy.rb 2.0 KB

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