2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostActionNotifier do
|
2013-04-13 10:31:20 -04:00
|
|
|
before do
|
2016-12-22 00:46:22 -05:00
|
|
|
PostActionNotifier.enable
|
2019-05-08 03:18:56 -04:00
|
|
|
Jobs.run_immediately!
|
2013-04-13 10:31:20 -04:00
|
|
|
end
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:evil_trout) { Fabricate(:evil_trout) }
|
|
|
|
fab!(:post) { Fabricate(:post) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'when editing a post' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'notifies a user of the revision' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2014-10-27 17:06:43 -04:00
|
|
|
post.revise(evil_trout, raw: "world is the new body of the message")
|
2019-05-08 03:18:56 -04:00
|
|
|
}.to change { post.reload.user.notifications.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies watching users of revision when post is wiki-ed and first post in topic' do
|
|
|
|
SiteSetting.editing_grace_period_max_diff = 1
|
|
|
|
|
|
|
|
post.update!(wiki: true)
|
2021-04-14 01:54:09 -04:00
|
|
|
owner = post.user
|
2019-05-08 03:18:56 -04:00
|
|
|
user2 = Fabricate(:user)
|
|
|
|
user3 = Fabricate(:user)
|
|
|
|
|
|
|
|
TopicUser.change(user2.id, post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:watching]
|
|
|
|
)
|
|
|
|
|
|
|
|
TopicUser.change(user3.id, post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking]
|
|
|
|
)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post.revise(Fabricate(:user), raw: "I made some changes to the wiki!")
|
|
|
|
end.to change { Notification.count }.by(2)
|
|
|
|
|
|
|
|
edited_notification_type = Notification.types[:edited]
|
|
|
|
|
|
|
|
expect(Notification.exists?(
|
2021-04-14 01:54:09 -04:00
|
|
|
user: owner,
|
2019-05-08 03:18:56 -04:00
|
|
|
notification_type: edited_notification_type
|
|
|
|
)).to eq(true)
|
|
|
|
|
|
|
|
expect(Notification.exists?(
|
|
|
|
user: user2,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
)).to eq(true)
|
|
|
|
|
|
|
|
expect do
|
2021-04-14 01:54:09 -04:00
|
|
|
post.revise(owner, raw: "I made some changes to the wiki again!")
|
2019-05-08 03:18:56 -04:00
|
|
|
end.to change {
|
|
|
|
Notification.where(notification_type: edited_notification_type).count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
expect(Notification.where(
|
|
|
|
user: user2,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
).count).to eq(2)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post.revise(user2, raw: "I changed the wiki totally")
|
|
|
|
end.to change {
|
|
|
|
Notification.where(notification_type: edited_notification_type).count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
expect(Notification.where(
|
2021-04-14 01:54:09 -04:00
|
|
|
user: owner,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
).count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies watching users of revision when topic category allow_unlimited_owner_edits_on_first_post and first post in topic is edited' do
|
|
|
|
SiteSetting.editing_grace_period_max_diff = 1
|
|
|
|
|
|
|
|
post.topic.update(category: Fabricate(:category, allow_unlimited_owner_edits_on_first_post: true))
|
|
|
|
owner = post.user
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
user3 = Fabricate(:user)
|
|
|
|
|
|
|
|
TopicUser.change(user2.id, post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:watching]
|
|
|
|
)
|
|
|
|
|
|
|
|
TopicUser.change(user3.id, post.topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking]
|
|
|
|
)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post.revise(Fabricate(:user), raw: "I made some changes to the first post!")
|
|
|
|
end.to change { Notification.count }.by(2)
|
|
|
|
|
|
|
|
edited_notification_type = Notification.types[:edited]
|
|
|
|
|
|
|
|
expect(Notification.exists?(
|
|
|
|
user: owner,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
)).to eq(true)
|
|
|
|
|
|
|
|
expect(Notification.exists?(
|
|
|
|
user: user2,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
)).to eq(true)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post.revise(owner, raw: "I made some changes to the first post again!")
|
|
|
|
end.to change {
|
|
|
|
Notification.where(notification_type: edited_notification_type).count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
expect(Notification.where(
|
|
|
|
user: user2,
|
|
|
|
notification_type: edited_notification_type
|
|
|
|
).count).to eq(2)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post.revise(user2, raw: "I changed the first post totally")
|
|
|
|
end.to change {
|
|
|
|
Notification.where(notification_type: edited_notification_type).count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
expect(Notification.where(
|
|
|
|
user: owner,
|
2019-05-08 03:18:56 -04:00
|
|
|
notification_type: edited_notification_type
|
|
|
|
).count).to eq(2)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-06-11 11:14:00 -04:00
|
|
|
|
2018-08-24 09:13:07 -04:00
|
|
|
it 'stores the revision number with the notification' do
|
|
|
|
post.revise(evil_trout, raw: "world is the new body of the message")
|
|
|
|
notification_data = JSON.parse post.user.notifications.last.data
|
|
|
|
expect(notification_data['revision_number']).to eq post.post_revisions.last.number
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when edit notifications are disabled" do
|
2019-07-31 10:50:41 -04:00
|
|
|
before { SiteSetting.disable_system_edit_notifications = true }
|
2014-09-09 12:56:04 -04:00
|
|
|
|
|
|
|
it 'notifies a user of the revision made by another user' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2014-10-27 17:06:43 -04:00
|
|
|
post.revise(evil_trout, raw: "world is the new body of the message")
|
2014-12-31 09:55:03 -05:00
|
|
|
}.to change(post.user.notifications, :count).by(1)
|
2014-09-09 12:56:04 -04:00
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it 'does not notify a user of the revision made by the system user' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2014-10-27 17:06:43 -04:00
|
|
|
post.revise(Discourse.system_user, raw: "world is the new body of the message")
|
2014-12-31 09:55:03 -05:00
|
|
|
}.not_to change(post.user.notifications, :count)
|
2014-09-09 12:56:04 -04:00
|
|
|
end
|
2014-06-11 11:14:00 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when category edit notifications are disabled" do
|
2021-10-18 09:04:01 -04:00
|
|
|
it 'notifies a user of the revision made by another user' do
|
|
|
|
SiteSetting.disable_category_edit_notifications = false
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, category_id: Fabricate(:category).id)
|
|
|
|
}.to change(post.user.notifications, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not notify a user of the revision made by the system user' do
|
|
|
|
SiteSetting.disable_category_edit_notifications = true
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, category_id: Fabricate(:category).id)
|
|
|
|
}.not_to change(post.user.notifications, :count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when tags edit notifications are disabled" do
|
2021-11-02 13:53:21 -04:00
|
|
|
it 'notifies a user of the revision made by another user' do
|
|
|
|
SiteSetting.disable_tags_edit_notifications = false
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, tags: [Fabricate(:tag).name])
|
|
|
|
}.to change(post.user.notifications, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not notify a user of the revision made by the system user' do
|
|
|
|
SiteSetting.disable_tags_edit_notifications = true
|
|
|
|
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, tags: [Fabricate(:tag).name])
|
|
|
|
}.not_to change(post.user.notifications, :count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-04 05:57:33 -04:00
|
|
|
context 'when using plugin API to add custom recipients' do
|
|
|
|
let(:lurker) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
plugin = Plugin::Instance.new
|
|
|
|
plugin.add_post_revision_notifier_recipients do |post_revision|
|
|
|
|
[lurker.id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-31 00:55:42 -04:00
|
|
|
after do
|
|
|
|
PostActionNotifier.reset!
|
|
|
|
end
|
|
|
|
|
2020-08-04 05:57:33 -04:00
|
|
|
it 'notifies the specified user of the revision' do
|
|
|
|
expect {
|
|
|
|
post.revise(evil_trout, raw: "world is the new body of the message")
|
|
|
|
}.to change {
|
|
|
|
lurker.notifications.count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with private message' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:mention_post) { Fabricate(:post, user: user, raw: 'Hello @eviltrout') }
|
2014-03-06 08:34:14 -05:00
|
|
|
let(:topic) do
|
|
|
|
topic = mention_post.topic
|
2014-09-11 03:39:20 -04:00
|
|
|
topic.update_columns archetype: Archetype.private_message, category_id: nil
|
2014-03-06 08:34:14 -05:00
|
|
|
topic
|
|
|
|
end
|
2013-02-19 12:59:46 -05:00
|
|
|
|
|
|
|
it "won't notify someone who can't see the post" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2013-02-19 12:59:46 -05:00
|
|
|
Guardian.any_instance.expects(:can_see?).with(instance_of(Post)).returns(false)
|
|
|
|
mention_post
|
2015-11-30 01:03:47 -05:00
|
|
|
PostAlerter.post_created(mention_post)
|
2014-12-31 09:55:03 -05:00
|
|
|
}.not_to change(evil_trout.notifications, :count)
|
2013-02-19 12:59:46 -05:00
|
|
|
end
|
2014-03-06 08:34:14 -05:00
|
|
|
|
|
|
|
it 'creates like notifications' do
|
|
|
|
other_user = Fabricate(:user)
|
|
|
|
topic.allowed_users << user << other_user
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(other_user, mention_post)
|
2014-12-31 09:55:03 -05:00
|
|
|
}.to change(user.notifications, :count)
|
2014-03-06 08:34:14 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with moderator action post' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:first_post) { Fabricate(:post, user: user, raw: 'A useless post for you.') }
|
2013-05-23 11:42:41 -04:00
|
|
|
let(:topic) { first_post.topic }
|
|
|
|
|
|
|
|
it 'should not notify anyone' do
|
|
|
|
expect {
|
|
|
|
Fabricate(:post, topic: topic, raw: 'This topic is CLOSED', post_type: Post.types[:moderator_action])
|
|
|
|
}.to_not change { Notification.count }
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|