diff --git a/api4/insights.go b/api4/insights.go index db2bda4e36..e8302040d3 100644 --- a/api4/insights.go +++ b/api4/insights.go @@ -339,7 +339,7 @@ func getTopDMsForUserSince(c *Context, w http.ResponseWriter, r *http.Request) { startTime := model.StartOfDayForTimeRange(c.Params.TimeRange, user.GetTimezoneLocation()) - topDMs, err := c.App.GetTopDMsForUserSince(c.AppContext.Session().UserId, &model.InsightsOpts{ + topDMs, err := c.App.GetTopDMsForUserSince(user.Id, &model.InsightsOpts{ StartUnixMilli: startTime.UnixMilli(), Page: c.Params.Page, PerPage: c.Params.PerPage, diff --git a/api4/insights_test.go b/api4/insights_test.go index f5245e5a88..1f03aaa680 100644 --- a/api4/insights_test.go +++ b/api4/insights_test.go @@ -824,36 +824,58 @@ func TestGetTopDMsForUserSince(t *testing.T) { th.ConfigStore.SetReadOnlyFF(false) defer th.ConfigStore.SetReadOnlyFF(true) th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true }) + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableBotAccountCreation = true }) th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional)) + // basicuser1 - bu1, basicuser - bu - // create dm channels for bu-bu, bu1-bu1, bu-bu1 + // create dm channels for bu-bu, bu1-bu1, bu-bu1, bot-bu basicUser := th.BasicUser basicUser1 := th.BasicUser2 th.LoginBasic2() client := th.Client - channelBu1, _, err := client.CreateDirectChannel(basicUser1.Id, basicUser1.Id) + channelBu1Bu1, _, err := client.CreateDirectChannel(basicUser1.Id, basicUser1.Id) require.NoError(t, err) th.LoginBasic() client = th.Client - channelBu, _, err := client.CreateDirectChannel(basicUser.Id, basicUser.Id) + channelBuBu, _, err := client.CreateDirectChannel(basicUser.Id, basicUser.Id) require.NoError(t, err) - channelBu12, _, err := client.CreateDirectChannel(basicUser.Id, basicUser1.Id) + channelBuBu1, _, err := client.CreateDirectChannel(basicUser.Id, basicUser1.Id) + require.NoError(t, err) + + // bot creation with permission + th.AddPermissionToRole(model.PermissionCreateBot.Id, model.TeamUserRoleId) + th.App.UpdateUserRoles(th.BasicUser.Id, model.TeamUserRoleId+" "+model.SystemUserRoleId, false) + bot := &model.Bot{ + Username: GenerateTestUsername(), + DisplayName: "a bot", + Description: "bot", + } + + createdBot, resp, err := th.Client.CreateBot(bot) + require.NoError(t, err) + CheckCreatedStatus(t, resp) + defer th.App.PermanentDeleteBot(createdBot.UserId) + channelBuBot, _, err := client.CreateDirectChannel(basicUser.Id, createdBot.UserId) require.NoError(t, err) // create 2 posts in channelBu, 1 in channelBu1, 3 in channelBu12 postsGenConfig := []map[string]interface{}{ { - "chId": channelBu.Id, + "chId": channelBuBu.Id, "postCount": 2, }, { - "chId": channelBu1.Id, + "chId": channelBu1Bu1.Id, "postCount": 1, }, { - "chId": channelBu12.Id, + "chId": channelBuBu1.Id, + "postCount": 3, + }, + { + "chId": channelBuBot.Id, "postCount": 3, }, } @@ -861,7 +883,7 @@ func TestGetTopDMsForUserSince(t *testing.T) { for _, postGen := range postsGenConfig { postCount := postGen["postCount"].(int) for i := 0; i < postCount; i++ { - if postGen["chId"] == channelBu1.Id { + if postGen["chId"] == channelBu1Bu1.Id { th.LoginBasic2() client = th.Client userId := basicUser1.Id diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index ab96f31fd1..7fe22ee1fc 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -3003,26 +3003,41 @@ func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset } // fill SecondParticipant column - topDMs = postProcessTopDMs(userID, topDMs) + topDMs, err = postProcessTopDMs(s, userID, topDMs) + if err != nil { + return nil, err + } return model.GetTopDMListWithPagination(topDMs, limit), nil } -func postProcessTopDMs(userID string, topDMs []*model.TopDM) []*model.TopDM { +func postProcessTopDMs(s *SqlPostStore, userID string, topDMs []*model.TopDM) ([]*model.TopDM, error) { + var topDMsFiltered = []*model.TopDM{} for _, topDM := range topDMs { participants := strings.Split(topDM.Participants, ",") if len(participants) == 1 { // chatting to self topDM.SecondParticipant = 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 + + if participants[0] == userID { + topDM.SecondParticipant = participants[1] + } else { + topDM.SecondParticipant = participants[0] + } + + // filter topDM out if second user is bot + users, err := s.User().GetProfileByIds(context.Background(), []string{topDM.SecondParticipant}, &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 + } } - if participants[0] == userID { - topDM.SecondParticipant = participants[1] - } else { - topDM.SecondParticipant = participants[0] - } + + topDMsFiltered = append(topDMsFiltered, topDM) } - return topDMs + return topDMsFiltered, nil }