FIX: Unread group PMs should use `GroupUser#first_unread_pm_at`. (#14075)

This bug was causing unread PMs for groups to appear inaccurate.
This commit is contained in:
Alan Guo Xiang Tan 2021-08-18 11:23:28 +08:00 committed by GitHub
parent 3d92555f7a
commit d13716286c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View File

@ -182,9 +182,23 @@ class TopicQuery
staff: user.staff? staff: user.staff?
) )
first_unread_pm_at = UserStat first_unread_pm_at =
.where(user_id: user.id) case type
.pluck_first(:first_unread_pm_at) when :user
user_first_unread_pm_at(user)
when :group
GroupUser
.where(user: user, group: group)
.pluck_first(:first_unread_pm_at)
else
user_first_unread_pm_at = user_first_unread_pm_at(user)
group_first_unread_pm_at = GroupUser
.where(user: user)
.minimum(:first_unread_pm_at)
[user_first_unread_pm_at, group_first_unread_pm_at].compact.min
end
if first_unread_pm_at if first_unread_pm_at
list = list.where("topics.updated_at >= ?", first_unread_pm_at) list = list.where("topics.updated_at >= ?", first_unread_pm_at)
@ -246,5 +260,9 @@ class TopicQuery
.first .first
end end
end end
def user_first_unread_pm_at(user)
UserStat.where(user: user).pluck_first(:first_unread_pm_at)
end
end end
end end

View File

@ -111,16 +111,26 @@ describe TopicQuery::PrivateMessageLists do
end end
describe '#list_private_messages_all_unread' do describe '#list_private_messages_all_unread' do
it 'returns a list of unread private messages' do before do
topics = TopicQuery.new(nil).list_private_messages_all_unread(user_2).topics
expect(topics).to eq([])
TopicUser.find_by(user: user_2, topic: group_message).update!( TopicUser.find_by(user: user_2, topic: group_message).update!(
last_read_post_number: 1 last_read_post_number: 1
) )
create_post(user: user, topic: group_message) create_post(user: user, topic: group_message)
end
it 'returns a list of unread private messages' do
topics = TopicQuery.new(nil).list_private_messages_all_unread(user_2).topics
expect(topics).to contain_exactly(group_message)
end
it 'takes into account first_unread_pm_at optimization' do
user_2.user_stat.update!(first_unread_pm_at: group_message.created_at + 1.day)
GroupUser.find_by(user: user_2, group: group).update!(
first_unread_pm_at: group_message.created_at - 1.day
)
topics = TopicQuery.new(nil).list_private_messages_all_unread(user_2).topics topics = TopicQuery.new(nil).list_private_messages_all_unread(user_2).topics