1
0

remove_status_service_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe RemoveStatusService, :inline_jobs do
  4. subject { described_class.new }
  5. let!(:alice) { Fabricate(:account) }
  6. let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  7. let!(:jeff) { Fabricate(:account) }
  8. let!(:hank) { Fabricate(:account, username: 'hank', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
  9. let!(:bill) { Fabricate(:account, username: 'bill', protocol: :activitypub, domain: 'example2.com', inbox_url: 'http://example2.com/inbox') }
  10. before do
  11. stub_request(:post, hank.inbox_url).to_return(status: 200)
  12. stub_request(:post, bill.inbox_url).to_return(status: 200)
  13. jeff.follow!(alice)
  14. hank.follow!(alice)
  15. end
  16. context 'when removed status is not a reblog' do
  17. let!(:media_attachment) { Fabricate(:media_attachment, account: alice) }
  18. let!(:status) { PostStatusService.new.call(alice, text: "Hello @#{bob.pretty_acct} ThisIsASecret", media_ids: [media_attachment.id]) }
  19. before do
  20. FavouriteService.new.call(jeff, status)
  21. Fabricate(:status, account: bill, reblog: status, uri: 'hoge')
  22. end
  23. it 'removes status from notifications and from author and local follower home feeds, publishes to media timeline, sends delete activities' do
  24. allow(redis).to receive(:publish).with(any_args)
  25. expect { subject.call(status) }
  26. .to remove_status_from_notifications
  27. expect(home_feed_ids(alice))
  28. .to_not include(status.id)
  29. expect(home_feed_ids(jeff))
  30. .to_not include(status.id)
  31. expect(redis)
  32. .to have_received(:publish).with('timeline:public:media', Oj.dump(event: :delete, payload: status.id.to_s))
  33. expect(delete_delivery(hank, status))
  34. .to have_been_made.once
  35. expect(delete_delivery(bill, status))
  36. .to have_been_made.once
  37. end
  38. def home_feed_ids(personage)
  39. HomeFeed
  40. .new(personage)
  41. .get(10)
  42. .pluck(:id)
  43. end
  44. def remove_status_from_notifications
  45. change { Notification.where(activity_type: 'Favourite', from_account: jeff, account: alice).count }
  46. .from(1)
  47. .to(0)
  48. end
  49. def delete_delivery(target, status)
  50. a_request(:post, target.inbox_url)
  51. .with(body: delete_activity_for(status))
  52. end
  53. def delete_activity_for(status)
  54. hash_including(
  55. 'type' => 'Delete',
  56. 'object' => {
  57. 'type' => 'Tombstone',
  58. 'id' => ActivityPub::TagManager.instance.uri_for(status),
  59. 'atomUri' => OStatus::TagManager.instance.uri_for(status),
  60. }
  61. )
  62. end
  63. end
  64. context 'when removed status is a private self-reblog' do
  65. let!(:original_status) { Fabricate(:status, account: alice, text: 'Hello ThisIsASecret', visibility: :private) }
  66. let!(:status) { ReblogService.new.call(alice, original_status) }
  67. it 'sends Undo activity to followers' do
  68. subject.call(status)
  69. expect(undo_delivery(hank, original_status))
  70. .to have_been_made.once
  71. end
  72. end
  73. context 'when removed status is public self-reblog' do
  74. let!(:original_status) { Fabricate(:status, account: alice, text: 'Hello ThisIsASecret', visibility: :public) }
  75. let!(:status) { ReblogService.new.call(alice, original_status) }
  76. it 'sends Undo activity to followers' do
  77. subject.call(status)
  78. expect(undo_delivery(hank, original_status))
  79. .to have_been_made.once
  80. end
  81. end
  82. context 'when removed status is a reblog of a non-follower' do
  83. let!(:original_status) { Fabricate(:status, account: bill, text: 'Hello ThisIsASecret', visibility: :public) }
  84. let!(:status) { ReblogService.new.call(alice, original_status) }
  85. it 'sends Undo activity to followers' do
  86. subject.call(status)
  87. expect(undo_delivery(bill, original_status))
  88. .to have_been_made.once
  89. end
  90. end
  91. def undo_delivery(target, status)
  92. a_request(:post, target.inbox_url)
  93. .with(body: undo_activity_for(status))
  94. end
  95. def undo_activity_for(status)
  96. hash_including(
  97. 'type' => 'Undo',
  98. 'object' => hash_including(
  99. 'type' => 'Announce',
  100. 'object' => ActivityPub::TagManager.instance.uri_for(status)
  101. )
  102. )
  103. end
  104. end