undo.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Undo < ActivityPub::Activity
  3. def perform
  4. case @object['type']
  5. when 'Announce'
  6. undo_announce
  7. when 'Accept'
  8. undo_accept
  9. when 'Follow'
  10. undo_follow
  11. when 'Like'
  12. undo_like
  13. when 'Block'
  14. undo_block
  15. when nil
  16. handle_reference
  17. end
  18. end
  19. private
  20. def handle_reference
  21. # Some implementations do not inline the object, and as we don't have a
  22. # global index, we have to guess what object it is.
  23. return if object_uri.nil?
  24. try_undo_announce || try_undo_accept || try_undo_follow || try_undo_like || try_undo_block || delete_later!(object_uri)
  25. end
  26. def try_undo_announce
  27. status = Status.where.not(reblog_of_id: nil).find_by(uri: object_uri, account: @account)
  28. if status.present?
  29. RemoveStatusService.new.call(status)
  30. true
  31. else
  32. false
  33. end
  34. end
  35. def try_undo_accept
  36. # We can't currently handle `Undo Accept` as we don't record `Accept`'s uri
  37. false
  38. end
  39. def try_undo_follow
  40. follow = @account.follow_requests.find_by(uri: object_uri) || @account.active_relationships.find_by(uri: object_uri)
  41. if follow.present?
  42. follow.destroy
  43. true
  44. else
  45. false
  46. end
  47. end
  48. def try_undo_like
  49. # There is an index on accounts, but an account may have *many* favs, so this may be too costly
  50. false
  51. end
  52. def try_undo_block
  53. block = @account.block_relationships.find_by(uri: object_uri)
  54. if block.present?
  55. UnblockService.new.call(@account, block.target_account)
  56. true
  57. else
  58. false
  59. end
  60. end
  61. def undo_announce
  62. return if object_uri.nil?
  63. status = Status.find_by(uri: object_uri, account: @account)
  64. status ||= Status.find_by(uri: @object['atomUri'], account: @account) if @object.is_a?(Hash) && @object['atomUri'].present?
  65. if status.nil?
  66. delete_later!(object_uri)
  67. else
  68. RemoveStatusService.new.call(status)
  69. end
  70. end
  71. def undo_accept
  72. ::Follow.find_by(target_account: @account, uri: target_uri)&.revoke_request!
  73. end
  74. def undo_follow
  75. target_account = account_from_uri(target_uri)
  76. return if target_account.nil? || !target_account.local?
  77. if @account.following?(target_account)
  78. @account.unfollow!(target_account)
  79. elsif @account.requested?(target_account)
  80. FollowRequest.find_by(account: @account, target_account: target_account)&.destroy
  81. else
  82. delete_later!(object_uri)
  83. end
  84. end
  85. def undo_like
  86. status = status_from_uri(target_uri)
  87. return if status.nil? || !status.account.local?
  88. if @account.favourited?(status)
  89. favourite = status.favourites.where(account: @account).first
  90. favourite&.destroy
  91. else
  92. delete_later!(object_uri)
  93. end
  94. end
  95. def undo_block
  96. target_account = account_from_uri(target_uri)
  97. return if target_account.nil? || !target_account.local?
  98. if @account.blocking?(target_account)
  99. UnblockService.new.call(@account, target_account)
  100. else
  101. delete_later!(object_uri)
  102. end
  103. end
  104. def target_uri
  105. @target_uri ||= value_or_id(@object['object'])
  106. end
  107. end