update_status_service_spec.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UpdateStatusService 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. end
  10. it 'does not create an edit or notify anyone' do
  11. subject.call(status, status.account_id, text: 'Foo')
  12. expect(status.reload.edits)
  13. .to be_empty
  14. expect(ActivityPub::DistributionWorker)
  15. .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. end
  24. it 'updates text, resets card, saves edit history' do
  25. subject.call(status, status.account_id, text: 'Bar')
  26. expect(status.reload)
  27. .to have_attributes(
  28. text: 'Bar',
  29. preview_card: be_nil
  30. )
  31. expect(status.edits.ordered.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. PreviewCardsStatus.create(status: status, preview_card: preview_card)
  39. end
  40. it 'updates content warning and saves history' do
  41. subject.call(status, status.account_id, text: 'Foo', spoiler_text: 'Bar')
  42. expect(status.reload.spoiler_text)
  43. .to eq 'Bar'
  44. expect(status.edits.ordered.pluck(:text, :spoiler_text))
  45. .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. end
  55. it 'updates media attachments, handles attachments, saves history' do
  56. subject.call(status, status.account_id, text: 'Foo', media_ids: [attached_media_attachment.id.to_s])
  57. expect(status.ordered_media_attachments)
  58. .to eq [attached_media_attachment]
  59. expect(detached_media_attachment.reload.status_id)
  60. .to eq status.id
  61. expect(attached_media_attachment.reload.status_id)
  62. .to eq status.id
  63. expect(status.edits.ordered.pluck(:ordered_media_attachment_ids))
  64. .to eq [[detached_media_attachment.id], [attached_media_attachment.id]]
  65. end
  66. end
  67. context 'when already-attached media changes' do
  68. let!(:status) { Fabricate(:status, text: 'Foo') }
  69. let!(:media_attachment) { Fabricate(:media_attachment, account: status.account, description: 'Old description') }
  70. before do
  71. status.media_attachments << media_attachment
  72. end
  73. it 'does not detach media attachment, updates description, and saves history' do
  74. subject.call(status, status.account_id, text: 'Foo', media_ids: [media_attachment.id.to_s], media_attributes: [{ id: media_attachment.id, description: 'New description' }])
  75. expect(media_attachment.reload)
  76. .to have_attributes(
  77. status_id: status.id,
  78. description: 'New description'
  79. )
  80. expect(status.edits.ordered.map { |edit| edit.ordered_media_attachments.map(&:description) })
  81. .to eq [['Old description'], ['New description']]
  82. end
  83. end
  84. context 'when poll changes' do
  85. let(:account) { Fabricate(:account) }
  86. 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 }) }
  87. let!(:poll) { status.poll }
  88. let!(:voter) { Fabricate(:account) }
  89. before do
  90. status.update(poll: poll)
  91. VoteService.new.call(voter, poll, [0])
  92. end
  93. it 'updates poll, resets votes, saves history, requeues notifications' do
  94. subject.call(status, status.account_id, text: 'Foo', poll: { options: %w(Bar Baz Foo), expires_in: 5.days.to_i })
  95. poll = status.poll.reload
  96. expect(poll)
  97. .to have_attributes(
  98. options: %w(Bar Baz Foo),
  99. votes_count: 0,
  100. cached_tallies: [0, 0, 0]
  101. )
  102. expect(poll.votes.count)
  103. .to eq(0)
  104. expect(status.edits.ordered.pluck(:poll_options))
  105. .to eq [%w(Foo Bar), %w(Bar Baz Foo)]
  106. expect(PollExpirationNotifyWorker)
  107. .to have_enqueued_sidekiq_job(poll.id).at(poll.expires_at + 5.minutes)
  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. it 'changes mentions and keeps old as silent' do
  116. subject.call(status, status.account_id, text: 'Hello @bob')
  117. expect(status.active_mentions.pluck(:account_id))
  118. .to eq [bob.id]
  119. expect(status.mentions.pluck(:account_id))
  120. .to contain_exactly(alice.id, bob.id)
  121. end
  122. end
  123. context 'when hashtags in text change' do
  124. let!(:account) { Fabricate(:account) }
  125. let!(:status) { PostStatusService.new.call(account, text: 'Hello #foo') }
  126. it 'changes tags' do
  127. subject.call(status, status.account_id, text: 'Hello #bar')
  128. expect(status.tags.pluck(:name)).to eq %w(bar)
  129. end
  130. end
  131. it 'notifies ActivityPub about the update' do
  132. status = Fabricate(:status, text: 'Foo')
  133. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  134. subject.call(status, status.account_id, text: 'Bar')
  135. expect(ActivityPub::DistributionWorker).to have_received(:perform_async)
  136. end
  137. end