2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TopicUser do
|
2017-05-25 15:07:12 -04:00
|
|
|
let :watching do
|
|
|
|
TopicUser.notification_levels[:watching]
|
|
|
|
end
|
|
|
|
|
|
|
|
let :regular do
|
|
|
|
TopicUser.notification_levels[:regular]
|
|
|
|
end
|
|
|
|
|
|
|
|
let :tracking do
|
|
|
|
TopicUser.notification_levels[:tracking]
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-06-28 04:34:20 -04:00
|
|
|
describe "#unwatch_categories!" do
|
|
|
|
it "correctly unwatches categories" do
|
|
|
|
op_topic = Fabricate(:topic)
|
|
|
|
another_topic = Fabricate(:topic)
|
|
|
|
tracked_topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
user = op_topic.user
|
|
|
|
|
|
|
|
TopicUser.change(user.id, op_topic, notification_level: watching)
|
|
|
|
TopicUser.change(user.id, another_topic, notification_level: watching)
|
|
|
|
TopicUser.change(
|
|
|
|
user.id,
|
|
|
|
tracked_topic,
|
|
|
|
notification_level: watching,
|
|
|
|
total_msecs_viewed: SiteSetting.default_other_auto_track_topics_after_msecs + 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
TopicUser.unwatch_categories!(user, [Fabricate(:category).id, Fabricate(:category).id])
|
|
|
|
expect(TopicUser.get(another_topic, user).notification_level).to eq(watching)
|
|
|
|
|
|
|
|
TopicUser.unwatch_categories!(user, [op_topic.category_id])
|
|
|
|
|
|
|
|
expect(TopicUser.get(op_topic, user).notification_level).to eq(watching)
|
|
|
|
expect(TopicUser.get(another_topic, user).notification_level).to eq(regular)
|
|
|
|
expect(TopicUser.get(tracked_topic, user).notification_level).to eq(tracking)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-08 05:53:52 -05:00
|
|
|
describe "#notification_levels" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when verifying enum sequence" do
|
2016-01-08 05:53:52 -05:00
|
|
|
before { @notification_levels = TopicUser.notification_levels }
|
|
|
|
|
|
|
|
it "'muted' should be at 0 position" do
|
|
|
|
expect(@notification_levels[:muted]).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'watching' should be at 3rd position" do
|
|
|
|
expect(@notification_levels[:watching]).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#notification_reasons" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when verifying enum sequence" do
|
2016-01-08 05:53:52 -05:00
|
|
|
before { @notification_reasons = TopicUser.notification_reasons }
|
|
|
|
|
|
|
|
it "'created_topic' should be at 1st position" do
|
|
|
|
expect(@notification_reasons[:created_topic]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'plugin_changed' should be at 9th position" do
|
|
|
|
expect(@notification_reasons[:plugin_changed]).to eq(9)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
it { is_expected.to belong_to :user }
|
|
|
|
it { is_expected.to belong_to :topic }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:user)
|
2013-03-06 15:17:07 -05:00
|
|
|
|
2013-12-29 21:02:12 -05:00
|
|
|
let(:topic) do
|
|
|
|
u = Fabricate(:user)
|
|
|
|
guardian = Guardian.new(u)
|
|
|
|
TopicCreator.create(u, guardian, title: "this is my topic title")
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2017-04-11 23:44:00 -04:00
|
|
|
let(:topic_user) { TopicUser.get(topic, user) }
|
2013-03-06 15:17:07 -05:00
|
|
|
let(:topic_creator_user) { TopicUser.get(topic, topic.user) }
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
let(:new_user) do
|
|
|
|
u = Fabricate(:user)
|
|
|
|
u.user_option.update_columns(auto_track_topics_after_msecs: 1000)
|
|
|
|
u
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2016-02-18 00:57:22 -05:00
|
|
|
|
2013-03-06 15:17:07 -05:00
|
|
|
let(:topic_new_user) { TopicUser.get(topic, new_user) }
|
2013-12-29 21:02:12 -05:00
|
|
|
let(:yesterday) { DateTime.now.yesterday }
|
2013-03-06 15:17:07 -05:00
|
|
|
|
2015-01-06 20:19:23 -05:00
|
|
|
def ensure_topic_user
|
|
|
|
TopicUser.change(user, topic, last_emailed_post_number: 1)
|
|
|
|
end
|
2013-03-06 15:17:07 -05:00
|
|
|
|
|
|
|
describe "unpinned" do
|
|
|
|
it "defaults to blank" do
|
2015-01-06 20:19:23 -05:00
|
|
|
ensure_topic_user
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_user.cleared_pinned_at).to be_blank
|
2013-03-06 15:17:07 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "notifications" do
|
2017-04-11 23:44:00 -04:00
|
|
|
it "should trigger the right DiscourseEvent" do
|
2020-11-11 14:46:13 -05:00
|
|
|
called = false
|
|
|
|
blk = Proc.new { called = true }
|
2017-04-11 23:44:00 -04:00
|
|
|
begin
|
2020-11-11 14:46:13 -05:00
|
|
|
DiscourseEvent.on(:topic_notification_level_changed, &blk)
|
2017-04-11 23:44:00 -04:00
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
user.id,
|
|
|
|
topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(called).to eq(true)
|
|
|
|
ensure
|
2020-11-11 14:46:13 -05:00
|
|
|
DiscourseEvent.off(:topic_notification_level_changed, &blk)
|
2017-04-11 23:44:00 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should be set to tracking if auto_track_topics is enabled" do
|
2016-02-18 00:57:22 -05:00
|
|
|
user.user_option.update_column(:auto_track_topics_after_msecs, 0)
|
2015-01-06 20:19:23 -05:00
|
|
|
ensure_topic_user
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.get(topic, user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should reset regular topics to tracking topics if auto track is changed" do
|
2015-01-06 20:19:23 -05:00
|
|
|
ensure_topic_user
|
2016-02-18 00:57:22 -05:00
|
|
|
user.user_option.auto_track_topics_after_msecs = 0
|
|
|
|
user.user_option.save
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should be set to "regular" notifications, by default on non creators' do
|
2015-01-06 20:19:23 -05:00
|
|
|
ensure_topic_user
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.get(topic, user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:regular],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "reason should reset when changed" do
|
2013-03-06 15:17:07 -05:00
|
|
|
topic.notify_muted!(topic.user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.get(topic, topic.user).notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:user_changed],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it "should have the correct reason for a user change when watched" do
|
2013-03-06 15:17:07 -05:00
|
|
|
topic.notify_watch!(user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
expect(topic_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:user_changed],
|
|
|
|
)
|
|
|
|
expect(topic_user.notifications_changed_at).not_to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it "should have the correct reason for a user change when set to regular" do
|
2013-03-06 15:17:07 -05:00
|
|
|
topic.notify_regular!(user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:regular])
|
|
|
|
expect(topic_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:user_changed],
|
|
|
|
)
|
|
|
|
expect(topic_user.notifications_changed_at).not_to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it "should have the correct reason for a user change when set to regular" do
|
2013-03-06 15:17:07 -05:00
|
|
|
topic.notify_muted!(user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:muted])
|
|
|
|
expect(topic_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:user_changed],
|
|
|
|
)
|
|
|
|
expect(topic_user.notifications_changed_at).not_to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should watch topics a user created" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_creator_user.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
expect(topic_creator_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:created_topic],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "visited at" do
|
2017-02-15 10:58:18 -05:00
|
|
|
it "set upon initial visit" do
|
|
|
|
freeze_time yesterday
|
|
|
|
|
2016-05-14 04:06:29 -04:00
|
|
|
TopicUser.track_visit!(topic.id, user.id)
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2017-02-15 10:58:18 -05:00
|
|
|
expect(topic_user.first_visited_at.to_i).to eq(yesterday.to_i)
|
|
|
|
expect(topic_user.last_visited_at.to_i).to eq(yesterday.to_i)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "updates upon repeat visit" do
|
2017-02-15 10:58:18 -05:00
|
|
|
freeze_time yesterday
|
|
|
|
|
|
|
|
TopicUser.track_visit!(topic.id, user.id)
|
|
|
|
|
|
|
|
freeze_time Time.zone.now
|
|
|
|
|
|
|
|
TopicUser.track_visit!(topic.id, user.id)
|
|
|
|
# reload is a no go
|
|
|
|
topic_user = TopicUser.get(topic, user)
|
|
|
|
expect(topic_user.first_visited_at.to_i).to eq(yesterday.to_i)
|
|
|
|
expect(topic_user.last_visited_at.to_i).to eq(Time.zone.now.to_i)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "read tracking" do
|
2013-03-06 15:17:07 -05:00
|
|
|
context "without auto tracking" do
|
|
|
|
let(:topic_user) { TopicUser.get(topic, user) }
|
|
|
|
|
|
|
|
it "should create a new record for a visit" do
|
2017-02-15 10:58:18 -05:00
|
|
|
freeze_time yesterday
|
|
|
|
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, topic.id, 1, 1, 0)
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
expect(topic_user.last_read_post_number).to eq(1)
|
|
|
|
expect(topic_user.last_visited_at.to_i).to eq(yesterday.to_i)
|
|
|
|
expect(topic_user.first_visited_at.to_i).to eq(yesterday.to_i)
|
2013-03-06 15:17:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should update the record for repeat visit" do
|
2017-02-15 10:58:18 -05:00
|
|
|
today = Time.zone.now
|
|
|
|
freeze_time Time.zone.now
|
|
|
|
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(user, topic.id, 1, 1, 0)
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
tomorrow = 1.day.from_now
|
|
|
|
freeze_time tomorrow
|
|
|
|
|
|
|
|
Fabricate(:post, topic: topic, user: user)
|
2021-09-08 21:16:53 -04:00
|
|
|
channel = TopicTrackingState.unread_channel_key(user.id)
|
|
|
|
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish(channel) { TopicUser.update_last_read(user, topic.id, 2, 1, 0) }
|
|
|
|
|
|
|
|
expect(messages.blank?).to eq(false)
|
|
|
|
|
2017-02-15 10:58:18 -05:00
|
|
|
topic_user = TopicUser.get(topic, user)
|
|
|
|
|
|
|
|
expect(topic_user.last_read_post_number).to eq(2)
|
|
|
|
expect(topic_user.last_visited_at.to_i).to eq(today.to_i)
|
|
|
|
expect(topic_user.first_visited_at.to_i).to eq(today.to_i)
|
2013-03-06 15:17:07 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with private messages" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:target_user) { Fabricate(:user) }
|
2018-02-26 04:57:16 -05:00
|
|
|
|
|
|
|
let(:post) do
|
|
|
|
create_post(archetype: Archetype.private_message, target_usernames: target_user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:topic) { post.topic }
|
|
|
|
|
2020-01-23 10:37:48 -05:00
|
|
|
it "should ensure recipients and senders are watching" do
|
2018-02-26 04:57:16 -05:00
|
|
|
expect(TopicUser.get(topic, post.user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(TopicUser.get(topic, target_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should ensure invited user is watching once visited" do
|
|
|
|
another_user = Fabricate(:user)
|
|
|
|
topic.invite(target_user, another_user.username)
|
|
|
|
TopicUser.track_visit!(topic.id, another_user.id)
|
|
|
|
|
|
|
|
expect(TopicUser.get(topic, another_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
2014-01-22 01:46:52 -05:00
|
|
|
|
2018-02-26 04:57:16 -05:00
|
|
|
another_user = Fabricate(:user)
|
|
|
|
TopicUser.track_visit!(topic.id, another_user.id)
|
2014-01-22 01:46:52 -05:00
|
|
|
|
2018-02-26 04:57:16 -05:00
|
|
|
expect(TopicUser.get(topic, another_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:regular],
|
|
|
|
)
|
2014-01-22 01:46:52 -05:00
|
|
|
end
|
2018-03-05 00:35:15 -05:00
|
|
|
|
2021-09-08 21:16:53 -04:00
|
|
|
it "should publish the right message_bus message" do
|
|
|
|
TopicUser.update_last_read(user, topic.id, 1, 1, 0)
|
|
|
|
|
|
|
|
Fabricate(:post, topic: topic, user: user)
|
|
|
|
|
|
|
|
channel = PrivateMessageTopicTrackingState.user_channel(user.id)
|
|
|
|
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish(channel) { TopicUser.update_last_read(user, topic.id, 2, 1, 0) }
|
|
|
|
|
|
|
|
expect(messages.blank?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2018-03-05 00:35:15 -05:00
|
|
|
describe "inviting a group" do
|
|
|
|
let(:group) do
|
|
|
|
Fabricate(:group, default_notification_level: NotificationLevels.topic_levels[:tracking])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should use group's default notification level" do
|
|
|
|
another_user = Fabricate(:user)
|
|
|
|
group.add(another_user)
|
2019-08-16 04:23:51 -04:00
|
|
|
|
2021-04-14 12:30:51 -04:00
|
|
|
Jobs.run_immediately!
|
2018-03-05 00:35:15 -05:00
|
|
|
topic.invite_group(target_user, group)
|
|
|
|
|
|
|
|
expect(TopicUser.get(topic, another_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
another_user = Fabricate(:user)
|
|
|
|
topic.invite(target_user, another_user.username)
|
|
|
|
TopicUser.track_visit!(topic.id, another_user.id)
|
|
|
|
|
|
|
|
expect(TopicUser.get(topic, another_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2014-01-22 01:46:52 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with auto tracking" do
|
2013-03-18 13:55:34 -04:00
|
|
|
let(:post_creator) do
|
|
|
|
PostCreator.new(new_user, raw: Fabricate.build(:post).raw, topic_id: topic.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2017-11-17 16:08:31 -05:00
|
|
|
before { TopicUser.update_last_read(new_user, topic.id, 2, 2, 0) }
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "should automatically track topics you reply to" do
|
2013-03-18 13:55:34 -04:00
|
|
|
post_creator.create
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_new_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
|
|
|
|
expect(topic_new_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:created_post],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2017-04-14 18:30:01 -04:00
|
|
|
it "should update tracking state when you reply" do
|
|
|
|
new_user.user_option.update_column(:notification_level_when_replying, 3)
|
|
|
|
post_creator.create
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE topic_users set notification_level=2
|
|
|
|
WHERE topic_id = :topic_id AND user_id = :user_id",
|
|
|
|
topic_id: topic_new_user.topic_id,
|
|
|
|
user_id: topic_new_user.user_id,
|
|
|
|
)
|
|
|
|
|
2017-04-14 18:30:01 -04:00
|
|
|
TopicUser.auto_notification(
|
|
|
|
topic_new_user.user_id,
|
|
|
|
topic_new_user.topic_id,
|
|
|
|
TopicUser.notification_reasons[:created_post],
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: topic_new_user.user_id, topic_id: topic_new_user.topic_id)
|
|
|
|
expect(tu.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not update tracking state when you reply" do
|
|
|
|
new_user.user_option.update_column(:notification_level_when_replying, 3)
|
|
|
|
post_creator.create
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE topic_users set notification_level=3
|
2017-04-14 18:30:01 -04:00
|
|
|
WHERE topic_id = :topic_id AND user_id = :user_id",
|
|
|
|
topic_id: topic_new_user.topic_id,
|
|
|
|
user_id: topic_new_user.user_id,
|
|
|
|
)
|
|
|
|
TopicUser.auto_notification(
|
|
|
|
topic_new_user.user_id,
|
|
|
|
topic_new_user.topic_id,
|
|
|
|
TopicUser.notification_reasons[:created_post],
|
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: topic_new_user.user_id, topic_id: topic_new_user.topic_id)
|
|
|
|
expect(tu.notification_level).to eq(TopicUser.notification_levels[:watching])
|
|
|
|
end
|
|
|
|
|
2017-04-19 06:26:04 -04:00
|
|
|
it "should not update tracking state when state manually set to normal you reply" do
|
|
|
|
new_user.user_option.update_column(:notification_level_when_replying, 3)
|
|
|
|
post_creator.create
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE topic_users set notification_level=1
|
2017-04-19 06:26:04 -04:00
|
|
|
WHERE topic_id = :topic_id AND user_id = :user_id",
|
|
|
|
topic_id: topic_new_user.topic_id,
|
|
|
|
user_id: topic_new_user.user_id,
|
|
|
|
)
|
|
|
|
TopicUser.auto_notification(
|
|
|
|
topic_new_user.user_id,
|
|
|
|
topic_new_user.topic_id,
|
|
|
|
TopicUser.notification_reasons[:created_post],
|
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: topic_new_user.user_id, topic_id: topic_new_user.topic_id)
|
|
|
|
expect(tu.notification_level).to eq(TopicUser.notification_levels[:regular])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not update tracking state when state manually set to muted you reply" do
|
|
|
|
new_user.user_option.update_column(:notification_level_when_replying, 3)
|
|
|
|
post_creator.create
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE topic_users set notification_level=0
|
2017-04-19 06:26:04 -04:00
|
|
|
WHERE topic_id = :topic_id AND user_id = :user_id",
|
|
|
|
topic_id: topic_new_user.topic_id,
|
|
|
|
user_id: topic_new_user.user_id,
|
|
|
|
)
|
|
|
|
TopicUser.auto_notification(
|
|
|
|
topic_new_user.user_id,
|
|
|
|
topic_new_user.topic_id,
|
|
|
|
TopicUser.notification_reasons[:created_post],
|
|
|
|
TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
tu = TopicUser.find_by(user_id: topic_new_user.user_id, topic_id: topic_new_user.topic_id)
|
|
|
|
expect(tu.notification_level).to eq(TopicUser.notification_levels[:muted])
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "should not automatically track topics you reply to and have set state manually" do
|
2013-03-18 13:55:34 -04:00
|
|
|
post_creator.create
|
2013-03-06 15:17:07 -05:00
|
|
|
TopicUser.change(
|
|
|
|
new_user,
|
|
|
|
topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:regular],
|
|
|
|
)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_new_user.notification_level).to eq(TopicUser.notification_levels[:regular])
|
|
|
|
expect(topic_new_user.notifications_reason_id).to eq(
|
|
|
|
TopicUser.notification_reasons[:user_changed],
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should automatically track topics after they are read for long enough" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_new_user.notification_level).to eq(TopicUser.notification_levels[:regular])
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(
|
|
|
|
new_user,
|
|
|
|
topic.id,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
SiteSetting.default_other_auto_track_topics_after_msecs + 1,
|
2015-04-25 11:18:35 -04:00
|
|
|
)
|
|
|
|
expect(TopicUser.get(topic, new_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:tracking],
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it "should not automatically track topics after they are read for long enough if changed manually" do
|
2013-03-06 15:17:07 -05:00
|
|
|
TopicUser.change(
|
|
|
|
new_user,
|
|
|
|
topic,
|
|
|
|
notification_level: TopicUser.notification_levels[:regular],
|
|
|
|
)
|
2017-11-17 16:08:31 -05:00
|
|
|
TopicUser.update_last_read(
|
|
|
|
new_user,
|
|
|
|
topic,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
SiteSetting.default_other_auto_track_topics_after_msecs + 1,
|
|
|
|
)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic_new_user.notification_level).to eq(TopicUser.notification_levels[:regular])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2018-08-23 00:36:49 -04:00
|
|
|
|
|
|
|
it "should not automatically track PMs" do
|
|
|
|
new_user.user_option.update!(auto_track_topics_after_msecs: 0)
|
|
|
|
|
|
|
|
another_user = Fabricate(:user)
|
2022-10-04 20:50:20 -04:00
|
|
|
Group.refresh_automatic_groups!
|
2018-08-23 00:36:49 -04:00
|
|
|
pm = Fabricate(:private_message_topic, user: another_user)
|
|
|
|
pm.invite(another_user, new_user.username)
|
|
|
|
|
|
|
|
TopicUser.track_visit!(pm.id, new_user.id)
|
|
|
|
TopicUser.update_last_read(new_user, pm.id, 2, 2, 1000)
|
|
|
|
expect(TopicUser.get(pm, new_user).notification_level).to eq(
|
|
|
|
TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "change a flag" do
|
|
|
|
it "only inserts a row once, even on repeated calls" do
|
2013-12-29 21:02:12 -05:00
|
|
|
topic
|
|
|
|
user
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect {
|
2015-01-06 20:19:23 -05:00
|
|
|
TopicUser.change(user, topic.id, total_msecs_viewed: 1)
|
|
|
|
TopicUser.change(user, topic.id, total_msecs_viewed: 2)
|
|
|
|
TopicUser.change(user, topic.id, total_msecs_viewed: 3)
|
2015-04-25 11:18:35 -04:00
|
|
|
}.to change(TopicUser, :count).by(1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe "after creating a row" do
|
2015-01-06 20:19:23 -05:00
|
|
|
before { ensure_topic_user }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "has a lookup" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.lookup_for(user, [topic])).to be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a key in the lookup for this forum topic" do
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.lookup_for(user, [topic]).has_key?(topic.id)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-29 04:11:04 -04:00
|
|
|
it "can scope by tracking" do
|
|
|
|
TopicUser.create!(
|
|
|
|
user_id: 1,
|
|
|
|
topic_id: 1,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
TopicUser.create!(
|
|
|
|
user_id: 2,
|
|
|
|
topic_id: 1,
|
|
|
|
notification_level: TopicUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
TopicUser.create!(
|
|
|
|
user_id: 3,
|
|
|
|
topic_id: 1,
|
|
|
|
notification_level: TopicUser.notification_levels[:regular],
|
|
|
|
)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.tracking(1).count).to eq(2)
|
|
|
|
expect(TopicUser.tracking(10).count).to eq(0)
|
2013-05-29 04:11:04 -04:00
|
|
|
end
|
2013-04-05 00:29:46 -04:00
|
|
|
|
|
|
|
it "is able to self heal" do
|
|
|
|
p1 = Fabricate(:post)
|
|
|
|
p2 = Fabricate(:post, user: p1.user, topic: p1.topic, post_number: 2)
|
2013-07-22 01:06:53 -04:00
|
|
|
p1.topic.notifier.watch_topic!(p1.user_id)
|
2013-04-05 00:29:46 -04:00
|
|
|
|
2021-07-05 02:17:31 -04:00
|
|
|
DB.exec(
|
|
|
|
"UPDATE topic_users set last_read_post_number=0
|
2013-04-05 00:29:46 -04:00
|
|
|
WHERE topic_id = :topic_id AND user_id = :user_id",
|
|
|
|
topic_id: p1.topic_id,
|
|
|
|
user_id: p1.user_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
[p1, p2].each do |p|
|
|
|
|
PostTiming.create(
|
|
|
|
topic_id: p.topic_id,
|
|
|
|
post_number: p.post_number,
|
|
|
|
user_id: p.user_id,
|
|
|
|
msecs: 100,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
TopicUser.ensure_consistency!
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
tu = TopicUser.find_by(user_id: p1.user_id, topic_id: p1.topic_id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tu.last_read_post_number).to eq(p2.post_number)
|
2013-04-05 00:29:46 -04:00
|
|
|
end
|
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
describe "mailing_list_mode" do
|
|
|
|
it "will receive email notification for every topic" do
|
|
|
|
user1 = Fabricate(:user)
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2021-03-04 15:24:37 -05:00
|
|
|
SiteSetting.disable_mailing_list_mode = false
|
2016-05-26 22:35:22 -04:00
|
|
|
SiteSetting.default_email_mailing_list_mode = true
|
|
|
|
SiteSetting.default_email_mailing_list_mode_frequency = 1
|
2015-08-21 14:39:21 -04:00
|
|
|
|
|
|
|
user2 = Fabricate(:user)
|
2014-02-06 19:06:35 -05:00
|
|
|
post = create_post
|
2015-08-21 14:39:21 -04:00
|
|
|
|
|
|
|
user3 = Fabricate(:user)
|
2014-02-06 19:06:35 -05:00
|
|
|
create_post(topic_id: post.topic_id)
|
|
|
|
|
|
|
|
# mails posts from earlier topics
|
2014-05-06 09:41:59 -04:00
|
|
|
tu = TopicUser.find_by(user_id: user3.id, topic_id: post.topic_id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tu.last_emailed_post_number).to eq(2)
|
2014-02-06 19:06:35 -05:00
|
|
|
|
|
|
|
# mails nothing to random users
|
2014-05-06 09:41:59 -04:00
|
|
|
tu = TopicUser.find_by(user_id: user1.id, topic_id: post.topic_id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tu).to eq(nil)
|
2014-02-06 19:06:35 -05:00
|
|
|
|
|
|
|
# mails other user
|
2014-05-06 09:41:59 -04:00
|
|
|
tu = TopicUser.find_by(user_id: user2.id, topic_id: post.topic_id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(tu.last_emailed_post_number).to eq(2)
|
2013-12-29 21:02:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-24 04:50:02 -05:00
|
|
|
it "correctly triggers an event on first visit" do
|
|
|
|
begin
|
|
|
|
tracked_user = Fabricate(:user)
|
|
|
|
post = create_post
|
|
|
|
|
|
|
|
called = 0
|
|
|
|
visits = []
|
2023-11-29 00:38:07 -05:00
|
|
|
user_first_visit = ->(topic_id, user_id) do
|
2021-02-24 04:50:02 -05:00
|
|
|
visits << "#{topic_id}-#{user_id}"
|
|
|
|
called += 1
|
2023-11-29 00:38:07 -05:00
|
|
|
end
|
2021-02-24 04:50:02 -05:00
|
|
|
|
|
|
|
DiscourseEvent.on(:topic_first_visited_by_user, &user_first_visit)
|
|
|
|
|
|
|
|
expect(called).to eq(0)
|
|
|
|
|
|
|
|
TopicUser.change(tracked_user, post.topic.id, total_msecs_viewed: 1)
|
|
|
|
|
|
|
|
expect(visits).to eq(["#{post.topic.id}-#{tracked_user.id}"])
|
|
|
|
expect(called).to eq(1)
|
|
|
|
|
|
|
|
TopicUser.change(tracked_user, post.topic.id, total_msecs_viewed: 2)
|
|
|
|
|
|
|
|
expect(called).to eq(1)
|
|
|
|
ensure
|
|
|
|
DiscourseEvent.off(:topic_first_visited_by_user, &user_first_visit)
|
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|