update_status_service_spec.rb 5.8 KB

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