1
0

notify_service_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe NotifyService, type: :service do
  4. subject { described_class.new.call(recipient, type, activity) }
  5. let(:user) { Fabricate(:user) }
  6. let(:recipient) { user.account }
  7. let(:sender) { Fabricate(:account, domain: 'example.com') }
  8. let(:activity) { Fabricate(:follow, account: sender, target_account: recipient) }
  9. let(:type) { :follow }
  10. it { expect { subject }.to change(Notification, :count).by(1) }
  11. it 'does not notify when sender is blocked' do
  12. recipient.block!(sender)
  13. expect { subject }.to_not change(Notification, :count)
  14. end
  15. it 'does not notify when sender is muted with hide_notifications' do
  16. recipient.mute!(sender, notifications: true)
  17. expect { subject }.to_not change(Notification, :count)
  18. end
  19. it 'does notify when sender is muted without hide_notifications' do
  20. recipient.mute!(sender, notifications: false)
  21. expect { subject }.to change(Notification, :count)
  22. end
  23. it 'does not notify when sender\'s domain is blocked' do
  24. recipient.block_domain!(sender.domain)
  25. expect { subject }.to_not change(Notification, :count)
  26. end
  27. it 'does still notify when sender\'s domain is blocked but sender is followed' do
  28. recipient.block_domain!(sender.domain)
  29. recipient.follow!(sender)
  30. expect { subject }.to change(Notification, :count)
  31. end
  32. it 'does not notify when sender is silenced and not followed' do
  33. sender.silence!
  34. expect { subject }.to_not change(Notification, :count)
  35. end
  36. it 'does not notify when recipient is suspended' do
  37. recipient.suspend!
  38. expect { subject }.to_not change(Notification, :count)
  39. end
  40. context 'with direct messages' do
  41. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct)) }
  42. let(:type) { :mention }
  43. before do
  44. user.settings.update('interactions.must_be_following_dm': enabled)
  45. user.save
  46. end
  47. context 'when recipient is supposed to be following sender' do
  48. let(:enabled) { true }
  49. it 'does not notify' do
  50. expect { subject }.to_not change(Notification, :count)
  51. end
  52. context 'when the message chain is initiated by recipient, but is not direct message' do
  53. let(:reply_to) { Fabricate(:status, account: recipient) }
  54. let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
  55. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
  56. it 'does not notify' do
  57. expect { subject }.to_not change(Notification, :count)
  58. end
  59. end
  60. context 'when the message chain is initiated by recipient, but without a mention to the sender, even if the sender sends multiple messages in a row' do
  61. let(:reply_to) { Fabricate(:status, account: recipient) }
  62. let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
  63. let(:dummy_reply) { Fabricate(:status, account: sender, visibility: :direct, thread: reply_to) }
  64. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: dummy_reply)) }
  65. it 'does not notify' do
  66. expect { subject }.to_not change(Notification, :count)
  67. end
  68. end
  69. context 'when the message chain is initiated by the recipient with a mention to the sender' do
  70. let(:reply_to) { Fabricate(:status, account: recipient, visibility: :direct) }
  71. let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
  72. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
  73. it 'does notify' do
  74. expect { subject }.to change(Notification, :count)
  75. end
  76. end
  77. end
  78. context 'when recipient is NOT supposed to be following sender' do
  79. let(:enabled) { false }
  80. it 'does notify' do
  81. expect { subject }.to change(Notification, :count)
  82. end
  83. end
  84. end
  85. describe 'reblogs' do
  86. let(:status) { Fabricate(:status, account: Fabricate(:account)) }
  87. let(:activity) { Fabricate(:status, account: sender, reblog: status) }
  88. let(:type) { :reblog }
  89. it 'shows reblogs by default' do
  90. recipient.follow!(sender)
  91. expect { subject }.to change(Notification, :count)
  92. end
  93. it 'shows reblogs when explicitly enabled' do
  94. recipient.follow!(sender, reblogs: true)
  95. expect { subject }.to change(Notification, :count)
  96. end
  97. it 'shows reblogs when disabled' do
  98. recipient.follow!(sender, reblogs: false)
  99. expect { subject }.to change(Notification, :count)
  100. end
  101. end
  102. context 'with muted and blocked users' do
  103. let(:asshole) { Fabricate(:account, username: 'asshole') }
  104. let(:reply_to) { Fabricate(:status, account: asshole) }
  105. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, thread: reply_to)) }
  106. let(:type) { :mention }
  107. it 'does not notify when conversation is muted' do
  108. recipient.mute_conversation!(activity.status.conversation)
  109. expect { subject }.to_not change(Notification, :count)
  110. end
  111. it 'does not notify when it is a reply to a blocked user' do
  112. recipient.block!(asshole)
  113. expect { subject }.to_not change(Notification, :count)
  114. end
  115. end
  116. context 'with sender as recipient' do
  117. let(:sender) { recipient }
  118. it 'does not notify when recipient is the sender' do
  119. expect { subject }.to_not change(Notification, :count)
  120. end
  121. end
  122. describe 'email' do
  123. before do
  124. ActionMailer::Base.deliveries.clear
  125. user.settings.update('notification_emails.follow': enabled)
  126. user.save
  127. end
  128. context 'when email notification is enabled' do
  129. let(:enabled) { true }
  130. it 'sends email' do
  131. expect { subject }.to change(ActionMailer::Base.deliveries, :count).by(1)
  132. end
  133. end
  134. context 'when email notification is disabled' do
  135. let(:enabled) { false }
  136. it "doesn't send email" do
  137. expect { subject }.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  138. end
  139. end
  140. end
  141. end