From f007d941555348988baea9c52a66c82a0919db96 Mon Sep 17 00:00:00 2001 From: Shivashis Padhi Date: Thu, 28 Jul 2022 12:46:18 +0530 Subject: [PATCH] Ignore self DMs --- api4/insights_test.go | 10 ++++------ store/sqlstore/post_store.go | 2 +- store/storetest/post_store.go | 7 ++----- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/api4/insights_test.go b/api4/insights_test.go index 8cbe7a40e1..5830762368 100644 --- a/api4/insights_test.go +++ b/api4/insights_test.go @@ -909,8 +909,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { client = th.Client topDMs, _, topDmsErr := client.GetTopDMsForUserSince("today", 0, 100) require.NoError(t, topDmsErr) - require.Len(t, topDMs.Items, 2) - require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) + 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) }) @@ -921,8 +920,8 @@ func TestGetTopDMsForUserSince(t *testing.T) { client = th.Client topDMs, _, topDmsErr := client.GetTopDMsForUserSince("today", 0, 100) require.NoError(t, topDmsErr) - require.Len(t, topDMs.Items, 2) - require.Equal(t, topDMs.Items[1].MessageCount, int64(1)) + require.Len(t, topDMs.Items, 1) + require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) }) // deactivate basicuser1 _, err = th.Client.DeleteUser(basicUser1.Id) @@ -933,8 +932,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { client = th.Client topDMs, _, topDmsErr := client.GetTopDMsForUserSince("today", 0, 100) require.NoError(t, topDmsErr) - require.Len(t, topDMs.Items, 2) - require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) + require.Len(t, topDMs.Items, 1) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) }) } diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 5a746ae558..b1fb82aeb2 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -3017,7 +3017,7 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([ var secondParticipantId string if len(participants) == 1 { // chatting to self - secondParticipantId = userID + continue } else { // divide message count by 2, because it's counted twice due to channel memberships being 2 for dms. topDM.MessageCount = topDM.MessageCount / 2 diff --git a/store/storetest/post_store.go b/store/storetest/post_store.go index 7308272804..aca7a8f76e 100644 --- a/store/storetest/post_store.go +++ b/store/storetest/post_store.go @@ -4091,7 +4091,7 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { require.Equal(t, topDMs.Items[0].SecondParticipant.Id, u3.Id) require.Equal(t, topDMs.Items[0].MessageCount, int64(4)) }) - t.Run("topDMs will consider self dms", func(t *testing.T) { + t.Run("topDMs will not consider self dms", func(t *testing.T) { chUser, nErr := ss.Channel().CreateDirectChannel(&user, &user) require.NoError(t, nErr) _, err = ss.Post().Save(&model.Post{ @@ -4105,9 +4105,6 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { topDMs, err := ss.Post().GetTopDMsForUserSince(user.Id, 100, 0, 100) require.NoError(t, err) // len of topDMs.Items should be 3 - require.Len(t, topDMs.Items, 3) - // check order, magnitude of items - require.Equal(t, topDMs.Items[2].SecondParticipant.Id, user.Id) - require.Equal(t, topDMs.Items[2].MessageCount, int64(1)) + require.Len(t, topDMs.Items, 2) }) }