notify_service_spec.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. context 'when the sender is a local moderator' do
  16. let(:sender) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  17. let(:type) { :mention }
  18. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender)) }
  19. it 'does notify when the sender is blocked' do
  20. recipient.block!(sender)
  21. expect { subject }.to change(Notification, :count).by(1)
  22. end
  23. end
  24. it 'does not notify when sender is muted with hide_notifications' do
  25. recipient.mute!(sender, notifications: true)
  26. expect { subject }.to_not change(Notification, :count)
  27. end
  28. it 'does notify when sender is muted without hide_notifications' do
  29. recipient.mute!(sender, notifications: false)
  30. expect { subject }.to change(Notification, :count)
  31. end
  32. it 'does not notify when sender\'s domain is blocked' do
  33. recipient.block_domain!(sender.domain)
  34. expect { subject }.to_not change(Notification, :count)
  35. end
  36. it 'does still notify when sender\'s domain is blocked but sender is followed' do
  37. recipient.block_domain!(sender.domain)
  38. recipient.follow!(sender)
  39. expect { subject }.to change(Notification, :count)
  40. end
  41. it 'does not notify when sender is silenced and not followed' do
  42. sender.silence!
  43. expect { subject }.to_not change(Notification, :count)
  44. end
  45. it 'does not notify when recipient is suspended' do
  46. recipient.suspend!
  47. expect { subject }.to_not change(Notification, :count)
  48. end
  49. context 'with direct messages' do
  50. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct)) }
  51. let(:type) { :mention }
  52. before do
  53. user.settings.update('interactions.must_be_following_dm': enabled)
  54. user.save
  55. end
  56. context 'when recipient is supposed to be following sender' do
  57. let(:enabled) { true }
  58. it 'does not notify' do
  59. expect { subject }.to_not change(Notification, :count)
  60. end
  61. context 'when the message chain is initiated by recipient, but is not direct message' do
  62. let(:reply_to) { Fabricate(:status, account: recipient) }
  63. let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
  64. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
  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 recipient, but without a mention to the sender, even if the sender sends multiple messages in a row' do
  70. let(:public_status) { Fabricate(:status, account: recipient) }
  71. let(:intermediate_reply) { Fabricate(:status, account: sender, thread: public_status, visibility: :direct) }
  72. let!(:intermediate_mention) { Fabricate(:mention, account: sender, status: intermediate_reply) }
  73. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: intermediate_reply)) }
  74. it 'does not notify' do
  75. expect { subject }.to_not change(Notification, :count)
  76. end
  77. end
  78. context 'when the message chain is initiated by the recipient with a mention to the sender' do
  79. let(:reply_to) { Fabricate(:status, account: recipient, visibility: :direct) }
  80. let!(:mention) { Fabricate(:mention, account: sender, status: reply_to) }
  81. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, visibility: :direct, thread: reply_to)) }
  82. it 'does notify' do
  83. expect { subject }.to change(Notification, :count)
  84. end
  85. end
  86. end
  87. context 'when recipient is NOT supposed to be following sender' do
  88. let(:enabled) { false }
  89. it 'does notify' do
  90. expect { subject }.to change(Notification, :count)
  91. end
  92. end
  93. end
  94. describe 'reblogs' do
  95. let(:status) { Fabricate(:status, account: Fabricate(:account)) }
  96. let(:activity) { Fabricate(:status, account: sender, reblog: status) }
  97. let(:type) { :reblog }
  98. it 'shows reblogs by default' do
  99. recipient.follow!(sender)
  100. expect { subject }.to change(Notification, :count)
  101. end
  102. it 'shows reblogs when explicitly enabled' do
  103. recipient.follow!(sender, reblogs: true)
  104. expect { subject }.to change(Notification, :count)
  105. end
  106. it 'shows reblogs when disabled' do
  107. recipient.follow!(sender, reblogs: false)
  108. expect { subject }.to change(Notification, :count)
  109. end
  110. end
  111. context 'with muted and blocked users' do
  112. let(:asshole) { Fabricate(:account, username: 'asshole') }
  113. let(:reply_to) { Fabricate(:status, account: asshole) }
  114. let(:activity) { Fabricate(:mention, account: recipient, status: Fabricate(:status, account: sender, thread: reply_to)) }
  115. let(:type) { :mention }
  116. it 'does not notify when conversation is muted' do
  117. recipient.mute_conversation!(activity.status.conversation)
  118. expect { subject }.to_not change(Notification, :count)
  119. end
  120. it 'does not notify when it is a reply to a blocked user' do
  121. recipient.block!(asshole)
  122. expect { subject }.to_not change(Notification, :count)
  123. end
  124. end
  125. context 'with sender as recipient' do
  126. let(:sender) { recipient }
  127. it 'does not notify when recipient is the sender' do
  128. expect { subject }.to_not change(Notification, :count)
  129. end
  130. end
  131. describe 'email' do
  132. before do
  133. ActionMailer::Base.deliveries.clear
  134. user.settings.update('notification_emails.follow': enabled)
  135. user.save
  136. end
  137. context 'when email notification is enabled' do
  138. let(:enabled) { true }
  139. it 'sends email' do
  140. expect { subject }.to change(ActionMailer::Base.deliveries, :count).by(1)
  141. end
  142. end
  143. context 'when email notification is disabled' do
  144. let(:enabled) { false }
  145. it "doesn't send email" do
  146. expect { subject }.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  147. end
  148. end
  149. end
  150. end