From 4ffdf6b859aa2eab80c672e8a51c88a27099a817 Mon Sep 17 00:00:00 2001 From: Shivashis Padhi Date: Thu, 8 Sep 2022 02:04:01 +0530 Subject: [PATCH] [MM-46667] P1 Fix top DM insights pagination (#20896) Automatic Merge --- api4/insights_test.go | 9 ++++++++- store/sqlstore/post_store.go | 37 ++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/api4/insights_test.go b/api4/insights_test.go index 97e3ddf6d6..45d03bf367 100644 --- a/api4/insights_test.go +++ b/api4/insights_test.go @@ -957,7 +957,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { }, { "chId": channelBuBot.Id, - "postCount": 3, + "postCount": 4, }, } @@ -991,6 +991,13 @@ func TestGetTopDMsForUserSince(t *testing.T) { require.Len(t, topDMs.Items, 1) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) require.Equal(t, topDMs.Items[0].SecondParticipant.Id, basicUser1.Id) + + // test pagination + topDMsPage0PerPage1, _, topDmsErr := client.GetTopDMsForUserSince("today", 0, 2) + require.NoError(t, topDmsErr) + require.Len(t, topDMsPage0PerPage1.Items, 1) + require.Equal(t, topDMsPage0PerPage1.HasNext, false) + require.Equal(t, topDMsPage0PerPage1.Items[0].SecondParticipant.Id, basicUser1.Id) }) // get top dms for bu1 diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index eaa8fb8332..6fb67ec1de 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -2996,10 +2996,27 @@ func (s *SqlPostStore) updateThreadsFromPosts(transaction *sqlxTxWrapper, posts } func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset int, limit int) (*model.TopDMList, error) { + var botsFilterExpr, stringSplitKeyword string + if s.DriverName() == model.DatabaseDriverPostgres { + stringSplitKeyword = "split_part" + } else if s.DriverName() == model.DatabaseDriverMysql { + stringSplitKeyword = "SUBSTRING_INDEX" + } + + /* + Channel.Name is of the format userId1__userId2. + Using this, self dms, and bot dms can be filtered. + */ + botsFilterExpr = fmt.Sprintf(` + %s(Channels.Name, '__', 1) NOT IN (SELECT UserId FROM Bots) + AND %s(Channels.Name, '__', 2) NOT IN (SELECT UserId FROM Bots) + `, stringSplitKeyword, stringSplitKeyword) channelSelector := s.getQueryBuilder().Select("Id", "TotalMsgCount").From("Channels").Join("ChannelMembers as cm on cm.ChannelId = Channels.Id"). Where(sq.And{ sq.Expr("Channels.Type = 'D'"), sq.Eq{"cm.UserId": userID}, + sq.NotEq{"Channels.Name": fmt.Sprintf("%s__%s", userID, userID)}, + sq.Expr(botsFilterExpr), }) var aggregator string @@ -3050,17 +3067,12 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM, si for _, topDM := range topDMs { participants := strings.Split(topDM.Participants, ",") var secondParticipantId string - if len(participants) == 1 { - // channel with self - secondParticipantId = "-1" + // divide message count by 2, because it's counted twice due to channel memberships being 2 for dms. + topDM.MessageCount = topDM.MessageCount / 2 + if participants[0] == userID { + secondParticipantId = participants[1] } else { - // divide message count by 2, because it's counted twice due to channel memberships being 2 for dms. - topDM.MessageCount = topDM.MessageCount / 2 - if participants[0] == userID { - secondParticipantId = participants[1] - } else { - secondParticipantId = participants[0] - } + secondParticipantId = participants[0] } secondParticipantIds = append(secondParticipantIds, secondParticipantId) channelIds = append(channelIds, topDM.ChannelId) @@ -3114,12 +3126,9 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM, si for index, topDM := range topDMs { if secondParticipantIds[index] == "-1" { - continue + return nil, errors.Wrapf(err, "failed to find second user for topDM: %s", userID) } user := usersMap[secondParticipantIds[index]] - if user.IsBot { - continue - } topDM.SecondParticipant = &model.TopDMInsightUserInformation{ InsightUserInformation: model.InsightUserInformation{ Id: user.Id,