status_policy.rb 2.2 KB

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