From 61a716a98c5680897133f870320cfe9b445c8c28 Mon Sep 17 00:00:00 2001 From: Shivashis Padhi Date: Mon, 1 Aug 2022 13:58:50 +0530 Subject: [PATCH] Fix Posts query while populating top DMs, add OutgoingMessageCount check to tests --- store/sqlstore/post_store.go | 23 +++++++++++++++++------ store/storetest/post_store.go | 3 +++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 501d40a3dd..6e80105a3d 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -3003,14 +3003,14 @@ func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset } // fill SecondParticipant column - topDMs, err = postProcessTopDMs(s, userID, topDMs) + topDMs, err = postProcessTopDMs(s, userID, topDMs, since) if err != nil { return nil, err } return model.GetTopDMListWithPagination(topDMs, limit), nil } -func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([]*model.TopDM, error) { +func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM, since int64) ([]*model.TopDM, error) { var topDMsFiltered = []*model.TopDM{} var secondParticipantIds []string var channelIds []string @@ -3041,10 +3041,21 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([ // get outgoing message count for userId outgoingMessagesQuery := s.getQueryBuilder().Select("ch.Id as ChannelId, count(p.Id) as MessageCount").From("Channels as ch"). - Join("Posts as p on p.ChannelId=ch.Id").Where(sq.Eq{ - "ch.Id": channelIds, - "p.UserId": userID, - }).GroupBy("ch.Id") + Join("Posts as p on p.ChannelId=ch.Id").Where( + sq.And{ + sq.Gt{ + "p.UpdateAt": since, + }, + sq.Eq{ + "p.DeleteAt": 0, + }, + sq.Eq{ + "ch.Id": channelIds, + }, + sq.Eq{ + "p.UserId": userID, + }, + }).GroupBy("ch.Id") outgoingMessages := make([]*model.OutgoingMessageQueryResult, 0) sql, args, err := outgoingMessagesQuery.ToSql() diff --git a/store/storetest/post_store.go b/store/storetest/post_store.go index aca7a8f76e..f077182a01 100644 --- a/store/storetest/post_store.go +++ b/store/storetest/post_store.go @@ -4075,10 +4075,13 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { // check order, magnitude of items require.Equal(t, topDMs.Items[0].SecondParticipant.Id, u3.Id) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) + require.Equal(t, topDMs.Items[0].OutgoingMessageCount, int64(3)) require.Equal(t, topDMs.Items[1].SecondParticipant.Id, u1.Id) require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) + require.Equal(t, topDMs.Items[1].OutgoingMessageCount, int64(1)) require.Equal(t, topDMs.Items[2].SecondParticipant.Id, u2.Id) require.Equal(t, topDMs.Items[2].MessageCount, int64(1)) + require.Equal(t, topDMs.Items[2].OutgoingMessageCount, int64(0)) // this also ensures that u3-u4 conversation doesn't show up in others' top DMs. }) t.Run("topDMs should only consider user's DM channels ", func(t *testing.T) {