2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe PostAction do
|
2014-12-31 22:55:03 +08:00
|
|
|
it { is_expected.to rate_limit }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2024-01-25 14:28:26 +08:00
|
|
|
fab!(:moderator) { Fabricate(:moderator, refresh_auto_groups: true) }
|
2023-12-13 17:18:42 +08:00
|
|
|
fab!(:codinghorror) { Fabricate(:coding_horror, refresh_auto_groups: true) }
|
|
|
|
fab!(:eviltrout) { Fabricate(:evil_trout, refresh_auto_groups: true) }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:post)
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:second_post) { Fabricate(:post, topic: post.topic) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-03-18 23:17:51 +08:00
|
|
|
def value_for(user_id, dt)
|
|
|
|
GivenDailyLike.find_for(user_id, dt).pluck(:likes_given)[0] || 0
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "disallows the same action from happening twice" do
|
|
|
|
PostAction.create(user: eviltrout, post: post, post_action_type_id: PostActionType.types[:like])
|
|
|
|
pa =
|
|
|
|
PostAction.new(user: eviltrout, post: post, post_action_type_id: PostActionType.types[:like])
|
|
|
|
expect(pa).not_to be_valid
|
2016-03-06 06:51:30 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "messaging" do
|
2019-01-04 01:03:01 +08:00
|
|
|
it "notifies moderators (integration test)" do
|
2017-10-24 05:19:30 +08:00
|
|
|
post = create_post
|
|
|
|
mod = moderator
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result =
|
|
|
|
PostActionCreator.notify_moderators(codinghorror, post, "this is my special long message")
|
2017-10-24 05:19:30 +08:00
|
|
|
|
|
|
|
posts =
|
|
|
|
Post
|
|
|
|
.joins(:topic)
|
|
|
|
.select("posts.id, topics.subtype, posts.topic_id")
|
|
|
|
.where("topics.archetype" => Archetype.private_message)
|
|
|
|
.to_a
|
|
|
|
|
|
|
|
expect(posts.count).to eq(1)
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(result.post_action.related_post_id).to eq(posts[0].id.to_i)
|
|
|
|
expect(result.reviewable_score.meta_topic_id).to eq(posts[0].topic_id)
|
2017-10-24 05:19:30 +08:00
|
|
|
expect(posts[0].subtype).to eq(TopicSubtype.notify_moderators)
|
|
|
|
|
|
|
|
topic = posts[0].topic
|
|
|
|
|
|
|
|
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
|
|
|
|
topic_user_ids = topic.reload.topic_users.map { |x| x.user_id }
|
|
|
|
expect(topic_user_ids).to include(codinghorror.id)
|
|
|
|
expect(topic_user_ids).to include(mod.id)
|
|
|
|
|
2023-02-13 12:39:45 +08:00
|
|
|
expect(topic.topic_users.where(user_id: mod.id).pick(:notification_level)).to eq(
|
2019-10-21 18:32:27 +08:00
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
2017-10-24 05:19:30 +08:00
|
|
|
|
2023-02-13 12:39:45 +08:00
|
|
|
expect(topic.topic_users.where(user_id: codinghorror.id).pick(:notification_level)).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
2017-10-24 05:19:30 +08:00
|
|
|
|
|
|
|
# reply to PM should not clear flag
|
|
|
|
PostCreator.new(
|
|
|
|
mod,
|
|
|
|
topic_id: posts[0].topic_id,
|
|
|
|
raw: "This is my test reply to the user, it should clear flags",
|
|
|
|
).create
|
2019-01-04 01:03:01 +08:00
|
|
|
result.post_action.reload
|
|
|
|
expect(result.post_action.deleted_at).to eq(nil)
|
2017-10-24 05:19:30 +08:00
|
|
|
|
|
|
|
# Acting on the flag should not post an automated status message (since a moderator already replied)
|
|
|
|
expect(topic.posts.count).to eq(2)
|
2018-11-01 03:35:07 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
2017-10-24 05:19:30 +08:00
|
|
|
topic.reload
|
|
|
|
expect(topic.posts.count).to eq(2)
|
|
|
|
|
|
|
|
# Clearing the flags should not post an automated status message
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.notify_moderators(mod, post, "another special message")
|
|
|
|
result.reviewable.perform(admin, :disagree)
|
2017-10-24 05:19:30 +08:00
|
|
|
topic.reload
|
|
|
|
expect(topic.posts.count).to eq(2)
|
|
|
|
|
|
|
|
# Acting on the flag should post an automated status message
|
|
|
|
another_post = create_post
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.notify_moderators(codinghorror, another_post, "foobar")
|
|
|
|
topic = result.post_action.related_post.topic
|
2017-10-24 05:19:30 +08:00
|
|
|
|
|
|
|
expect(topic.posts.count).to eq(1)
|
2019-01-04 01:03:01 +08:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
2018-11-01 03:35:07 +08:00
|
|
|
|
2017-10-24 05:19:30 +08:00
|
|
|
topic.reload
|
|
|
|
expect(topic.posts.count).to eq(2)
|
|
|
|
expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
|
2021-04-22 18:23:44 +08:00
|
|
|
expect(topic.message_archived?(mod)).to eq(true)
|
2013-04-22 15:45:03 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with category group moderators" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group_user)
|
2021-08-12 06:11:22 +08:00
|
|
|
let(:group) { group_user.group }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
group.update!(messageable_level: Group::ALIAS_LEVELS[:nobody])
|
2024-09-04 09:38:46 +08:00
|
|
|
Fabricate(:category_moderation_group, category: post.topic.category, group:)
|
2021-08-12 06:11:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies via pm" do
|
|
|
|
result =
|
|
|
|
PostActionCreator.notify_moderators(codinghorror, post, "this is my special long message")
|
|
|
|
|
|
|
|
readable_by_groups = result.reviewable_score.meta_topic.topic_allowed_groups.map(&:group_id)
|
|
|
|
expect(readable_by_groups).to include(group.id)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-29 04:08:31 +08:00
|
|
|
describe "update_counters" do
|
|
|
|
it "properly updates topic counters" do
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time Date.today
|
|
|
|
# we need this to test it
|
|
|
|
TopicUser.change(codinghorror, post.topic, posted: true)
|
2015-01-08 11:35:56 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
expect(value_for(moderator.id, Date.today)).to eq(0)
|
2014-08-19 22:14:17 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(moderator, post)
|
|
|
|
PostActionCreator.like(codinghorror, second_post)
|
2015-01-08 11:35:56 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
post.topic.reload
|
|
|
|
expect(post.topic.like_count).to eq(2)
|
2015-01-08 11:35:56 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
expect(value_for(moderator.id, Date.today)).to eq(1)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
tu = TopicUser.get(post.topic, codinghorror)
|
|
|
|
expect(tu.liked).to be true
|
2013-05-04 08:52:45 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2018-07-25 05:17:47 +08:00
|
|
|
describe "undo/redo repeatedly" do
|
|
|
|
it "doesn't create a second action for the same user/type" do
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
|
|
|
PostActionCreator.like(codinghorror, post)
|
2018-07-25 05:17:47 +08:00
|
|
|
expect(PostAction.where(post: post).with_deleted.count).to eq(1)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2018-07-25 05:17:47 +08:00
|
|
|
|
|
|
|
# Check that we don't lose consistency into negatives
|
|
|
|
expect(post.reload.like_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe "when a user likes something" do
|
2019-01-14 11:43:09 +08:00
|
|
|
before { PostActionNotifier.enable }
|
2015-03-26 09:08:04 +08:00
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
it "should generate and remove notifications correctly" do
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
expect(Notification.count).to eq(1)
|
2016-12-22 13:46:22 +08:00
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
notification = Notification.last
|
2016-12-22 13:46:22 +08:00
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
expect(notification.user_id).to eq(post.user_id)
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2015-03-26 09:08:04 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
expect(Notification.count).to eq(0)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
expect(Notification.count).to eq(1)
|
|
|
|
|
|
|
|
notification = Notification.last
|
|
|
|
|
|
|
|
expect(notification.user_id).to eq(post.user_id)
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not notify when never is selected" do
|
|
|
|
post.user.user_option.update!(
|
|
|
|
like_notification_frequency: UserOption.like_notification_frequency_type[:never],
|
|
|
|
)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(codinghorror, post) end.to_not change { Notification.count }
|
2019-01-17 11:08:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies on likes correctly" do
|
2019-01-04 01:03:01 +08:00
|
|
|
SiteSetting.post_undo_action_window_mins = 120
|
|
|
|
PostActionCreator.like(eviltrout, post)
|
|
|
|
PostActionCreator.like(admin, post)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
# one like
|
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(1)
|
|
|
|
|
|
|
|
post.user.user_option.update!(
|
|
|
|
like_notification_frequency: UserOption.like_notification_frequency_type[:always],
|
|
|
|
)
|
|
|
|
|
|
|
|
admin2 = Fabricate(:admin)
|
|
|
|
|
|
|
|
# Travel 1 hour in time to test that order post_actions by `created_at`
|
|
|
|
freeze_time 1.hour.from_now
|
|
|
|
|
|
|
|
expect do PostActionCreator.like(admin2, post) end.to_not change { Notification.count }
|
|
|
|
|
|
|
|
# adds info to the notification
|
|
|
|
notification = Notification.find_by(post_number: 1, topic_id: post.topic_id)
|
|
|
|
|
|
|
|
expect(notification.data_hash["count"].to_i).to eq(2)
|
|
|
|
expect(notification.data_hash["username2"]).to eq(eviltrout.username)
|
|
|
|
|
|
|
|
# this is a tricky thing ... removing a like should fix up the notifications
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(eviltrout, post, :like)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
# rebuilds the missing notification
|
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(1)
|
|
|
|
|
|
|
|
notification = Notification.find_by(post_number: 1, topic_id: post.topic_id)
|
|
|
|
|
|
|
|
expect(notification.data_hash["count"]).to eq(2)
|
|
|
|
expect(notification.data_hash["username"]).to eq(admin2.username)
|
|
|
|
expect(notification.data_hash["username2"]).to eq(admin.username)
|
|
|
|
|
|
|
|
post.user.user_option.update!(
|
|
|
|
like_notification_frequency:
|
|
|
|
UserOption.like_notification_frequency_type[:first_time_and_daily],
|
|
|
|
)
|
|
|
|
|
|
|
|
# this gets skipped
|
|
|
|
admin3 = Fabricate(:admin)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(admin3, post)
|
2019-01-17 11:08:03 +08:00
|
|
|
|
|
|
|
freeze_time 2.days.from_now
|
|
|
|
|
|
|
|
admin4 = Fabricate(:admin)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(admin4, post)
|
2015-03-26 09:08:04 +08:00
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
# first happened within the same day, no need to notify
|
2019-01-17 11:08:03 +08:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(2)
|
2019-01-14 11:43:09 +08:00
|
|
|
end
|
|
|
|
|
2019-01-16 10:40:16 +08:00
|
|
|
describe "likes consolidation" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:liker) { Fabricate(:user) }
|
|
|
|
fab!(:liker2) { Fabricate(:user) }
|
|
|
|
fab!(:likee) { Fabricate(:user) }
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-16 16:17:04 +08:00
|
|
|
it "can be disabled" do
|
2019-12-05 17:06:06 +08:00
|
|
|
SiteSetting.notification_consolidation_threshold = 0
|
2019-01-16 16:17:04 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-16 16:17:04 +08:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}.by(1)
|
|
|
|
|
2019-12-05 17:06:06 +08:00
|
|
|
SiteSetting.notification_consolidation_threshold = 1
|
2019-01-16 16:17:04 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to_not change {
|
2019-01-16 16:17:04 +08:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}
|
2019-01-16 10:40:16 +08:00
|
|
|
end
|
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
describe "frequency first_time_and_daily" do
|
|
|
|
before do
|
|
|
|
likee.user_option.update!(
|
|
|
|
like_notification_frequency:
|
|
|
|
UserOption.like_notification_frequency_type[:first_time_and_daily],
|
|
|
|
)
|
|
|
|
end
|
2019-01-16 16:17:04 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
it "should consolidate likes notification when the threshold is reached" do
|
2019-12-05 17:06:06 +08:00
|
|
|
SiteSetting.notification_consolidation_threshold = 2
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect do
|
2019-01-04 01:03:01 +08:00
|
|
|
3.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 14:31:07 +08:00
|
|
|
end.to change { likee.reload.notifications.count }.by(1)
|
|
|
|
|
|
|
|
notification = likee.notifications.last
|
|
|
|
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked_consolidated])
|
|
|
|
|
|
|
|
data = JSON.parse(notification.data)
|
|
|
|
|
|
|
|
expect(data["username"]).to eq(liker.username)
|
|
|
|
expect(data["display_username"]).to eq(liker.username)
|
|
|
|
expect(data["count"]).to eq(3)
|
|
|
|
|
|
|
|
notification.update!(read: true)
|
|
|
|
|
|
|
|
expect do
|
2019-01-04 01:03:01 +08:00
|
|
|
2.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 14:31:07 +08:00
|
|
|
end.to_not change { likee.reload.notifications.count }
|
|
|
|
|
|
|
|
data = JSON.parse(notification.reload.data)
|
|
|
|
|
|
|
|
expect(notification.read).to eq(false)
|
|
|
|
expect(data["count"]).to eq(5)
|
|
|
|
|
|
|
|
# Like from a different user shouldn't be consolidated
|
|
|
|
expect do
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(Fabricate(:user), Fabricate(:post, user: likee))
|
2019-01-17 14:31:07 +08:00
|
|
|
end.to change { likee.reload.notifications.count }.by(1)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
notification = likee.notifications.last
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
freeze_time((SiteSetting.likes_notification_consolidation_window_mins.minutes + 1).since)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-17 14:31:07 +08:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}.by(1)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
notification = likee.notifications.last
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
|
|
|
end
|
|
|
|
end
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
describe "frequency always" do
|
|
|
|
before do
|
|
|
|
likee.user_option.update!(
|
|
|
|
like_notification_frequency: UserOption.like_notification_frequency_type[:always],
|
2019-01-16 15:01:13 +08:00
|
|
|
)
|
2019-01-17 14:31:07 +08:00
|
|
|
end
|
2019-01-16 15:01:13 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
it "should consolidate liked notifications when threshold is reached" do
|
2019-12-05 17:06:06 +08:00
|
|
|
SiteSetting.notification_consolidation_threshold = 2
|
2019-01-16 15:01:13 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
post = Fabricate(:post, user: likee)
|
2019-01-16 15:01:13 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect do
|
2019-01-04 01:03:01 +08:00
|
|
|
[liker2, liker].each { |user| PostActionCreator.like(user, post) }
|
2019-01-17 14:31:07 +08:00
|
|
|
end.to change { likee.reload.notifications.count }.by(1)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
notification = likee.notifications.last
|
|
|
|
data_hash = notification.data_hash
|
|
|
|
|
|
|
|
expect(data_hash["original_username"]).to eq(liker.username)
|
|
|
|
expect(data_hash["username2"]).to eq(liker2.username)
|
|
|
|
expect(data_hash["count"].to_i).to eq(2)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect do
|
2019-01-04 01:03:01 +08:00
|
|
|
2.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 14:31:07 +08:00
|
|
|
end.to change { likee.reload.notifications.count }.by(2)
|
2019-01-16 10:40:16 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
expect(likee.notifications.pluck(:notification_type).uniq).to contain_exactly(
|
|
|
|
Notification.types[:liked],
|
|
|
|
)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-17 14:31:07 +08:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}.by(-1)
|
|
|
|
|
|
|
|
notification = likee.notifications.last
|
|
|
|
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked_consolidated])
|
|
|
|
|
|
|
|
expect(notification.data_hash["count"].to_i).to eq(3)
|
|
|
|
expect(notification.data_hash["username"]).to eq(liker.username)
|
|
|
|
end
|
2019-01-16 10:40:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
it "should not generate a notification if liker has been muted" do
|
|
|
|
mutee = Fabricate(:user)
|
2015-03-26 09:08:04 +08:00
|
|
|
MutedUser.create!(user_id: post.user.id, muted_user_id: mutee.id)
|
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
expect do PostActionCreator.like(mutee, post) end.to_not change { Notification.count }
|
|
|
|
end
|
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
it "should not generate a notification if liker has the topic muted" do
|
|
|
|
post = Fabricate(:post, user: eviltrout)
|
|
|
|
|
|
|
|
TopicUser.create!(
|
|
|
|
topic: post.topic,
|
|
|
|
user: eviltrout,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted],
|
|
|
|
)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect do PostActionCreator.like(codinghorror, post) end.to_not change { Notification.count }
|
2019-01-17 14:31:07 +08:00
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
it "should generate a notification if liker is an admin irregardless of \
|
2019-01-14 11:43:09 +08:00
|
|
|
muting" do
|
2015-03-26 09:08:04 +08:00
|
|
|
MutedUser.create!(user_id: post.user.id, muted_user_id: admin.id)
|
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
expect do PostActionCreator.like(admin, post) end.to change { Notification.count }.by(1)
|
|
|
|
|
|
|
|
notification = Notification.last
|
2015-03-26 09:08:04 +08:00
|
|
|
|
2019-01-14 11:43:09 +08:00
|
|
|
expect(notification.user_id).to eq(post.user_id)
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2015-03-26 09:08:04 +08:00
|
|
|
end
|
|
|
|
|
2022-02-03 13:24:33 +08:00
|
|
|
it "should not increase topic like count when liking a whisper" do
|
2022-12-17 00:42:51 +08:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
2022-02-03 13:24:33 +08:00
|
|
|
post.revise(admin, post_type: Post.types[:whisper])
|
|
|
|
|
|
|
|
PostActionCreator.like(admin, post)
|
|
|
|
|
|
|
|
expect(post.reload.like_count).to eq(1)
|
|
|
|
expect(post.topic.like_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2013-05-28 00:45:10 +08:00
|
|
|
it "should increase the `like_count` and `like_score` when a user likes something" do
|
2017-07-24 21:17:42 +08:00
|
|
|
freeze_time Date.today
|
2013-05-28 00:45:10 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2017-07-24 21:17:42 +08:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(1)
|
|
|
|
expect(post.like_score).to eq(1)
|
|
|
|
post.topic.reload
|
|
|
|
expect(post.topic.like_count).to eq(1)
|
|
|
|
expect(value_for(codinghorror.id, Date.today)).to eq(1)
|
|
|
|
|
|
|
|
# When a staff member likes it
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(moderator, post)
|
2017-07-24 21:17:42 +08:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(2)
|
|
|
|
expect(post.like_score).to eq(4)
|
2019-01-17 11:08:03 +08:00
|
|
|
expect(post.topic.like_count).to eq(2)
|
2013-05-28 00:45:10 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
# Removing likes
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2017-07-24 21:17:42 +08:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(1)
|
|
|
|
expect(post.like_score).to eq(3)
|
2019-01-17 11:08:03 +08:00
|
|
|
expect(post.topic.like_count).to eq(1)
|
2017-07-24 21:17:42 +08:00
|
|
|
expect(value_for(codinghorror.id, Date.today)).to eq(0)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(moderator, post, :like)
|
2017-07-24 21:17:42 +08:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(0)
|
|
|
|
expect(post.like_score).to eq(0)
|
2019-01-17 11:08:03 +08:00
|
|
|
expect(post.topic.like_count).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2018-02-27 05:27:18 +08:00
|
|
|
|
|
|
|
it "shouldn't change given_likes unless likes are given or removed" do
|
2024-10-24 20:15:32 +08:00
|
|
|
freeze_time
|
2018-02-27 05:27:18 +08:00
|
|
|
|
2021-12-04 04:54:07 +08:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2018-02-27 05:27:18 +08:00
|
|
|
expect(value_for(codinghorror.id, Date.today)).to eq(1)
|
|
|
|
|
|
|
|
PostActionType.types.each do |type_name, type_id|
|
|
|
|
post = Fabricate(:post)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.create(codinghorror, post, type_name)
|
2018-02-27 05:27:18 +08:00
|
|
|
actual_count = value_for(codinghorror.id, Date.today)
|
|
|
|
expected_count = type_name == :like ? 2 : 1
|
|
|
|
expect(actual_count).to eq(expected_count),
|
|
|
|
"Expected likes_given to be #{expected_count} when adding '#{type_name}', but got #{actual_count}"
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.new(codinghorror, post, type_id).perform
|
2018-02-27 05:27:18 +08:00
|
|
|
actual_count = value_for(codinghorror.id, Date.today)
|
|
|
|
expect(actual_count).to eq(1),
|
|
|
|
"Expected likes_given to be 1 when removing '#{type_name}', but got #{actual_count}"
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "flagging" do
|
2023-12-13 17:18:42 +08:00
|
|
|
before { SiteSetting.flag_post_allowed_groups = "1|2|11" }
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "does not allow you to flag stuff twice, even if the reason is different" do
|
|
|
|
expect(PostActionCreator.spam(eviltrout, post)).to be_success
|
|
|
|
expect(PostActionCreator.off_topic(eviltrout, post)).to be_failed
|
2013-05-04 08:52:45 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "allows you to flag stuff again if your previous flag was removed" do
|
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionDestroyer.destroy(eviltrout, post, :spam)
|
|
|
|
expect(PostActionCreator.spam(eviltrout, post)).to be_success
|
2013-02-07 07:45:58 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should update counts when you clear flags" do
|
2019-01-04 01:03:01 +08:00
|
|
|
reviewable = PostActionCreator.spam(eviltrout, post).reviewable
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(post.reload.spam_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
reviewable.perform(Discourse.system_user, :disagree)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(post.reload.spam_count).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-03-20 21:38:23 +08:00
|
|
|
it "will not allow regular users to auto hide staff posts" do
|
2018-02-10 08:53:58 +08:00
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
post = Fabricate(:post, user: mod)
|
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 2.0)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2018-02-10 08:53:58 +08:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(Fabricate(:walter_white), post)
|
2018-02-10 08:53:58 +08:00
|
|
|
|
|
|
|
expect(post.hidden).to eq(false)
|
|
|
|
expect(post.hidden_at).to be_blank
|
|
|
|
end
|
|
|
|
|
2018-03-20 21:38:23 +08:00
|
|
|
it "allows staff users to auto hide staff posts" do
|
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
post = Fabricate(:post, user: mod)
|
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 8.0)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2018-03-20 21:38:23 +08:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(Fabricate(:admin), post)
|
2018-03-20 21:38:23 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
end
|
|
|
|
|
2019-09-18 21:51:07 +08:00
|
|
|
it "will not trigger auto hide on like" do
|
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
post = Fabricate(:post, user: mod)
|
|
|
|
|
|
|
|
result = PostActionCreator.spam(eviltrout, post)
|
|
|
|
result.reviewable.update!(score: 1000.0)
|
|
|
|
PostActionCreator.like(Fabricate(:admin), post)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.hidden).to eq(false)
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should follow the rules for automatic hiding workflow" do
|
2013-07-22 13:06:53 +08:00
|
|
|
post = create_post
|
2023-12-13 17:18:42 +08:00
|
|
|
walterwhite = Fabricate(:walter_white, refresh_auto_groups: true)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 3.0)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2014-08-19 22:14:17 +08:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(walterwhite, post)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-04-05 23:27:16 +08:00
|
|
|
job_args = Jobs::SendSystemMessage.jobs.last["args"].first
|
|
|
|
expect(job_args["user_id"]).to eq(post.user.id)
|
|
|
|
expect(job_args["message_type"]).to eq("post_hidden")
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flag_threshold_reached])
|
|
|
|
expect(post.topic.visible).to eq(false)
|
2024-04-29 08:34:46 +08:00
|
|
|
expect(post.topic.visibility_reason_id).to eq(
|
|
|
|
Topic.visibility_reasons[:op_flag_threshold_reached],
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it ")
|
2014-07-31 05:35:42 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(false)
|
2015-12-30 05:59:26 +08:00
|
|
|
expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flag_threshold_reached]) # keep most recent reason
|
|
|
|
expect(post.hidden_at).to be_present # keep the most recent hidden_at time
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.topic.visible).to eq(true)
|
2024-04-29 08:34:46 +08:00
|
|
|
expect(post.topic.visibility_reason_id).to eq(Topic.visibility_reasons[:op_unhidden])
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.off_topic(walterwhite, post)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-04-05 23:27:16 +08:00
|
|
|
job_args = Jobs::SendSystemMessage.jobs.last["args"].first
|
|
|
|
expect(job_args["user_id"]).to eq(post.user.id)
|
|
|
|
expect(job_args["message_type"]).to eq("post_hidden_again")
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flag_threshold_reached_again])
|
|
|
|
expect(post.topic.visible).to eq(false)
|
2024-04-29 08:34:46 +08:00
|
|
|
expect(post.topic.visibility_reason_id).to eq(
|
|
|
|
Topic.visibility_reasons[:op_flag_threshold_reached],
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it again ")
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flag_threshold_reached_again])
|
2024-04-29 08:34:46 +08:00
|
|
|
expect(post.topic.reload.visible).to eq(false)
|
|
|
|
expect(post.topic.visibility_reason_id).to eq(
|
|
|
|
Topic.visibility_reasons[:op_flag_threshold_reached],
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-02-19 04:18:31 +08:00
|
|
|
|
2018-08-30 08:03:32 +08:00
|
|
|
it "doesn't fail when post has nil user" do
|
|
|
|
post = create_post
|
|
|
|
post.update!(user: nil)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.new(moderator, post, PostActionType.types[:spam], take_action: true).perform
|
2018-08-30 08:03:32 +08:00
|
|
|
post.reload
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
end
|
2019-09-18 03:58:30 +08:00
|
|
|
it "hide tl0 posts that are flagged as spam by a tl3 user" do
|
2023-12-18 10:39:53 +08:00
|
|
|
newuser = Fabricate(:newuser, refresh_auto_groups: true)
|
2019-09-18 03:58:30 +08:00
|
|
|
post = create_post(user: newuser)
|
|
|
|
|
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2023-12-13 17:18:42 +08:00
|
|
|
PostActionCreator.spam(Fabricate(:leader, refresh_auto_groups: true), post)
|
2019-09-18 03:58:30 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flagged_by_tl3_user])
|
|
|
|
end
|
2018-10-10 23:50:00 +08:00
|
|
|
|
2014-02-19 04:18:31 +08:00
|
|
|
it "can flag the topic instead of a post" do
|
|
|
|
post1 = create_post
|
2019-01-04 01:03:01 +08:00
|
|
|
create_post(topic: post1.topic)
|
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
2023-12-13 17:18:42 +08:00
|
|
|
Fabricate(:user, refresh_auto_groups: true),
|
2019-01-04 01:03:01 +08:00
|
|
|
post1,
|
|
|
|
PostActionType.types[:spam],
|
|
|
|
flag_topic: true,
|
|
|
|
).perform
|
|
|
|
expect(result.post_action.targets_topic).to eq(true)
|
|
|
|
expect(result.reviewable.payload["targets_topic"]).to eq(true)
|
2014-02-19 04:18:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will flag the first post if you flag a topic but there is only one post in the topic" do
|
|
|
|
post = create_post
|
2019-01-04 01:03:01 +08:00
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
2023-12-13 17:18:42 +08:00
|
|
|
Fabricate(:user, refresh_auto_groups: true),
|
2019-01-04 01:03:01 +08:00
|
|
|
post,
|
|
|
|
PostActionType.types[:spam],
|
|
|
|
flag_topic: true,
|
|
|
|
).perform
|
|
|
|
expect(result.post_action.targets_topic).to eq(false)
|
|
|
|
expect(result.post_action.post_id).to eq(post.id)
|
|
|
|
expect(result.reviewable.payload["targets_topic"]).to eq(false)
|
2014-02-19 04:18:31 +08:00
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
it "will unhide the post when a moderator undoes the flag on which s/he took action" do
|
2014-08-19 22:14:17 +08:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
|
|
|
post = create_post
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.new(moderator, post, PostActionType.types[:spam], take_action: true).perform
|
2014-08-19 22:14:17 +08:00
|
|
|
|
|
|
|
post.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(true)
|
2014-08-19 22:14:17 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionDestroyer.destroy(moderator, post, :spam)
|
2014-08-19 22:14:17 +08:00
|
|
|
|
|
|
|
post.reload
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(post.hidden).to eq(false)
|
2014-08-19 22:14:17 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with topic auto closing" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:topic)
|
2019-01-08 18:43:10 +08:00
|
|
|
let(:post1) { create_post(topic: topic) }
|
|
|
|
let(:post2) { create_post(topic: topic) }
|
|
|
|
let(:post3) { create_post(topic: topic) }
|
2014-12-06 02:37:43 +08:00
|
|
|
|
2023-12-13 17:18:42 +08:00
|
|
|
fab!(:flagger1) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
fab!(:flagger2) { Fabricate(:user, refresh_auto_groups: true) }
|
2014-12-06 02:37:43 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
before do
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:disabled]
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 4.5)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2019-01-08 18:43:10 +08:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 2
|
|
|
|
SiteSetting.num_hours_to_close_topic = 1
|
2014-12-06 02:37:43 +08:00
|
|
|
end
|
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
it "will automatically pause a topic due to large community flagging" do
|
2020-02-06 22:57:52 +08:00
|
|
|
freeze_time
|
2019-07-09 17:45:11 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
# reaching `num_flaggers_to_close_topic` isn't enough
|
2019-01-04 01:03:01 +08:00
|
|
|
[flagger1, flagger2].each { |flagger| PostActionCreator.inappropriate(flagger, post1) }
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
2014-12-06 02:37:43 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
# clean up
|
|
|
|
PostAction.where(post: post1).delete_all
|
2014-12-06 02:37:43 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
# reaching `num_flags_to_close_topic` isn't enough
|
2019-01-04 01:03:01 +08:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger1, post) }
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
|
|
|
|
# clean up
|
|
|
|
PostAction.where(post: [post1, post2, post3]).delete_all
|
|
|
|
|
|
|
|
# reaching both should close the topic
|
|
|
|
[flagger1, flagger2].each do |flagger|
|
2019-01-04 01:03:01 +08:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2014-12-06 02:37:43 +08:00
|
|
|
end
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
|
|
|
|
topic_status_update = TopicTimer.last
|
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
2020-03-11 05:13:17 +08:00
|
|
|
expect(topic_status_update.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 18:43:10 +08:00
|
|
|
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
|
2014-12-06 02:37:43 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when on a staff post" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:staff_user) { Fabricate(:user, moderator: true) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, user: staff_user) }
|
2019-01-22 07:50:53 +08:00
|
|
|
|
|
|
|
it "will not close topics opened by staff" do
|
|
|
|
[flagger1, flagger2].each do |flagger|
|
2019-01-04 01:03:01 +08:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2019-01-22 07:50:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
it "will keep the topic in closed status until the community flags are handled" do
|
|
|
|
freeze_time
|
2015-05-13 14:45:59 +08:00
|
|
|
|
2019-02-12 04:41:03 +08:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 1
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 0.5)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2017-03-31 14:35:05 +08:00
|
|
|
|
2019-02-12 04:41:03 +08:00
|
|
|
post = Fabricate(:post, topic: topic)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(flagger1, post)
|
2019-01-08 18:43:10 +08:00
|
|
|
expect(topic.reload.closed).to eq(true)
|
2019-01-08 20:07:43 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
timer = TopicTimer.last
|
2019-03-28 14:28:01 +08:00
|
|
|
expect(timer.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
freeze_time timer.execute_at
|
2020-07-24 17:16:52 +08:00
|
|
|
|
2021-01-19 11:30:58 +08:00
|
|
|
Jobs::OpenTopic.new.execute(topic_timer_id: timer.id)
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
2020-03-11 05:13:17 +08:00
|
|
|
expect(timer.reload.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 18:43:10 +08:00
|
|
|
|
|
|
|
freeze_time timer.execute_at
|
2019-02-12 04:41:03 +08:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 10
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 10.0)
|
2021-12-09 01:12:24 +08:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2019-02-12 04:41:03 +08:00
|
|
|
|
2019-01-08 18:43:10 +08:00
|
|
|
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: timer.id, state: false)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will reopen topic after the flags are auto handled" do
|
|
|
|
freeze_time
|
|
|
|
[flagger1, flagger2].each do |flagger|
|
2019-01-04 01:03:01 +08:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2019-01-08 18:43:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
|
|
|
|
freeze_time 61.days.from_now
|
|
|
|
Jobs::AutoQueueHandler.new.execute({})
|
|
|
|
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: TopicTimer.last.id, state: false)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
2014-12-06 02:37:43 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2024-02-07 10:12:22 +08:00
|
|
|
# flags are already being tested
|
|
|
|
all_types_except_flags =
|
2024-07-18 08:10:22 +08:00
|
|
|
PostActionType.types.except(*PostActionType.flag_types_without_additional_message.keys)
|
2024-02-07 10:12:22 +08:00
|
|
|
all_types_except_flags.values.each do |action|
|
|
|
|
it "prevents user to act twice at the same time" do
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(PostActionCreator.new(eviltrout, post, action).perform).to be_success
|
|
|
|
expect(PostActionCreator.new(eviltrout, post, action).perform).to be_failed
|
2013-05-04 08:52:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
describe "messages" do
|
2015-02-06 02:58:49 +08:00
|
|
|
it "does not create a message when there is no message" do
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.spam(Discourse.system_user, post)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post_action.related_post_id).to be_nil
|
|
|
|
expect(result.reviewable_score.meta_topic_id).to be_nil
|
2015-02-06 02:58:49 +08:00
|
|
|
end
|
|
|
|
|
2024-07-18 08:10:22 +08:00
|
|
|
it "does not create a message for custom flag when message is not required" do
|
|
|
|
flag_without_message =
|
|
|
|
Fabricate(:flag, name: "flag without message", notify_type: true, require_message: false)
|
|
|
|
|
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
post,
|
2024-09-11 13:30:20 +08:00
|
|
|
PostActionType.types[:custom_flag_without_message],
|
2024-07-18 08:10:22 +08:00
|
|
|
message: "WAT",
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post_action.related_post_id).to be_nil
|
|
|
|
expect(result.reviewable_score.meta_topic_id).to be_nil
|
2024-10-09 07:19:31 +08:00
|
|
|
ensure
|
2024-07-18 08:10:22 +08:00
|
|
|
flag_without_message.destroy!
|
|
|
|
end
|
|
|
|
|
2015-02-06 02:58:49 +08:00
|
|
|
%i[notify_moderators notify_user spam].each do |post_action_type|
|
|
|
|
it "creates a message for #{post_action_type}" do
|
2019-01-04 01:03:01 +08:00
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
post,
|
|
|
|
PostActionType.types[post_action_type],
|
|
|
|
message: "WAT",
|
|
|
|
).perform
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post_action.related_post_id).to be_present
|
2015-02-06 02:58:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-18 08:10:22 +08:00
|
|
|
it "creates a message for custom flags when message is required" do
|
|
|
|
flag_with_message =
|
|
|
|
Fabricate(:flag, name: "flag with message", notify_type: true, require_message: true)
|
|
|
|
|
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
post,
|
2024-09-11 13:30:20 +08:00
|
|
|
PostActionType.types[:custom_flag_with_message],
|
2024-07-18 08:10:22 +08:00
|
|
|
message: "WAT",
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post_action.related_post_id).to be_present
|
|
|
|
expect(result.reviewable_score.meta_topic_id).to be_present
|
|
|
|
|
|
|
|
flag_with_message.destroy!
|
|
|
|
end
|
|
|
|
|
2017-12-19 10:58:26 +08:00
|
|
|
it "should raise the right errors when it fails to create a post" do
|
2021-08-12 06:11:22 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
UserSilencer.new(user, Discourse.system_user).silence
|
2017-12-19 10:58:26 +08:00
|
|
|
|
2021-08-12 06:11:22 +08:00
|
|
|
result = PostActionCreator.notify_moderators(user, post, "testing")
|
|
|
|
expect(result).to be_failed
|
2017-12-19 10:58:26 +08:00
|
|
|
end
|
|
|
|
|
2018-05-30 00:21:47 +08:00
|
|
|
it "should succeed even with low max title length" do
|
|
|
|
SiteSetting.max_topic_title_length = 50
|
|
|
|
post.topic.title = "This is a test topic " * 2
|
|
|
|
post.topic.save!
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
result = PostActionCreator.notify_moderators(Discourse.system_user, post, "WAT")
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post_action.related_post_id).to be_present
|
2018-05-30 00:21:47 +08:00
|
|
|
end
|
2015-02-06 02:58:49 +08:00
|
|
|
end
|
|
|
|
|
2015-04-16 15:29:18 +08:00
|
|
|
describe ".lookup_for" do
|
|
|
|
it "returns the correct map" do
|
|
|
|
user = Fabricate(:user)
|
2022-05-10 08:42:18 +08:00
|
|
|
post_action = PostActionCreator.create(user, post, :like).post_action
|
2015-04-16 15:29:18 +08:00
|
|
|
map = PostAction.lookup_for(user, [post.topic], post_action.post_action_type_id)
|
|
|
|
|
|
|
|
expect(map).to eq(post.topic_id => [post.post_number])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 17:17:54 +08:00
|
|
|
describe "#add_moderator_post_if_needed" do
|
2015-03-12 02:29:09 +08:00
|
|
|
it "should not add a moderator post when it's disabled" do
|
2015-03-12 02:07:17 +08:00
|
|
|
post = create_post
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.create(moderator, post, :spam, message: "WAT")
|
|
|
|
topic = result.post_action.related_post.topic
|
2015-03-12 02:29:09 +08:00
|
|
|
expect(topic.posts.count).to eq(1)
|
|
|
|
|
2017-08-09 17:17:54 +08:00
|
|
|
SiteSetting.auto_respond_to_flag_actions = false
|
2019-01-04 01:03:01 +08:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
|
|
|
expect(topic.reload.posts.count).to eq(1)
|
2015-03-12 02:29:09 +08:00
|
|
|
end
|
2015-10-15 08:56:10 +08:00
|
|
|
|
|
|
|
it "should create a notification in the related topic" do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2023-12-13 17:18:42 +08:00
|
|
|
user = Fabricate(:user, refresh_auto_groups: true)
|
2021-10-19 19:42:29 +08:00
|
|
|
stub_image_size
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.create(user, post, :spam, message: "WAT")
|
|
|
|
topic = result.post_action.related_post.topic
|
|
|
|
reviewable = result.reviewable
|
2015-10-15 08:56:10 +08:00
|
|
|
expect(user.notifications.count).to eq(0)
|
|
|
|
|
2017-08-09 17:17:54 +08:00
|
|
|
SiteSetting.auto_respond_to_flag_actions = true
|
2019-01-04 01:03:01 +08:00
|
|
|
reviewable.perform(admin, :agree_and_keep)
|
2015-10-15 08:56:10 +08:00
|
|
|
|
|
|
|
user_notifications = user.notifications
|
|
|
|
expect(user_notifications.last.topic).to eq(topic)
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
skip "should not add a moderator post when post is flagged via private message" do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-07-19 05:18:14 +08:00
|
|
|
user = Fabricate(:user)
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.create(user, post, :notify_user, message: "WAT")
|
|
|
|
action = result.post_action
|
2018-11-01 03:35:07 +08:00
|
|
|
action.reload.related_post.topic
|
2018-07-19 05:18:14 +08:00
|
|
|
expect(user.notifications.count).to eq(0)
|
|
|
|
|
|
|
|
SiteSetting.auto_respond_to_flag_actions = true
|
2019-01-04 01:03:01 +08:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
|
|
|
expect(user.reload.user_stat.flags_agreed).to eq(0)
|
2018-07-19 05:18:14 +08:00
|
|
|
|
|
|
|
user_notifications = user.notifications
|
|
|
|
expect(user_notifications.count).to eq(0)
|
|
|
|
end
|
2015-04-16 07:44:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "rate limiting" do
|
2022-02-18 20:44:32 +08:00
|
|
|
def limiter(tl, type)
|
2015-04-16 07:44:30 +08:00
|
|
|
user = Fabricate.build(:user)
|
|
|
|
user.trust_level = tl
|
2022-02-18 20:44:32 +08:00
|
|
|
action = PostAction.new(user: user, post_action_type_id: PostActionType.types[type])
|
2015-04-16 07:44:30 +08:00
|
|
|
action.post_action_rate_limiter
|
|
|
|
end
|
|
|
|
|
2022-02-18 20:44:32 +08:00
|
|
|
it "should scale up likes limits depending on trust level" do
|
|
|
|
expect(limiter(0, :like).max).to eq SiteSetting.max_likes_per_day
|
|
|
|
expect(limiter(1, :like).max).to eq SiteSetting.max_likes_per_day
|
|
|
|
expect(limiter(2, :like).max).to eq (
|
|
|
|
SiteSetting.max_likes_per_day * SiteSetting.tl2_additional_likes_per_day_multiplier
|
|
|
|
).to_i
|
|
|
|
expect(limiter(3, :like).max).to eq (
|
|
|
|
SiteSetting.max_likes_per_day * SiteSetting.tl3_additional_likes_per_day_multiplier
|
|
|
|
).to_i
|
|
|
|
expect(limiter(4, :like).max).to eq (
|
|
|
|
SiteSetting.max_likes_per_day * SiteSetting.tl4_additional_likes_per_day_multiplier
|
|
|
|
).to_i
|
2015-04-16 07:44:30 +08:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = -1
|
2022-02-18 20:44:32 +08:00
|
|
|
expect(limiter(2, :like).max).to eq SiteSetting.max_likes_per_day
|
2015-04-16 07:44:30 +08:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = 0.8
|
2022-02-18 20:44:32 +08:00
|
|
|
expect(limiter(2, :like).max).to eq SiteSetting.max_likes_per_day
|
2015-04-16 07:44:30 +08:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = "bob"
|
2022-02-18 20:44:32 +08:00
|
|
|
expect(limiter(2, :like).max).to eq SiteSetting.max_likes_per_day
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should scale up flag limits depending on trust level" do
|
|
|
|
%i[off_topic inappropriate spam notify_moderators].each do |type|
|
|
|
|
SiteSetting.tl2_additional_flags_per_day_multiplier = 1.5
|
|
|
|
|
|
|
|
expect(limiter(0, type).max).to eq SiteSetting.max_flags_per_day
|
|
|
|
expect(limiter(1, type).max).to eq SiteSetting.max_flags_per_day
|
|
|
|
expect(limiter(2, type).max).to eq (
|
|
|
|
SiteSetting.max_flags_per_day * SiteSetting.tl2_additional_flags_per_day_multiplier
|
|
|
|
).to_i
|
|
|
|
expect(limiter(3, type).max).to eq (
|
|
|
|
SiteSetting.max_flags_per_day * SiteSetting.tl3_additional_flags_per_day_multiplier
|
|
|
|
).to_i
|
|
|
|
expect(limiter(4, type).max).to eq (
|
|
|
|
SiteSetting.max_flags_per_day * SiteSetting.tl4_additional_flags_per_day_multiplier
|
|
|
|
).to_i
|
|
|
|
|
|
|
|
SiteSetting.tl2_additional_flags_per_day_multiplier = -1
|
|
|
|
expect(limiter(2, type).max).to eq SiteSetting.max_flags_per_day
|
|
|
|
|
|
|
|
SiteSetting.tl2_additional_flags_per_day_multiplier = 0.8
|
|
|
|
expect(limiter(2, type).max).to eq SiteSetting.max_flags_per_day
|
|
|
|
|
|
|
|
SiteSetting.tl2_additional_flags_per_day_multiplier = "bob"
|
|
|
|
expect(limiter(2, type).max).to eq SiteSetting.max_flags_per_day
|
|
|
|
end
|
2015-04-16 07:44:30 +08:00
|
|
|
end
|
2015-03-12 02:07:17 +08:00
|
|
|
end
|
|
|
|
|
2018-02-28 11:22:51 +08:00
|
|
|
describe "#is_flag?" do
|
|
|
|
describe "when post action is a flag" do
|
|
|
|
it "should return true" do
|
|
|
|
PostActionType.notify_flag_types.each do |_type, id|
|
|
|
|
post_action = PostAction.new(user: codinghorror, post_action_type_id: id)
|
|
|
|
|
|
|
|
expect(post_action.is_flag?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when post action is not a flag" do
|
|
|
|
it "should return false" do
|
|
|
|
post_action = PostAction.new(user: codinghorror, post_action_type_id: 99)
|
|
|
|
|
|
|
|
expect(post_action.is_flag?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-21 17:29:19 +08:00
|
|
|
describe "triggers Discourse events" do
|
2019-01-04 01:03:01 +08:00
|
|
|
it "triggers a flag_created event" do
|
|
|
|
event = DiscourseEvent.track(:flag_created) { PostActionCreator.spam(eviltrout, post) }
|
|
|
|
expect(event).to be_present
|
2018-04-11 22:17:05 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when resolving flags" do
|
2019-01-04 01:03:01 +08:00
|
|
|
let(:result) { PostActionCreator.spam(eviltrout, post) }
|
|
|
|
let(:post_action) { result.post_action }
|
|
|
|
let(:reviewable) { result.reviewable }
|
|
|
|
|
|
|
|
it "creates events for agreed" do
|
|
|
|
events = DiscourseEvent.track_events { reviewable.perform(moderator, :agree_and_keep) }
|
2018-04-11 22:17:05 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
reviewed_event = events.find { |e| e[:event_name] == :flag_reviewed }
|
|
|
|
expect(reviewed_event).to be_present
|
|
|
|
|
|
|
|
event = events.find { |e| e[:event_name] == :flag_agreed }
|
|
|
|
expect(event).to be_present
|
|
|
|
expect(event[:params]).to eq([post_action])
|
2018-04-11 22:17:05 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "creates events for disagreed" do
|
|
|
|
events = DiscourseEvent.track_events { reviewable.perform(moderator, :disagree) }
|
|
|
|
|
|
|
|
reviewed_event = events.find { |e| e[:event_name] == :flag_reviewed }
|
|
|
|
expect(reviewed_event).to be_present
|
|
|
|
|
|
|
|
event = events.find { |e| e[:event_name] == :flag_disagreed }
|
|
|
|
expect(event).to be_present
|
|
|
|
expect(event[:params]).to eq([post_action])
|
2018-04-11 22:17:05 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "creates events for ignored" do
|
2023-03-02 23:40:53 +08:00
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events { reviewable.perform(moderator, :ignore_and_do_nothing) }
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
reviewed_event = events.find { |e| e[:event_name] == :flag_reviewed }
|
|
|
|
expect(reviewed_event).to be_present
|
|
|
|
|
|
|
|
event = events.find { |e| e[:event_name] == :flag_deferred }
|
|
|
|
expect(event).to be_present
|
|
|
|
expect(event[:params]).to eq([post_action])
|
2018-04-11 22:17:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-09-05 13:47:18 +08:00
|
|
|
|
|
|
|
describe "count_per_day_for_type" do
|
|
|
|
before { PostActionCreator.create(eviltrout, post, :like) }
|
|
|
|
|
|
|
|
it "returns the correct count" do
|
|
|
|
expect(PostAction.count_per_day_for_type(PostActionType.types[:like])).to eq(
|
|
|
|
Time.now.utc.to_date => 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct count when there are multiple actions" do
|
|
|
|
PostActionCreator.create(codinghorror, post, :like)
|
|
|
|
expect(PostAction.count_per_day_for_type(PostActionType.types[:like])).to eq(
|
|
|
|
Time.now.utc.to_date => 2,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct count when there are multiple types" do
|
|
|
|
PostActionCreator.create(eviltrout, post, :spam)
|
|
|
|
expect(PostAction.count_per_day_for_type(PostActionType.types[:spam])).to eq(
|
|
|
|
Time.now.utc.to_date => 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct count with group filter" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(codinghorror)
|
|
|
|
|
|
|
|
PostActionCreator.create(codinghorror, post, :like)
|
|
|
|
expect(
|
|
|
|
PostAction.count_per_day_for_type(PostActionType.types[:like], { group_ids: [group.id] }),
|
|
|
|
).to eq(Time.now.utc.to_date => 1)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|