mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Update first_pm_unread_at of user's groups without unread.
If a user always read all group messages, we will never update the
`first_pm_unread_at` column since the previous query will not return the
group_user. Instead, we should update `first_pm_unread_at` to the
current timestamp if the user has read everything.
Follow-up to 9b75d95fc6
This commit is contained in:
parent
122cf8d3fb
commit
f27de87bf3
@ -24,7 +24,7 @@ class GroupUser < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.update_first_unread_pm(last_seen, limit: 10_000)
|
def self.update_first_unread_pm(last_seen, limit: 10_000)
|
||||||
DB.exec(<<~SQL, archetype: Archetype.private_message, last_seen: last_seen, limit: limit)
|
DB.exec(<<~SQL, archetype: Archetype.private_message, last_seen: last_seen, limit: limit, now: 10.minutes.ago)
|
||||||
UPDATE group_users gu
|
UPDATE group_users gu
|
||||||
SET first_unread_pm_at = Y.min_date
|
SET first_unread_pm_at = Y.min_date
|
||||||
FROM (
|
FROM (
|
||||||
@ -33,6 +33,12 @@ class GroupUser < ActiveRecord::Base
|
|||||||
X.user_id,
|
X.user_id,
|
||||||
X.min_date
|
X.min_date
|
||||||
FROM (
|
FROM (
|
||||||
|
SELECT
|
||||||
|
gu.group_id,
|
||||||
|
gu.user_id,
|
||||||
|
COALESCE(Z.min_date, :now) min_date
|
||||||
|
FROM group_users gu
|
||||||
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
gu2.group_id,
|
gu2.group_id,
|
||||||
gu2.user_id,
|
gu2.user_id,
|
||||||
@ -51,6 +57,7 @@ class GroupUser < ActiveRecord::Base
|
|||||||
END
|
END
|
||||||
AND (COALESCE(tu.notification_level, 1) >= 2)
|
AND (COALESCE(tu.notification_level, 1) >= 2)
|
||||||
GROUP BY gu2.user_id, gu2.group_id
|
GROUP BY gu2.user_id, gu2.group_id
|
||||||
|
) AS Z ON Z.user_id = gu.user_id AND Z.group_id = gu.group_id
|
||||||
) AS X
|
) AS X
|
||||||
WHERE X.user_id IN (
|
WHERE X.user_id IN (
|
||||||
SELECT id
|
SELECT id
|
||||||
|
@ -163,6 +163,7 @@ describe GroupUser do
|
|||||||
|
|
||||||
describe '#ensure_consistency!' do
|
describe '#ensure_consistency!' do
|
||||||
fab!(:group) { Fabricate(:group) }
|
fab!(:group) { Fabricate(:group) }
|
||||||
|
fab!(:group_2) { Fabricate(:group) }
|
||||||
|
|
||||||
fab!(:pm_post) { Fabricate(:private_message_post) }
|
fab!(:pm_post) { Fabricate(:private_message_post) }
|
||||||
|
|
||||||
@ -173,6 +174,7 @@ describe GroupUser do
|
|||||||
fab!(:user) do
|
fab!(:user) do
|
||||||
Fabricate(:user, last_seen_at: Time.zone.now).tap do |u|
|
Fabricate(:user, last_seen_at: Time.zone.now).tap do |u|
|
||||||
group.add(u)
|
group.add(u)
|
||||||
|
group_2.add(u)
|
||||||
|
|
||||||
TopicUser.change(u.id, pm_topic.id,
|
TopicUser.change(u.id, pm_topic.id,
|
||||||
notification_level: TopicUser.notification_levels[:tracking],
|
notification_level: TopicUser.notification_levels[:tracking],
|
||||||
@ -213,11 +215,13 @@ describe GroupUser do
|
|||||||
topic_id: pm_topic.id
|
topic_id: pm_topic.id
|
||||||
)
|
)
|
||||||
|
|
||||||
GroupUser.ensure_consistency!
|
expect { GroupUser.ensure_consistency! }
|
||||||
|
.to_not change { group.group_users.find_by(user_id: user_3.id).first_unread_pm_at }
|
||||||
|
|
||||||
|
expect(post.topic.updated_at).to_not eq(10.minutes.ago)
|
||||||
expect(group.group_users.find_by(user_id: user.id).first_unread_pm_at).to eq_time(post.topic.updated_at)
|
expect(group.group_users.find_by(user_id: user.id).first_unread_pm_at).to eq_time(post.topic.updated_at)
|
||||||
expect(group.group_users.find_by(user_id: user_2.id).first_unread_pm_at).to_not eq_time(post.topic.updated_at)
|
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_3.id).first_unread_pm_at).to_not eq_time(post.topic.updated_at)
|
expect(group.group_users.find_by(user_id: user_2.id).first_unread_pm_at).to eq_time(10.minutes.ago)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user