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.

80 lines
1.8 KiB

  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. end
  16. end
  17. private
  18. def undo_announce
  19. return if object_uri.nil?
  20. status = Status.find_by(uri: object_uri, account: @account)
  21. status ||= Status.find_by(uri: @object['atomUri'], account: @account) if @object.is_a?(Hash) && @object['atomUri'].present?
  22. if status.nil?
  23. delete_later!(object_uri)
  24. else
  25. RemoveStatusService.new.call(status)
  26. end
  27. end
  28. def undo_accept
  29. ::Follow.find_by(target_account: @account, uri: target_uri)&.revoke_request!
  30. end
  31. def undo_follow
  32. target_account = account_from_uri(target_uri)
  33. return if target_account.nil? || !target_account.local?
  34. if @account.following?(target_account)
  35. @account.unfollow!(target_account)
  36. elsif @account.requested?(target_account)
  37. FollowRequest.find_by(account: @account, target_account: target_account)&.destroy
  38. else
  39. delete_later!(object_uri)
  40. end
  41. end
  42. def undo_like
  43. status = status_from_uri(target_uri)
  44. return if status.nil? || !status.account.local?
  45. if @account.favourited?(status)
  46. favourite = status.favourites.where(account: @account).first
  47. favourite&.destroy
  48. else
  49. delete_later!(object_uri)
  50. end
  51. end
  52. def undo_block
  53. target_account = account_from_uri(target_uri)
  54. return if target_account.nil? || !target_account.local?
  55. if @account.blocking?(target_account)
  56. UnblockService.new.call(@account, target_account)
  57. else
  58. delete_later!(object_uri)
  59. end
  60. end
  61. def target_uri
  62. @target_uri ||= value_or_id(@object['object'])
  63. end
  64. end