闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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