- Use db userId instead of referring to session
 - Filter out bot DM channels, and add relevant test
Этот коммит содержится в:
Shivashis Padhi
2022-07-25 14:07:08 +05:30
родитель 27131f3051
Коммит 5f56d43d80
3 изменённых файлов: 55 добавлений и 18 удалений

Просмотреть файл

@@ -339,7 +339,7 @@ func getTopDMsForUserSince(c *Context, w http.ResponseWriter, r *http.Request) {
startTime := model.StartOfDayForTimeRange(c.Params.TimeRange, user.GetTimezoneLocation()) 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(), StartUnixMilli: startTime.UnixMilli(),
Page: c.Params.Page, Page: c.Params.Page,
PerPage: c.Params.PerPage, PerPage: c.Params.PerPage,

Просмотреть файл

@@ -824,36 +824,58 @@ func TestGetTopDMsForUserSince(t *testing.T) {
th.ConfigStore.SetReadOnlyFF(false) th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true) defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = 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)) th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional))
// basicuser1 - bu1, basicuser - bu // 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 basicUser := th.BasicUser
basicUser1 := th.BasicUser2 basicUser1 := th.BasicUser2
th.LoginBasic2() th.LoginBasic2()
client := th.Client client := th.Client
channelBu1, _, err := client.CreateDirectChannel(basicUser1.Id, basicUser1.Id) channelBu1Bu1, _, err := client.CreateDirectChannel(basicUser1.Id, basicUser1.Id)
require.NoError(t, err) require.NoError(t, err)
th.LoginBasic() th.LoginBasic()
client = th.Client client = th.Client
channelBu, _, err := client.CreateDirectChannel(basicUser.Id, basicUser.Id) channelBuBu, _, err := client.CreateDirectChannel(basicUser.Id, basicUser.Id)
require.NoError(t, err) 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) require.NoError(t, err)
// create 2 posts in channelBu, 1 in channelBu1, 3 in channelBu12 // create 2 posts in channelBu, 1 in channelBu1, 3 in channelBu12
postsGenConfig := []map[string]interface{}{ postsGenConfig := []map[string]interface{}{
{ {
"chId": channelBu.Id, "chId": channelBuBu.Id,
"postCount": 2, "postCount": 2,
}, },
{ {
"chId": channelBu1.Id, "chId": channelBu1Bu1.Id,
"postCount": 1, "postCount": 1,
}, },
{ {
"chId": channelBu12.Id, "chId": channelBuBu1.Id,
"postCount": 3,
},
{
"chId": channelBuBot.Id,
"postCount": 3, "postCount": 3,
}, },
} }
@@ -861,7 +883,7 @@ func TestGetTopDMsForUserSince(t *testing.T) {
for _, postGen := range postsGenConfig { for _, postGen := range postsGenConfig {
postCount := postGen["postCount"].(int) postCount := postGen["postCount"].(int)
for i := 0; i < postCount; i++ { for i := 0; i < postCount; i++ {
if postGen["chId"] == channelBu1.Id { if postGen["chId"] == channelBu1Bu1.Id {
th.LoginBasic2() th.LoginBasic2()
client = th.Client client = th.Client
userId := basicUser1.Id userId := basicUser1.Id

Просмотреть файл

@@ -3003,26 +3003,41 @@ func (s *SqlPostStore) GetTopDMsForUserSince(userID string, since int64, offset
} }
// fill SecondParticipant column // 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 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 { for _, topDM := range topDMs {
participants := strings.Split(topDM.Participants, ",") participants := strings.Split(topDM.Participants, ",")
if len(participants) == 1 { if len(participants) == 1 {
// chatting to self // chatting to self
topDM.SecondParticipant = userID topDM.SecondParticipant = userID
continue
} else { } else {
// divide message count by 2, because it's counted twice due to channel memberships being 2 for dms. // divide message count by 2, because it's counted twice due to channel memberships being 2 for dms.
topDM.MessageCount = topDM.MessageCount / 2 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] topDMsFiltered = append(topDMsFiltered, topDM)
} else {
topDM.SecondParticipant = participants[0]
}
} }
return topDMs return topDMsFiltered, nil
} }