status_policy.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 direct?
  12. owned? || mention_exists?
  13. elsif private?
  14. owned? || following_author? || mention_exists?
  15. else
  16. current_account.nil? || !author_blocking?
  17. end
  18. end
  19. def reblog?
  20. !direct? && (!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 direct?
  34. record.direct_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 blocking_author?
  51. return false if current_account.nil?
  52. @preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
  53. end
  54. def author_blocking?
  55. return false if current_account.nil?
  56. @preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
  57. end
  58. def following_author?
  59. return false if current_account.nil?
  60. @preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
  61. end
  62. def author
  63. record.account
  64. end
  65. end