2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe GroupUser do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
|
|
|
fab!(:user)
|
2022-11-30 22:52:08 +08:00
|
|
|
|
|
|
|
describe "callbacks" do
|
|
|
|
it "increments and decrements `Group#user_count` when record is created and destroyed" do
|
|
|
|
group_user = GroupUser.new(user: user, group: group)
|
|
|
|
|
|
|
|
expect do group_user.save! end.to change { group.reload.user_count }.from(0).to(1)
|
|
|
|
|
|
|
|
expect do group_user.destroy! end.to change { group.reload.user_count }.from(1).to(0)
|
|
|
|
end
|
|
|
|
end
|
2017-04-21 03:47:25 +08:00
|
|
|
|
|
|
|
it "correctly sets notification level" do
|
|
|
|
moderator = Fabricate(:moderator)
|
|
|
|
|
|
|
|
gu = GroupUser.find_by(user_id: moderator.id, group_id: Group::AUTO_GROUPS[:moderators])
|
|
|
|
|
|
|
|
expect(gu.notification_level).to eq(NotificationLevels.all[:tracking])
|
|
|
|
|
|
|
|
group = Group.create!(name: "bob")
|
|
|
|
group.add(moderator)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
gu = GroupUser.find_by(user_id: moderator.id, group_id: group.id)
|
|
|
|
expect(gu.notification_level).to eq(NotificationLevels.all[:watching])
|
|
|
|
|
|
|
|
group.remove(moderator)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
group.default_notification_level = 1
|
|
|
|
group.save
|
|
|
|
|
|
|
|
group.add(moderator)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
gu = GroupUser.find_by(user_id: moderator.id, group_id: group.id)
|
|
|
|
expect(gu.notification_level).to eq(NotificationLevels.all[:regular])
|
|
|
|
end
|
|
|
|
|
2020-08-07 00:27:27 +08:00
|
|
|
describe "default category notifications" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
|
|
|
fab!(:user)
|
2021-12-21 02:59:10 +08:00
|
|
|
fab!(:category1) { Fabricate(:category) }
|
|
|
|
fab!(:category2) { Fabricate(:category) }
|
|
|
|
fab!(:category3) { Fabricate(:category) }
|
|
|
|
fab!(:category4) { Fabricate(:category) }
|
|
|
|
fab!(:category5) { Fabricate(:category) }
|
2020-08-07 00:27:27 +08:00
|
|
|
|
|
|
|
def levels
|
|
|
|
CategoryUser.notification_levels
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change anything with no configured defaults" do
|
|
|
|
expect { group.add(user) }.to_not change { CategoryUser.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds new category notifications" do
|
|
|
|
group.muted_category_ids = [category1.id]
|
2020-08-14 05:20:23 +08:00
|
|
|
group.regular_category_ids = [category2.id]
|
|
|
|
group.tracking_category_ids = [category3.id]
|
|
|
|
group.watching_category_ids = [category4.id]
|
|
|
|
group.watching_first_post_category_ids = [category5.id]
|
2020-08-07 00:27:27 +08:00
|
|
|
group.save!
|
2020-08-14 05:20:23 +08:00
|
|
|
expect { group.add(user) }.to change { CategoryUser.count }.by(5)
|
2021-05-14 07:45:14 +08:00
|
|
|
h = CategoryUser.notification_levels_for(user)
|
2020-08-07 00:27:27 +08:00
|
|
|
expect(h[category1.id]).to eq(levels[:muted])
|
2020-08-14 05:20:23 +08:00
|
|
|
expect(h[category2.id]).to eq(levels[:regular])
|
|
|
|
expect(h[category3.id]).to eq(levels[:tracking])
|
|
|
|
expect(h[category4.id]).to eq(levels[:watching])
|
|
|
|
expect(h[category5.id]).to eq(levels[:watching_first_post])
|
2020-08-07 00:27:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "only upgrades notifications" do
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category1.id,
|
|
|
|
notification_level: levels[:muted],
|
|
|
|
)
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category2.id,
|
|
|
|
notification_level: levels[:tracking],
|
|
|
|
)
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category3.id,
|
|
|
|
notification_level: levels[:watching_first_post],
|
|
|
|
)
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category4.id,
|
|
|
|
notification_level: levels[:watching],
|
|
|
|
)
|
2020-08-14 05:20:23 +08:00
|
|
|
group.regular_category_ids = [category1.id]
|
|
|
|
group.watching_first_post_category_ids = [category2.id, category3.id, category4.id]
|
2020-08-07 00:27:27 +08:00
|
|
|
group.save!
|
|
|
|
group.add(user)
|
2021-05-14 07:45:14 +08:00
|
|
|
h = CategoryUser.notification_levels_for(user)
|
2020-08-14 05:20:23 +08:00
|
|
|
expect(h[category1.id]).to eq(levels[:regular])
|
2020-08-07 00:27:27 +08:00
|
|
|
expect(h[category2.id]).to eq(levels[:watching_first_post])
|
|
|
|
expect(h[category3.id]).to eq(levels[:watching_first_post])
|
|
|
|
expect(h[category4.id]).to eq(levels[:watching])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "merges notifications" do
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category1.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category2.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category_id: category4.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching_first_post],
|
|
|
|
)
|
|
|
|
group.muted_category_ids = [category3.id]
|
|
|
|
group.tracking_category_ids = [category4.id]
|
|
|
|
group.save!
|
|
|
|
group.add(user)
|
2021-05-14 07:45:14 +08:00
|
|
|
h = CategoryUser.notification_levels_for(user)
|
2020-08-07 00:27:27 +08:00
|
|
|
expect(h[category1.id]).to eq(levels[:tracking])
|
|
|
|
expect(h[category2.id]).to eq(levels[:watching])
|
|
|
|
expect(h[category3.id]).to eq(levels[:muted])
|
|
|
|
expect(h[category4.id]).to eq(levels[:watching_first_post])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "default tag notifications" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
|
|
|
fab!(:user)
|
2021-12-21 02:59:10 +08:00
|
|
|
fab!(:tag1) { Fabricate(:tag) }
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
fab!(:tag3) { Fabricate(:tag) }
|
|
|
|
fab!(:tag4) { Fabricate(:tag) }
|
|
|
|
fab!(:tag5) { Fabricate(:tag) }
|
|
|
|
fab!(:synonym1) { Fabricate(:tag, target_tag: tag1) }
|
2020-08-07 00:27:27 +08:00
|
|
|
|
|
|
|
def levels
|
|
|
|
TagUser.notification_levels
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change anything with no configured defaults" do
|
|
|
|
expect { group.add(user) }.to_not change { TagUser.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds new tag notifications" do
|
|
|
|
group.muted_tags = [synonym1.name]
|
2020-08-14 05:20:23 +08:00
|
|
|
group.regular_tags = [tag2.name]
|
|
|
|
group.tracking_tags = [tag3.name]
|
|
|
|
group.watching_tags = [tag4.name]
|
|
|
|
group.watching_first_post_tags = [tag5.name]
|
2020-08-07 00:27:27 +08:00
|
|
|
group.save!
|
2020-08-14 05:20:23 +08:00
|
|
|
expect { group.add(user) }.to change { TagUser.count }.by(5)
|
2020-08-07 00:27:27 +08:00
|
|
|
expect(TagUser.lookup(user, :muted).pluck(:tag_id)).to eq([tag1.id])
|
2020-08-14 05:20:23 +08:00
|
|
|
expect(TagUser.lookup(user, :regular).pluck(:tag_id)).to eq([tag2.id])
|
|
|
|
expect(TagUser.lookup(user, :tracking).pluck(:tag_id)).to eq([tag3.id])
|
|
|
|
expect(TagUser.lookup(user, :watching).pluck(:tag_id)).to eq([tag4.id])
|
|
|
|
expect(TagUser.lookup(user, :watching_first_post).pluck(:tag_id)).to eq([tag5.id])
|
2020-08-07 00:27:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "only upgrades notifications" do
|
|
|
|
TagUser.create!(user: user, tag_id: tag1.id, notification_level: levels[:muted])
|
|
|
|
TagUser.create!(user: user, tag_id: tag2.id, notification_level: levels[:tracking])
|
|
|
|
TagUser.create!(user: user, tag_id: tag3.id, notification_level: levels[:watching_first_post])
|
|
|
|
TagUser.create!(user: user, tag_id: tag4.id, notification_level: levels[:watching])
|
2020-08-14 05:20:23 +08:00
|
|
|
group.regular_tags = [tag1.name]
|
|
|
|
group.watching_first_post_tags = [tag2.name, tag3.name, tag4.name]
|
2020-08-07 00:27:27 +08:00
|
|
|
group.save!
|
|
|
|
group.add(user)
|
|
|
|
expect(TagUser.lookup(user, :muted).pluck(:tag_id)).to be_empty
|
2020-08-14 05:20:23 +08:00
|
|
|
expect(TagUser.lookup(user, :regular).pluck(:tag_id)).to eq([tag1.id])
|
2020-08-07 00:27:27 +08:00
|
|
|
expect(TagUser.lookup(user, :tracking).pluck(:tag_id)).to be_empty
|
|
|
|
expect(TagUser.lookup(user, :watching).pluck(:tag_id)).to eq([tag4.id])
|
2020-08-14 05:20:23 +08:00
|
|
|
expect(TagUser.lookup(user, :watching_first_post).pluck(:tag_id)).to contain_exactly(
|
|
|
|
tag2.id,
|
|
|
|
tag3.id,
|
|
|
|
)
|
2020-08-07 00:27:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "merges notifications" do
|
|
|
|
TagUser.create!(user: user, tag_id: tag1.id, notification_level: levels[:tracking])
|
|
|
|
TagUser.create!(user: user, tag_id: tag2.id, notification_level: levels[:watching])
|
|
|
|
TagUser.create!(user: user, tag_id: tag4.id, notification_level: levels[:watching_first_post])
|
|
|
|
group.muted_tags = [tag3.name]
|
|
|
|
group.tracking_tags = [tag2.name]
|
|
|
|
group.save!
|
|
|
|
group.add(user)
|
|
|
|
expect(TagUser.lookup(user, :muted).pluck(:tag_id)).to eq([tag3.id])
|
|
|
|
expect(TagUser.lookup(user, :tracking).pluck(:tag_id)).to eq([tag1.id])
|
|
|
|
expect(TagUser.lookup(user, :watching).pluck(:tag_id)).to eq([tag2.id])
|
|
|
|
expect(TagUser.lookup(user, :watching_first_post).pluck(:tag_id)).to eq([tag4.id])
|
|
|
|
end
|
|
|
|
end
|
2020-09-03 14:02:15 +08:00
|
|
|
|
|
|
|
describe "#ensure_consistency!" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2020-09-10 17:13:11 +08:00
|
|
|
fab!(:group_2) { Fabricate(:group) }
|
2020-09-03 14:02:15 +08:00
|
|
|
|
|
|
|
fab!(:pm_post) { Fabricate(:private_message_post) }
|
|
|
|
|
|
|
|
fab!(:pm_topic) { pm_post.topic.tap { |t| t.allowed_groups << group } }
|
|
|
|
|
|
|
|
fab!(:user) do
|
|
|
|
Fabricate(:user, last_seen_at: Time.zone.now).tap do |u|
|
|
|
|
group.add(u)
|
2020-09-10 17:13:11 +08:00
|
|
|
group_2.add(u)
|
2020-09-03 14:02:15 +08:00
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
u.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
last_read_post_number: pm_post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# User that is not tracking topic
|
|
|
|
fab!(:user_2) do
|
|
|
|
Fabricate(:user, last_seen_at: Time.zone.now).tap do |u|
|
|
|
|
group.add(u)
|
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
u.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:regular],
|
|
|
|
last_read_post_number: pm_post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# User that has not been seen
|
|
|
|
fab!(:user_3) do
|
|
|
|
Fabricate(:user).tap do |u|
|
|
|
|
group.add(u)
|
|
|
|
|
|
|
|
TopicUser.change(
|
|
|
|
u.id,
|
|
|
|
pm_topic.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
last_read_post_number: pm_post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates first unread pm timestamp correctly" do
|
|
|
|
freeze_time 10.minutes.from_now
|
|
|
|
|
|
|
|
post = create_post(user: pm_topic.user, topic_id: pm_topic.id)
|
|
|
|
|
2020-09-10 17:13:11 +08:00
|
|
|
expect { GroupUser.ensure_consistency! }.to_not change {
|
|
|
|
group.group_users.find_by(user_id: user_3.id).first_unread_pm_at
|
|
|
|
}
|
2020-09-03 14:02:15 +08:00
|
|
|
|
2020-09-10 17:34:48 +08:00
|
|
|
expect(post.topic.updated_at).to_not eq_time(10.minutes.ago)
|
2020-09-03 14:02:15 +08:00
|
|
|
expect(group.group_users.find_by(user_id: user.id).first_unread_pm_at).to eq_time(
|
|
|
|
post.topic.updated_at,
|
|
|
|
)
|
2020-09-10 17:13:11 +08:00
|
|
|
expect(group_2.group_users.find_by(user_id: user.id).first_unread_pm_at).to eq_time(
|
|
|
|
10.minutes.ago,
|
|
|
|
)
|
|
|
|
expect(group.group_users.find_by(user_id: user_2.id).first_unread_pm_at).to eq_time(
|
|
|
|
10.minutes.ago,
|
|
|
|
)
|
2020-09-03 14:02:15 +08:00
|
|
|
end
|
|
|
|
end
|
2021-09-21 09:29:12 +08:00
|
|
|
|
|
|
|
describe "#destroy!" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2021-09-21 09:29:12 +08:00
|
|
|
|
2022-08-12 20:45:09 +08:00
|
|
|
it "removes `primary_group_id`, `flair_group_id` and exec `match_primary_group_changes` method on user model" do
|
|
|
|
user = Fabricate(:user, primary_group: group, flair_group: group)
|
2021-09-21 09:29:12 +08:00
|
|
|
group_user = Fabricate(:group_user, group: group, user: user)
|
|
|
|
|
|
|
|
user.expects(:match_primary_group_changes).once
|
|
|
|
group_user.destroy!
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.primary_group_id).to be_nil
|
2022-08-12 20:45:09 +08:00
|
|
|
expect(user.flair_group_id).to be_nil
|
2021-09-21 09:29:12 +08:00
|
|
|
end
|
2022-08-29 18:00:48 +08:00
|
|
|
|
|
|
|
it "restores previous trust level" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(user.trust_level).to eq(1)
|
|
|
|
|
2023-03-01 12:35:21 +08:00
|
|
|
user.change_trust_level!(1, log_action_for: Discourse.system_user)
|
2022-08-29 18:00:48 +08:00
|
|
|
user.change_trust_level!(2, log_action_for: Discourse.system_user)
|
|
|
|
group.update!(grant_trust_level: 4)
|
|
|
|
|
|
|
|
group_user = Fabricate(:group_user, group: group, user: user)
|
|
|
|
expect(user.reload.trust_level).to eq(4)
|
|
|
|
|
|
|
|
group_user.destroy!
|
2023-03-01 12:35:21 +08:00
|
|
|
# keep in mind that we do not restore tl3, cause reqs can be lost
|
|
|
|
expect(user.reload.trust_level).to eq(2)
|
2022-08-29 18:00:48 +08:00
|
|
|
end
|
2021-09-21 09:29:12 +08:00
|
|
|
end
|
2017-04-21 03:47:25 +08:00
|
|
|
end
|