2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostAction do
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to rate_limit }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:moderator)
|
2023-12-13 04:18:42 -05:00
|
|
|
fab!(:codinghorror) { Fabricate(:coding_horror, refresh_auto_groups: true) }
|
|
|
|
fab!(:eviltrout) { Fabricate(:evil_trout, refresh_auto_groups: true) }
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:post)
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:second_post) { Fabricate(:post, topic: post.topic) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-03-18 11:17:51 -04:00
|
|
|
def value_for(user_id, dt)
|
|
|
|
GivenDailyLike.find_for(user_id, dt).pluck(:likes_given)[0] || 0
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05: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-05 17:51:30 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "messaging" do
|
2019-01-03 12:03:01 -05:00
|
|
|
it "notifies moderators (integration test)" do
|
2017-10-23 17:19:30 -04:00
|
|
|
post = create_post
|
|
|
|
mod = moderator
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
result =
|
|
|
|
PostActionCreator.notify_moderators(codinghorror, post, "this is my special long message")
|
2017-10-23 17:19:30 -04: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-03 12:03:01 -05: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-23 17:19:30 -04: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-12 23:39:45 -05:00
|
|
|
expect(topic.topic_users.where(user_id: mod.id).pick(:notification_level)).to eq(
|
2019-10-21 06:32:27 -04:00
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
2017-10-23 17:19:30 -04:00
|
|
|
|
2023-02-12 23:39:45 -05:00
|
|
|
expect(topic.topic_users.where(user_id: codinghorror.id).pick(:notification_level)).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
2017-10-23 17:19:30 -04: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-03 12:03:01 -05:00
|
|
|
result.post_action.reload
|
|
|
|
expect(result.post_action.deleted_at).to eq(nil)
|
2017-10-23 17:19:30 -04: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-10-31 15:35:07 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
2017-10-23 17:19:30 -04:00
|
|
|
topic.reload
|
|
|
|
expect(topic.posts.count).to eq(2)
|
|
|
|
|
|
|
|
# Clearing the flags should not post an automated status message
|
2019-01-03 12:03:01 -05:00
|
|
|
result = PostActionCreator.notify_moderators(mod, post, "another special message")
|
|
|
|
result.reviewable.perform(admin, :disagree)
|
2017-10-23 17:19:30 -04: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-03 12:03:01 -05:00
|
|
|
result = PostActionCreator.notify_moderators(codinghorror, another_post, "foobar")
|
|
|
|
topic = result.post_action.related_post.topic
|
2017-10-23 17:19:30 -04:00
|
|
|
|
|
|
|
expect(topic.posts.count).to eq(1)
|
2019-01-03 12:03:01 -05:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
2018-10-31 15:35:07 -04:00
|
|
|
|
2017-10-23 17:19:30 -04: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 06:23:44 -04:00
|
|
|
expect(topic.message_archived?(mod)).to eq(true)
|
2013-04-22 03:45:03 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with category group moderators" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:group_user)
|
2021-08-11 18:11:22 -04:00
|
|
|
let(:group) { group_user.group }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
group.update!(messageable_level: Group::ALIAS_LEVELS[:nobody])
|
|
|
|
post.topic.category.update!(reviewable_by_group_id: group.id)
|
|
|
|
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-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-28 16:08:31 -04:00
|
|
|
describe "update_counters" do
|
|
|
|
it "properly updates topic counters" do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time Date.today
|
|
|
|
# we need this to test it
|
|
|
|
TopicUser.change(codinghorror, post.topic, posted: true)
|
2015-01-07 22:35:56 -05:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(value_for(moderator.id, Date.today)).to eq(0)
|
2014-08-19 10:14:17 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(moderator, post)
|
|
|
|
PostActionCreator.like(codinghorror, second_post)
|
2015-01-07 22:35:56 -05:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
post.topic.reload
|
|
|
|
expect(post.topic.like_count).to eq(2)
|
2015-01-07 22:35:56 -05:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(value_for(moderator.id, Date.today)).to eq(1)
|
2016-03-17 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
tu = TopicUser.get(post.topic, codinghorror)
|
|
|
|
expect(tu.liked).to be true
|
2013-05-03 20:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2018-07-24 17:17:47 -04:00
|
|
|
describe "undo/redo repeatedly" do
|
|
|
|
it "doesn't create a second action for the same user/type" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
|
|
|
PostActionCreator.like(codinghorror, post)
|
2018-07-24 17:17:47 -04:00
|
|
|
expect(PostAction.where(post: post).with_deleted.count).to eq(1)
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2018-07-24 17:17:47 -04:00
|
|
|
|
|
|
|
# Check that we don't lose consistency into negatives
|
|
|
|
expect(post.reload.like_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "when a user likes something" do
|
2019-01-13 22:43:09 -05:00
|
|
|
before { PostActionNotifier.enable }
|
2015-03-25 21:08:04 -04:00
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
it "should generate and remove notifications correctly" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2019-01-16 22:08:03 -05:00
|
|
|
|
|
|
|
expect(Notification.count).to eq(1)
|
2016-12-22 00:46:22 -05:00
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
notification = Notification.last
|
2016-12-22 00:46:22 -05:00
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
expect(notification.user_id).to eq(post.user_id)
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2015-03-25 21:08:04 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2019-01-16 22:08:03 -05:00
|
|
|
|
|
|
|
expect(Notification.count).to eq(0)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2019-01-16 22:08:03 -05: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-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(codinghorror, post) end.to_not change { Notification.count }
|
2019-01-16 22:08:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies on likes correctly" do
|
2019-01-03 12:03:01 -05:00
|
|
|
SiteSetting.post_undo_action_window_mins = 120
|
|
|
|
PostActionCreator.like(eviltrout, post)
|
|
|
|
PostActionCreator.like(admin, post)
|
2019-01-16 22:08:03 -05: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-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(eviltrout, post, :like)
|
2019-01-16 22:08:03 -05: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-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(admin3, post)
|
2019-01-16 22:08:03 -05:00
|
|
|
|
|
|
|
freeze_time 2.days.from_now
|
|
|
|
|
|
|
|
admin4 = Fabricate(:admin)
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(admin4, post)
|
2015-03-25 21:08:04 -04:00
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
# first happened within the same day, no need to notify
|
2019-01-16 22:08:03 -05:00
|
|
|
expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(2)
|
2019-01-13 22:43:09 -05:00
|
|
|
end
|
|
|
|
|
2019-01-15 21:40:16 -05:00
|
|
|
describe "likes consolidation" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:liker) { Fabricate(:user) }
|
|
|
|
fab!(:liker2) { Fabricate(:user) }
|
|
|
|
fab!(:likee) { Fabricate(:user) }
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-16 03:17:04 -05:00
|
|
|
it "can be disabled" do
|
2019-12-05 04:06:06 -05:00
|
|
|
SiteSetting.notification_consolidation_threshold = 0
|
2019-01-16 03:17:04 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-16 03:17:04 -05:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}.by(1)
|
|
|
|
|
2019-12-05 04:06:06 -05:00
|
|
|
SiteSetting.notification_consolidation_threshold = 1
|
2019-01-16 03:17:04 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to_not change {
|
2019-01-16 03:17:04 -05:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}
|
2019-01-15 21:40:16 -05:00
|
|
|
end
|
|
|
|
|
2019-01-17 01:31:07 -05: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 03:17:04 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
it "should consolidate likes notification when the threshold is reached" do
|
2019-12-05 04:06:06 -05:00
|
|
|
SiteSetting.notification_consolidation_threshold = 2
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect do
|
2019-01-03 12:03:01 -05:00
|
|
|
3.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 01:31:07 -05: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-03 12:03:01 -05:00
|
|
|
2.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 01:31:07 -05: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-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(Fabricate(:user), Fabricate(:post, user: likee))
|
2019-01-17 01:31:07 -05:00
|
|
|
end.to change { likee.reload.notifications.count }.by(1)
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
notification = likee.notifications.last
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
freeze_time((SiteSetting.likes_notification_consolidation_window_mins.minutes + 1).since)
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-17 01:31:07 -05:00
|
|
|
likee.reload.notifications.count
|
|
|
|
}.by(1)
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
notification = likee.notifications.last
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
describe "frequency always" do
|
|
|
|
before do
|
|
|
|
likee.user_option.update!(
|
|
|
|
like_notification_frequency: UserOption.like_notification_frequency_type[:always],
|
2019-01-16 02:01:13 -05:00
|
|
|
)
|
2019-01-17 01:31:07 -05:00
|
|
|
end
|
2019-01-16 02:01:13 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
it "should consolidate liked notifications when threshold is reached" do
|
2019-12-05 04:06:06 -05:00
|
|
|
SiteSetting.notification_consolidation_threshold = 2
|
2019-01-16 02:01:13 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
post = Fabricate(:post, user: likee)
|
2019-01-16 02:01:13 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect do
|
2019-01-03 12:03:01 -05:00
|
|
|
[liker2, liker].each { |user| PostActionCreator.like(user, post) }
|
2019-01-17 01:31:07 -05:00
|
|
|
end.to change { likee.reload.notifications.count }.by(1)
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05: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-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect do
|
2019-01-03 12:03:01 -05:00
|
|
|
2.times { PostActionCreator.like(liker, Fabricate(:post, user: likee)) }
|
2019-01-17 01:31:07 -05:00
|
|
|
end.to change { likee.reload.notifications.count }.by(2)
|
2019-01-15 21:40:16 -05:00
|
|
|
|
2019-01-17 01:31:07 -05:00
|
|
|
expect(likee.notifications.pluck(:notification_type).uniq).to contain_exactly(
|
|
|
|
Notification.types[:liked],
|
|
|
|
)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(liker, Fabricate(:post, user: likee)) end.to change {
|
2019-01-17 01:31:07 -05: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-15 21:40:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
it "should not generate a notification if liker has been muted" do
|
|
|
|
mutee = Fabricate(:user)
|
2015-03-25 21:08:04 -04:00
|
|
|
MutedUser.create!(user_id: post.user.id, muted_user_id: mutee.id)
|
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
expect do PostActionCreator.like(mutee, post) end.to_not change { Notification.count }
|
|
|
|
end
|
|
|
|
|
2019-01-17 01:31:07 -05: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-03 12:03:01 -05:00
|
|
|
expect do PostActionCreator.like(codinghorror, post) end.to_not change { Notification.count }
|
2019-01-17 01:31:07 -05:00
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it "should generate a notification if liker is an admin irregardless of \
|
2019-01-13 22:43:09 -05:00
|
|
|
muting" do
|
2015-03-25 21:08:04 -04:00
|
|
|
MutedUser.create!(user_id: post.user.id, muted_user_id: admin.id)
|
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
expect do PostActionCreator.like(admin, post) end.to change { Notification.count }.by(1)
|
|
|
|
|
|
|
|
notification = Notification.last
|
2015-03-25 21:08:04 -04:00
|
|
|
|
2019-01-13 22:43:09 -05:00
|
|
|
expect(notification.user_id).to eq(post.user_id)
|
|
|
|
expect(notification.notification_type).to eq(Notification.types[:liked])
|
2015-03-25 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2022-02-03 00:24:33 -05:00
|
|
|
it "should not increase topic like count when liking a whisper" do
|
2022-12-16 11:42:51 -05:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
2022-02-03 00:24:33 -05: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-27 12:45:10 -04:00
|
|
|
it "should increase the `like_count` and `like_score` when a user likes something" do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time Date.today
|
2013-05-27 12:45:10 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2017-07-24 09:17:42 -04: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-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(moderator, post)
|
2017-07-24 09:17:42 -04:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(2)
|
|
|
|
expect(post.like_score).to eq(4)
|
2019-01-16 22:08:03 -05:00
|
|
|
expect(post.topic.like_count).to eq(2)
|
2013-05-27 12:45:10 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
# Removing likes
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(codinghorror, post, :like)
|
2017-07-24 09:17:42 -04:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(1)
|
|
|
|
expect(post.like_score).to eq(3)
|
2019-01-16 22:08:03 -05:00
|
|
|
expect(post.topic.like_count).to eq(1)
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(value_for(codinghorror.id, Date.today)).to eq(0)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(moderator, post, :like)
|
2017-07-24 09:17:42 -04:00
|
|
|
post.reload
|
|
|
|
expect(post.like_count).to eq(0)
|
|
|
|
expect(post.like_score).to eq(0)
|
2019-01-16 22:08:03 -05:00
|
|
|
expect(post.topic.like_count).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2018-02-26 16:27:18 -05:00
|
|
|
|
|
|
|
it "shouldn't change given_likes unless likes are given or removed" do
|
|
|
|
freeze_time(Time.zone.now)
|
|
|
|
|
2021-12-03 15:54:07 -05:00
|
|
|
PostActionCreator.like(codinghorror, post)
|
2018-02-26 16:27:18 -05:00
|
|
|
expect(value_for(codinghorror.id, Date.today)).to eq(1)
|
|
|
|
|
|
|
|
PostActionType.types.each do |type_name, type_id|
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.create(codinghorror, post, type_name)
|
2018-02-26 16:27:18 -05: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-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.new(codinghorror, post, type_id).perform
|
2018-02-26 16:27:18 -05: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-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "flagging" do
|
2023-12-13 04:18:42 -05:00
|
|
|
before { SiteSetting.flag_post_allowed_groups = "1|2|11" }
|
|
|
|
|
2019-01-03 12:03:01 -05: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-03 20:52:45 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05: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-06 18:45:58 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should update counts when you clear flags" do
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable = PostActionCreator.spam(eviltrout, post).reviewable
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(post.reload.spam_count).to eq(1)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable.perform(Discourse.system_user, :disagree)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(post.reload.spam_count).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2018-03-20 09:38:23 -04:00
|
|
|
it "will not allow regular users to auto hide staff posts" do
|
2018-02-09 19:53:58 -05:00
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
post = Fabricate(:post, user: mod)
|
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 2.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2018-02-09 19:53:58 -05:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(Fabricate(:walter_white), post)
|
2018-02-09 19:53:58 -05:00
|
|
|
|
|
|
|
expect(post.hidden).to eq(false)
|
|
|
|
expect(post.hidden_at).to be_blank
|
|
|
|
end
|
|
|
|
|
2018-03-20 09:38:23 -04:00
|
|
|
it "allows staff users to auto hide staff posts" do
|
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
post = Fabricate(:post, user: mod)
|
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 8.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2018-03-20 09:38:23 -04:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(Fabricate(:admin), post)
|
2018-03-20 09:38:23 -04:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
expect(post.hidden_at).to be_present
|
|
|
|
end
|
|
|
|
|
2019-09-18 09:51:07 -04: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-25 11:42:20 -05:00
|
|
|
it "should follow the rules for automatic hiding workflow" do
|
2013-07-22 01:06:53 -04:00
|
|
|
post = create_post
|
2023-12-13 04:18:42 -05:00
|
|
|
walterwhite = Fabricate(:walter_white, refresh_auto_groups: true)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 3.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:low]
|
2014-08-19 10:14:17 -04:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.spam(walterwhite, post)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-04-05 11:27:16 -04: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-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 09:55:03 -05: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)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it ")
|
2014-07-30 17:35:42 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(post.hidden).to eq(false)
|
2015-12-29 16:59:26 -05: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 09:55:03 -05:00
|
|
|
expect(post.topic.visible).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(eviltrout, post)
|
|
|
|
PostActionCreator.off_topic(walterwhite, post)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-04-05 11:27:16 -04: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-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 09:55:03 -05: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)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
post.revise(post.user, raw: post.raw + " ha I edited it again ")
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
|
2014-12-31 09:55:03 -05: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)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-02-18 15:18:31 -05:00
|
|
|
|
2018-08-29 20:03:32 -04:00
|
|
|
it "doesn't fail when post has nil user" do
|
|
|
|
post = create_post
|
|
|
|
post.update!(user: nil)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.new(moderator, post, PostActionType.types[:spam], take_action: true).perform
|
2018-08-29 20:03:32 -04:00
|
|
|
post.reload
|
|
|
|
expect(post.hidden).to eq(true)
|
|
|
|
end
|
2019-09-17 15:58:30 -04:00
|
|
|
it "hide tl0 posts that are flagged as spam by a tl3 user" do
|
2023-12-17 21:39:53 -05:00
|
|
|
newuser = Fabricate(:newuser, refresh_auto_groups: true)
|
2019-09-17 15:58:30 -04:00
|
|
|
post = create_post(user: newuser)
|
|
|
|
|
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
2023-12-13 04:18:42 -05:00
|
|
|
PostActionCreator.spam(Fabricate(:leader, refresh_auto_groups: true), post)
|
2019-09-17 15:58:30 -04: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 11:50:00 -04:00
|
|
|
|
2014-02-18 15:18:31 -05:00
|
|
|
it "can flag the topic instead of a post" do
|
|
|
|
post1 = create_post
|
2019-01-03 12:03:01 -05:00
|
|
|
create_post(topic: post1.topic)
|
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
2023-12-13 04:18:42 -05:00
|
|
|
Fabricate(:user, refresh_auto_groups: true),
|
2019-01-03 12:03:01 -05: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-18 15:18:31 -05: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-03 12:03:01 -05:00
|
|
|
result =
|
|
|
|
PostActionCreator.new(
|
2023-12-13 04:18:42 -05:00
|
|
|
Fabricate(:user, refresh_auto_groups: true),
|
2019-01-03 12:03:01 -05: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-18 15:18:31 -05:00
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it "will unhide the post when a moderator undoes the flag on which s/he took action" do
|
2014-08-19 10:14:17 -04:00
|
|
|
Discourse.stubs(:site_contact_user).returns(admin)
|
|
|
|
|
|
|
|
post = create_post
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.new(moderator, post, PostActionType.types[:spam], take_action: true).perform
|
2014-08-19 10:14:17 -04:00
|
|
|
|
|
|
|
post.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(post.hidden).to eq(true)
|
2014-08-19 10:14:17 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionDestroyer.destroy(moderator, post, :spam)
|
2014-08-19 10:14:17 -04:00
|
|
|
|
|
|
|
post.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(post.hidden).to eq(false)
|
2014-08-19 10:14:17 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with topic auto closing" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:topic)
|
2019-01-08 05:43:10 -05:00
|
|
|
let(:post1) { create_post(topic: topic) }
|
|
|
|
let(:post2) { create_post(topic: topic) }
|
|
|
|
let(:post3) { create_post(topic: topic) }
|
2014-12-05 13:37:43 -05:00
|
|
|
|
2023-12-13 04:18:42 -05:00
|
|
|
fab!(:flagger1) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
fab!(:flagger2) { Fabricate(:user, refresh_auto_groups: true) }
|
2014-12-05 13:37:43 -05:00
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
before do
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:disabled]
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 4.5)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2019-01-08 05:43:10 -05:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 2
|
|
|
|
SiteSetting.num_hours_to_close_topic = 1
|
2014-12-05 13:37:43 -05:00
|
|
|
end
|
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
it "will automatically pause a topic due to large community flagging" do
|
2020-02-06 09:57:52 -05:00
|
|
|
freeze_time
|
2019-07-09 05:45:11 -04:00
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
# reaching `num_flaggers_to_close_topic` isn't enough
|
2019-01-03 12:03:01 -05:00
|
|
|
[flagger1, flagger2].each { |flagger| PostActionCreator.inappropriate(flagger, post1) }
|
2019-01-08 05:43:10 -05:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
2014-12-05 13:37:43 -05:00
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
# clean up
|
|
|
|
PostAction.where(post: post1).delete_all
|
2014-12-05 13:37:43 -05:00
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
# reaching `num_flags_to_close_topic` isn't enough
|
2019-01-03 12:03:01 -05:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger1, post) }
|
2019-01-08 05:43:10 -05: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-03 12:03:01 -05:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2014-12-05 13:37:43 -05:00
|
|
|
end
|
2019-01-08 05:43:10 -05:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
|
|
|
|
topic_status_update = TopicTimer.last
|
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(topic_status_update.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 05:43:10 -05:00
|
|
|
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
|
2014-12-05 13:37:43 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when on a staff post" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:staff_user) { Fabricate(:user, moderator: true) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, user: staff_user) }
|
2019-01-21 18:50:53 -05:00
|
|
|
|
|
|
|
it "will not close topics opened by staff" do
|
|
|
|
[flagger1, flagger2].each do |flagger|
|
2019-01-03 12:03:01 -05:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2019-01-21 18:50:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
it "will keep the topic in closed status until the community flags are handled" do
|
|
|
|
freeze_time
|
2015-05-13 02:45:59 -04:00
|
|
|
|
2019-02-11 15:41:03 -05:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 1
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 0.5)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2017-03-31 02:35:05 -04:00
|
|
|
|
2019-02-11 15:41:03 -05:00
|
|
|
post = Fabricate(:post, topic: topic)
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger1, post)
|
2019-01-08 05:43:10 -05:00
|
|
|
expect(topic.reload.closed).to eq(true)
|
2019-01-08 07:07:43 -05:00
|
|
|
|
2019-01-08 05:43:10 -05:00
|
|
|
timer = TopicTimer.last
|
2019-03-28 02:28:01 -04:00
|
|
|
expect(timer.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 05:43:10 -05:00
|
|
|
|
|
|
|
freeze_time timer.execute_at
|
2020-07-24 05:16:52 -04:00
|
|
|
|
2021-01-18 22:30:58 -05:00
|
|
|
Jobs::OpenTopic.new.execute(topic_timer_id: timer.id)
|
2019-01-08 05:43:10 -05:00
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(timer.reload.execute_at).to eq_time(1.hour.from_now)
|
2019-01-08 05:43:10 -05:00
|
|
|
|
|
|
|
freeze_time timer.execute_at
|
2019-02-11 15:41:03 -05:00
|
|
|
SiteSetting.num_flaggers_to_close_topic = 10
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 10.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.auto_close_topic_sensitivity = Reviewable.sensitivities[:low]
|
2019-02-11 15:41:03 -05:00
|
|
|
|
2019-01-08 05:43:10 -05: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-03 12:03:01 -05:00
|
|
|
[post1, post2, post3].each { |post| PostActionCreator.inappropriate(flagger, post) }
|
2019-01-08 05:43:10 -05: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-05 13:37:43 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
it "prevents user to act twice at the same time" do
|
2022-09-25 23:58:40 -04:00
|
|
|
Group.refresh_automatic_groups!
|
2013-05-03 20:52:45 -04:00
|
|
|
# flags are already being tested
|
2019-01-03 12:03:01 -05:00
|
|
|
all_types_except_flags =
|
|
|
|
PostActionType.types.except(*PostActionType.flag_types_without_custom.keys)
|
2013-05-03 20:52:45 -04:00
|
|
|
all_types_except_flags.values.each do |action|
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(PostActionCreator.new(eviltrout, post, action).perform).to be_success
|
|
|
|
expect(PostActionCreator.new(eviltrout, post, action).perform).to be_failed
|
2013-05-03 20:52:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
describe "messages" do
|
2015-02-05 13:58:49 -05:00
|
|
|
it "does not create a message when there is no message" do
|
2019-01-03 12:03:01 -05: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-05 13:58:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
%i[notify_moderators notify_user spam].each do |post_action_type|
|
|
|
|
it "creates a message for #{post_action_type}" do
|
2019-01-03 12:03:01 -05: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-05 13:58:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-18 21:58:26 -05:00
|
|
|
it "should raise the right errors when it fails to create a post" do
|
2021-08-11 18:11:22 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
UserSilencer.new(user, Discourse.system_user).silence
|
2017-12-18 21:58:26 -05:00
|
|
|
|
2021-08-11 18:11:22 -04:00
|
|
|
result = PostActionCreator.notify_moderators(user, post, "testing")
|
|
|
|
expect(result).to be_failed
|
2017-12-18 21:58:26 -05:00
|
|
|
end
|
|
|
|
|
2018-05-29 12:21:47 -04: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-03 12:03:01 -05: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-29 12:21:47 -04:00
|
|
|
end
|
2015-02-05 13:58:49 -05:00
|
|
|
end
|
|
|
|
|
2015-04-16 03:29:18 -04:00
|
|
|
describe ".lookup_for" do
|
|
|
|
it "returns the correct map" do
|
|
|
|
user = Fabricate(:user)
|
2022-05-09 20:42:18 -04:00
|
|
|
post_action = PostActionCreator.create(user, post, :like).post_action
|
2015-04-16 03:29:18 -04: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 05:17:54 -04:00
|
|
|
describe "#add_moderator_post_if_needed" do
|
2015-03-11 14:29:09 -04:00
|
|
|
it "should not add a moderator post when it's disabled" do
|
2015-03-11 14:07:17 -04:00
|
|
|
post = create_post
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
result = PostActionCreator.create(moderator, post, :spam, message: "WAT")
|
|
|
|
topic = result.post_action.related_post.topic
|
2015-03-11 14:29:09 -04:00
|
|
|
expect(topic.posts.count).to eq(1)
|
|
|
|
|
2017-08-09 05:17:54 -04:00
|
|
|
SiteSetting.auto_respond_to_flag_actions = false
|
2019-01-03 12:03:01 -05:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
|
|
|
expect(topic.reload.posts.count).to eq(1)
|
2015-03-11 14:29:09 -04:00
|
|
|
end
|
2015-10-14 20:56:10 -04:00
|
|
|
|
|
|
|
it "should create a notification in the related topic" do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2023-12-13 04:18:42 -05:00
|
|
|
user = Fabricate(:user, refresh_auto_groups: true)
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2019-01-03 12:03:01 -05:00
|
|
|
result = PostActionCreator.create(user, post, :spam, message: "WAT")
|
|
|
|
topic = result.post_action.related_post.topic
|
|
|
|
reviewable = result.reviewable
|
2015-10-14 20:56:10 -04:00
|
|
|
expect(user.notifications.count).to eq(0)
|
|
|
|
|
2017-08-09 05:17:54 -04:00
|
|
|
SiteSetting.auto_respond_to_flag_actions = true
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable.perform(admin, :agree_and_keep)
|
2015-10-14 20:56:10 -04:00
|
|
|
|
|
|
|
user_notifications = user.notifications
|
|
|
|
expect(user_notifications.last.topic).to eq(topic)
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
skip "should not add a moderator post when post is flagged via private message" do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-07-18 17:18:14 -04:00
|
|
|
user = Fabricate(:user)
|
2019-01-03 12:03:01 -05:00
|
|
|
result = PostActionCreator.create(user, post, :notify_user, message: "WAT")
|
|
|
|
action = result.post_action
|
2018-10-31 15:35:07 -04:00
|
|
|
action.reload.related_post.topic
|
2018-07-18 17:18:14 -04:00
|
|
|
expect(user.notifications.count).to eq(0)
|
|
|
|
|
|
|
|
SiteSetting.auto_respond_to_flag_actions = true
|
2019-01-03 12:03:01 -05:00
|
|
|
result.reviewable.perform(admin, :agree_and_keep)
|
|
|
|
expect(user.reload.user_stat.flags_agreed).to eq(0)
|
2018-07-18 17:18:14 -04:00
|
|
|
|
|
|
|
user_notifications = user.notifications
|
|
|
|
expect(user_notifications.count).to eq(0)
|
|
|
|
end
|
2015-04-15 19:44:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "rate limiting" do
|
2022-02-18 07:44:32 -05:00
|
|
|
def limiter(tl, type)
|
2015-04-15 19:44:30 -04:00
|
|
|
user = Fabricate.build(:user)
|
|
|
|
user.trust_level = tl
|
2022-02-18 07:44:32 -05:00
|
|
|
action = PostAction.new(user: user, post_action_type_id: PostActionType.types[type])
|
2015-04-15 19:44:30 -04:00
|
|
|
action.post_action_rate_limiter
|
|
|
|
end
|
|
|
|
|
2022-02-18 07:44:32 -05: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-15 19:44:30 -04:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = -1
|
2022-02-18 07:44:32 -05:00
|
|
|
expect(limiter(2, :like).max).to eq SiteSetting.max_likes_per_day
|
2015-04-15 19:44:30 -04:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = 0.8
|
2022-02-18 07:44:32 -05:00
|
|
|
expect(limiter(2, :like).max).to eq SiteSetting.max_likes_per_day
|
2015-04-15 19:44:30 -04:00
|
|
|
|
|
|
|
SiteSetting.tl2_additional_likes_per_day_multiplier = "bob"
|
2022-02-18 07:44:32 -05: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-15 19:44:30 -04:00
|
|
|
end
|
2015-03-11 14:07:17 -04:00
|
|
|
end
|
|
|
|
|
2018-02-27 22:22:51 -05: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 05:29:19 -04:00
|
|
|
describe "triggers Discourse events" do
|
2019-01-03 12:03:01 -05: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 10:17:05 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when resolving flags" do
|
2019-01-03 12:03:01 -05: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 10:17:05 -04:00
|
|
|
|
2019-01-03 12:03:01 -05: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 10:17:05 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05: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 10:17:05 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
it "creates events for ignored" do
|
2023-03-02 10:40:53 -05:00
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events { reviewable.perform(moderator, :ignore_and_do_nothing) }
|
2019-01-03 12:03:01 -05: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 10:17:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-09-05 01:47:18 -04: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-05 14:16:51 -05:00
|
|
|
end
|