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.

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