status_policy.rb 2.0 KB

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