1
0

notify_service_spec.rb 6.1 KB

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