update_status_service_spec.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UpdateStatusService, type: :service do
  4. subject { described_class.new }
  5. context 'when nothing changes' do
  6. let!(:status) { Fabricate(:status, text: 'Foo', language: 'en') }
  7. before do
  8. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  9. subject.call(status, status.account_id, text: 'Foo')
  10. end
  11. it 'does not create an edit' do
  12. expect(status.reload.edits).to be_empty
  13. end
  14. it 'does not notify anyone' do
  15. expect(ActivityPub::DistributionWorker).to_not have_received(:perform_async)
  16. end
  17. end
  18. context 'when text changes' do
  19. let(:status) { Fabricate(:status, text: 'Foo') }
  20. let(:preview_card) { Fabricate(:preview_card) }
  21. before do
  22. PreviewCardsStatus.create(status: status, preview_card: preview_card)
  23. subject.call(status, status.account_id, text: 'Bar')
  24. end
  25. it 'updates text' do
  26. expect(status.reload.text).to eq 'Bar'
  27. end
  28. it 'resets preview card' do
  29. expect(status.reload.preview_card).to be_nil
  30. end
  31. it 'saves edit history' do
  32. expect(status.edits.pluck(:text)).to eq %w(Foo Bar)
  33. end
  34. end
  35. context 'when content warning changes' do
  36. let(:status) { Fabricate(:status, text: 'Foo', spoiler_text: '') }
  37. let(:preview_card) { Fabricate(:preview_card) }
  38. before do
  39. PreviewCardsStatus.create(status: status, preview_card: preview_card)
  40. subject.call(status, status.account_id, text: 'Foo', spoiler_text: 'Bar')
  41. end
  42. it 'updates content warning' do
  43. expect(status.reload.spoiler_text).to eq 'Bar'
  44. end
  45. it 'saves edit history' do
  46. expect(status.edits.pluck(:text, :spoiler_text)).to eq [['Foo', ''], ['Foo', 'Bar']]
  47. end
  48. end
  49. context 'when media attachments change' do
  50. let!(:status) { Fabricate(:status, text: 'Foo') }
  51. let!(:detached_media_attachment) { Fabricate(:media_attachment, account: status.account) }
  52. let!(:attached_media_attachment) { Fabricate(:media_attachment, account: status.account) }
  53. before do
  54. status.media_attachments << detached_media_attachment
  55. subject.call(status, status.account_id, text: 'Foo', media_ids: [attached_media_attachment.id])
  56. end
  57. it 'updates media attachments' do
  58. expect(status.ordered_media_attachments).to eq [attached_media_attachment]
  59. end
  60. it 'does not detach detached media attachments' do
  61. expect(detached_media_attachment.reload.status_id).to eq status.id
  62. end
  63. it 'attaches attached media attachments' do
  64. expect(attached_media_attachment.reload.status_id).to eq status.id
  65. end
  66. it 'saves edit history' do
  67. expect(status.edits.pluck(:ordered_media_attachment_ids)).to eq [[detached_media_attachment.id], [attached_media_attachment.id]]
  68. end
  69. end
  70. context 'when already-attached media changes' do
  71. let!(:status) { Fabricate(:status, text: 'Foo') }
  72. let!(:media_attachment) { Fabricate(:media_attachment, account: status.account, description: 'Old description') }
  73. before do
  74. status.media_attachments << media_attachment
  75. subject.call(status, status.account_id, text: 'Foo', media_ids: [media_attachment.id], media_attributes: [{ id: media_attachment.id, description: 'New description' }])
  76. end
  77. it 'does not detach media attachment' do
  78. expect(media_attachment.reload.status_id).to eq status.id
  79. end
  80. it 'updates the media attachment description' do
  81. expect(media_attachment.reload.description).to eq 'New description'
  82. end
  83. it 'saves edit history' do
  84. expect(status.edits.map { |edit| edit.ordered_media_attachments.map(&:description) }).to eq [['Old description'], ['New description']]
  85. end
  86. end
  87. context 'when poll changes' do
  88. let(:account) { Fabricate(:account) }
  89. let!(:status) { Fabricate(:status, text: 'Foo', account: account, poll_attributes: { options: %w(Foo Bar), account: account, multiple: false, hide_totals: false, expires_at: 7.days.from_now }) }
  90. let!(:poll) { status.poll }
  91. let!(:voter) { Fabricate(:account) }
  92. before do
  93. status.update(poll: poll)
  94. VoteService.new.call(voter, poll, [0])
  95. subject.call(status, status.account_id, text: 'Foo', poll: { options: %w(Bar Baz Foo), expires_in: 5.days.to_i })
  96. end
  97. it 'updates poll' do
  98. poll = status.poll.reload
  99. expect(poll.options).to eq %w(Bar Baz Foo)
  100. end
  101. it 'resets votes' do
  102. poll = status.poll.reload
  103. expect(poll.votes_count).to eq 0
  104. expect(poll.votes.count).to eq 0
  105. expect(poll.cached_tallies).to eq [0, 0, 0]
  106. end
  107. it 'saves edit history' do
  108. expect(status.edits.pluck(:poll_options)).to eq [%w(Foo Bar), %w(Bar Baz Foo)]
  109. end
  110. it 'requeues expiration notification' do
  111. poll = status.poll.reload
  112. expect(PollExpirationNotifyWorker).to have_enqueued_sidekiq_job(poll.id).at(poll.expires_at + 5.minutes)
  113. end
  114. end
  115. context 'when mentions in text change' do
  116. let!(:account) { Fabricate(:account) }
  117. let!(:alice) { Fabricate(:account, username: 'alice') }
  118. let!(:bob) { Fabricate(:account, username: 'bob') }
  119. let!(:status) { PostStatusService.new.call(account, text: 'Hello @alice') }
  120. before do
  121. subject.call(status, status.account_id, text: 'Hello @bob')
  122. end
  123. it 'changes mentions' do
  124. expect(status.active_mentions.pluck(:account_id)).to eq [bob.id]
  125. end
  126. it 'keeps old mentions as silent mentions' do
  127. expect(status.mentions.pluck(:account_id)).to contain_exactly(alice.id, bob.id)
  128. end
  129. end
  130. context 'when hashtags in text change' do
  131. let!(:account) { Fabricate(:account) }
  132. let!(:status) { PostStatusService.new.call(account, text: 'Hello #foo') }
  133. before do
  134. subject.call(status, status.account_id, text: 'Hello #bar')
  135. end
  136. it 'changes tags' do
  137. expect(status.tags.pluck(:name)).to eq %w(bar)
  138. end
  139. end
  140. it 'notifies ActivityPub about the update' do
  141. status = Fabricate(:status, text: 'Foo')
  142. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  143. subject.call(status, status.account_id, text: 'Bar')
  144. expect(ActivityPub::DistributionWorker).to have_received(:perform_async)
  145. end
  146. end