status_policy.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. role.can?(:manage_reports, :manage_users)
  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. role.can?(:manage_reports) || owned?
  28. end
  29. alias unreblog? destroy?
  30. def update?
  31. role.can?(:manage_reports) || owned?
  32. end
  33. def review?
  34. role.can?(:manage_taxonomies)
  35. end
  36. private
  37. def requires_mention?
  38. record.direct_visibility? || record.limited_visibility?
  39. end
  40. def owned?
  41. author.id == current_account&.id
  42. end
  43. def private?
  44. record.private_visibility?
  45. end
  46. def mention_exists?
  47. return false if current_account.nil?
  48. if record.mentions.loaded?
  49. record.mentions.any? { |mention| mention.account_id == current_account.id }
  50. else
  51. record.mentions.where(account: current_account).exists?
  52. end
  53. end
  54. def author_blocking_domain?
  55. return false if current_account.nil? || current_account.domain.nil?
  56. author.domain_blocking?(current_account.domain)
  57. end
  58. def blocking_author?
  59. return false if current_account.nil?
  60. @preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
  61. end
  62. def author_blocking?
  63. return false if current_account.nil?
  64. @preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
  65. end
  66. def following_author?
  67. return false if current_account.nil?
  68. @preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
  69. end
  70. def author
  71. record.account
  72. end
  73. end