diff --git a/api4/insights_test.go b/api4/insights_test.go index 235fad247b..8cbe7a40e1 100644 --- a/api4/insights_test.go +++ b/api4/insights_test.go @@ -912,6 +912,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { require.Len(t, topDMs.Items, 2) require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) + require.Equal(t, topDMs.Items[0].SecondParticipant.Id, basicUser1.Id) }) // get top dms for bu1 diff --git a/app/post_test.go b/app/post_test.go index 1f611f8287..f8b12ed746 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -3145,11 +3145,11 @@ func TestGetTopDMsForUserSince(t *testing.T) { require.Len(t, topDMs.Items, 3) // check order, magnitude of items // fmt.Println(topDMs.Items[0].MessageCount, topDMs.Items[1].MessageCount, topDMs.Items[2].MessageCount) - require.Equal(t, topDMs.Items[0].SecondParticipant, u3.Id) + require.Equal(t, topDMs.Items[0].SecondParticipant.Id, u3.Id) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) - require.Equal(t, topDMs.Items[1].SecondParticipant, u1.Id) + require.Equal(t, topDMs.Items[1].SecondParticipant.Id, u1.Id) require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) - require.Equal(t, topDMs.Items[2].SecondParticipant, u2.Id) + require.Equal(t, topDMs.Items[2].SecondParticipant.Id, u2.Id) require.Equal(t, topDMs.Items[2].MessageCount, int64(1)) // this also ensures that u3-u4 conversation doesn't show up in others' top DMs. }) @@ -3160,7 +3160,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { // len of topDMs.Items should be 3 require.Len(t, topDMs.Items, 1) // check order, magnitude of items - require.Equal(t, topDMs.Items[0].SecondParticipant, u3.Id) + require.Equal(t, topDMs.Items[0].SecondParticipant.Id, u3.Id) require.Equal(t, topDMs.Items[0].MessageCount, int64(4)) }) } diff --git a/model/insights.go b/model/insights.go index 76667e8916..85a8f29f31 100644 --- a/model/insights.go +++ b/model/insights.go @@ -90,6 +90,11 @@ type InsightUserInformation struct { Username string `json:"username"` } +type TopDMInsightUserInformation struct { + InsightUserInformation + Position string `json:"position"` +} + type DurationPostCount struct { ChannelID string `db:"channelid"` // Duration is an ISO8601 date string representing either a day or a day and hour (ex. "2022-05-26" or "2022-05-26T14"). @@ -99,9 +104,9 @@ type DurationPostCount struct { // Top DMs type TopDM struct { - MessageCount int64 `json:"post_count"` - Participants string `json:"-"` - SecondParticipant string `json:"second_participant"` + MessageCount int64 `json:"post_count"` + Participants string `json:"-"` + SecondParticipant *TopDMInsightUserInformation `json:"second_participant"` } type TopDMList struct { diff --git a/model/insights_test.go b/model/insights_test.go index c56fb0c980..5505cc1bc8 100644 --- a/model/insights_test.go +++ b/model/insights_test.go @@ -123,13 +123,13 @@ func TestGetTopThreadListWithPagination(t *testing.T) { func TestGetTopDMsListWithPagination(t *testing.T) { dms := []*TopDM{ - {SecondParticipant: NewId(), MessageCount: 100}, - {SecondParticipant: NewId(), MessageCount: 80}, - {SecondParticipant: NewId(), MessageCount: 90}, - {SecondParticipant: NewId(), MessageCount: 76}, - {SecondParticipant: NewId(), MessageCount: 43}, - {SecondParticipant: NewId(), MessageCount: 2}, - {SecondParticipant: NewId(), MessageCount: 1}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 100}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 80}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 90}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 76}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 43}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 2}, + {SecondParticipant: &TopDMInsightUserInformation{InsightUserInformation: InsightUserInformation{Id: NewId()}}, MessageCount: 1}, } hasNextTT := []struct { Description string diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index d98a940896..5d423efc5d 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -3014,27 +3014,40 @@ func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([ var topDMsFiltered = []*model.TopDM{} for _, topDM := range topDMs { participants := strings.Split(topDM.Participants, ",") + var secondParticipantId string if len(participants) == 1 { // chatting to self - topDM.SecondParticipant = userID + secondParticipantId = userID } 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 { - topDM.SecondParticipant = participants[1] + secondParticipantId = participants[1] } else { - topDM.SecondParticipant = participants[0] + secondParticipantId = participants[0] } // filter topDM out if second user is bot - users, err := s.User().GetProfileByIds(context.Background(), []string{topDM.SecondParticipant}, &store.UserGetByIdsOpts{}, true) + users, err := s.User().GetProfileByIds(context.Background(), []string{secondParticipantId}, &store.UserGetByIdsOpts{}, true) if err != nil { return nil, errors.Wrapf(err, "failed to get second participant information for user-id: %s", topDM.SecondParticipant) } if users[0].IsBot { continue } + user := users[0] + topDM.SecondParticipant = &model.TopDMInsightUserInformation{ + InsightUserInformation: model.InsightUserInformation{ + Id: user.Id, + LastPictureUpdate: user.LastPictureUpdate, + FirstName: user.FirstName, + LastName: user.LastName, + Username: user.Username, + NickName: user.Nickname, + }, + Position: user.Position, + } } topDMsFiltered = append(topDMsFiltered, topDM) diff --git a/store/storetest/post_store.go b/store/storetest/post_store.go index 9b00b05994..7308272804 100644 --- a/store/storetest/post_store.go +++ b/store/storetest/post_store.go @@ -4073,11 +4073,11 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { // len of topDMs.Items should be 3 require.Len(t, topDMs.Items, 3) // check order, magnitude of items - require.Equal(t, topDMs.Items[0].SecondParticipant, u3.Id) + require.Equal(t, topDMs.Items[0].SecondParticipant.Id, u3.Id) require.Equal(t, topDMs.Items[0].MessageCount, int64(3)) - require.Equal(t, topDMs.Items[1].SecondParticipant, u1.Id) + require.Equal(t, topDMs.Items[1].SecondParticipant.Id, u1.Id) require.Equal(t, topDMs.Items[1].MessageCount, int64(2)) - require.Equal(t, topDMs.Items[2].SecondParticipant, u2.Id) + require.Equal(t, topDMs.Items[2].SecondParticipant.Id, u2.Id) require.Equal(t, topDMs.Items[2].MessageCount, int64(1)) // this also ensures that u3-u4 conversation doesn't show up in others' top DMs. }) @@ -4088,7 +4088,7 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { // len of topDMs.Items should be 3 require.Len(t, topDMs.Items, 1) // check order, magnitude of items - require.Equal(t, topDMs.Items[0].SecondParticipant, u3.Id) + 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) { @@ -4107,7 +4107,7 @@ func testGetTopDMsForUserSince(t *testing.T, ss store.Store, s SqlStore) { // 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, user.Id) + require.Equal(t, topDMs.Items[2].SecondParticipant.Id, user.Id) require.Equal(t, topDMs.Items[2].MessageCount, int64(1)) }) }