You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.0 KiB

  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? || owned?
  32. end
  33. def review?
  34. staff?
  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