MM-35392 Load thread unreads for other teams on app load (#17944)

* Add ability to include thread unreads in team unreads api response

* Do not include GMs/DMs in team unreads for threads

* Fix bad merge
Этот коммит содержится в:
Joram Wilander
2021-07-22 10:24:20 -04:00
коммит произвёл GitHub
родитель 04ef406bd6
Коммит a0cc420e2a
11 изменённых файлов: 176 добавлений и 130 удалений

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

@@ -420,8 +420,9 @@ func getTeamsUnreadForUser(c *Context, w http.ResponseWriter, r *http.Request) {
// optional team id to be excluded from the result
teamId := r.URL.Query().Get("exclude_team")
includeCollapsedThreads := r.URL.Query().Get("include_collapsed_threads") == "true"
unreadTeamsList, err := c.App.GetTeamsUnreadForUser(teamId, c.Params.UserId)
unreadTeamsList, err := c.App.GetTeamsUnreadForUser(teamId, c.Params.UserId, includeCollapsedThreads)
if err != nil {
c.Err = err
return

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

@@ -2651,22 +2651,22 @@ func TestGetMyTeamsUnread(t *testing.T) {
user := th.BasicUser
Client.Login(user.Email, user.Password)
teams, resp := Client.GetTeamsUnreadForUser(user.Id, "")
teams, resp := Client.GetTeamsUnreadForUser(user.Id, "", true)
CheckNoError(t, resp)
require.NotEqual(t, len(teams), 0, "should have results")
teams, resp = Client.GetTeamsUnreadForUser(user.Id, th.BasicTeam.Id)
teams, resp = Client.GetTeamsUnreadForUser(user.Id, th.BasicTeam.Id, true)
CheckNoError(t, resp)
require.Empty(t, teams, "should not have results")
_, resp = Client.GetTeamsUnreadForUser("fail", "")
_, resp = Client.GetTeamsUnreadForUser("fail", "", true)
CheckBadRequestStatus(t, resp)
_, resp = Client.GetTeamsUnreadForUser(model.NewId(), "")
_, resp = Client.GetTeamsUnreadForUser(model.NewId(), "", true)
CheckForbiddenStatus(t, resp)
Client.Logout()
_, resp = Client.GetTeamsUnreadForUser(user.Id, "")
_, resp = Client.GetTeamsUnreadForUser(user.Id, "", true)
CheckUnauthorizedStatus(t, resp)
}

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

@@ -5551,7 +5551,7 @@ func TestGetThreadsForUser(t *testing.T) {
require.Nil(t, resp.Error)
require.Len(t, uss.Threads, 10)
require.Equal(t, uss.Threads[0].PostId, rootIdBefore)
require.Equal(t, rootIdBefore, uss.Threads[0].PostId)
uss2, resp2 := th.Client.GetUserThreads(th.BasicUser.Id, th.BasicTeam.Id, model.GetUserThreadsOpts{
Deleted: false,
@@ -5561,7 +5561,7 @@ func TestGetThreadsForUser(t *testing.T) {
require.Nil(t, resp2.Error)
require.Len(t, uss2.Threads, 10)
require.Equal(t, uss2.Threads[0].PostId, rootIdAfter)
require.Equal(t, rootIdAfter, uss2.Threads[0].PostId)
uss3, resp3 := th.Client.GetUserThreads(th.BasicUser.Id, th.BasicTeam.Id, model.GetUserThreadsOpts{
Deleted: false,

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

@@ -762,7 +762,7 @@ type AppIface interface {
GetTeamsForScheme(scheme *model.Scheme, offset int, limit int) ([]*model.Team, *model.AppError)
GetTeamsForSchemePage(scheme *model.Scheme, page int, perPage int) ([]*model.Team, *model.AppError)
GetTeamsForUser(userID string) ([]*model.Team, *model.AppError)
GetTeamsUnreadForUser(excludeTeamId string, userID string) ([]*model.TeamUnread, *model.AppError)
GetTeamsUnreadForUser(excludeTeamId string, userID string, includeCollapsedThreads bool) ([]*model.TeamUnread, *model.AppError)
GetTermsOfService(id string) (*model.TermsOfService, *model.AppError)
GetThreadForUser(teamID string, threadMembership *model.ThreadMembership, extended bool) (*model.ThreadResponse, *model.AppError)
GetThreadMembershipForUser(userId, threadId string) (*model.ThreadMembership, *model.AppError)

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

@@ -9250,7 +9250,7 @@ func (a *OpenTracingAppLayer) GetTeamsForUser(userID string) ([]*model.Team, *mo
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetTeamsUnreadForUser(excludeTeamId string, userID string) ([]*model.TeamUnread, *model.AppError) {
func (a *OpenTracingAppLayer) GetTeamsUnreadForUser(excludeTeamId string, userID string, includeCollapsedThreads bool) ([]*model.TeamUnread, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetTeamsUnreadForUser")
@@ -9262,7 +9262,7 @@ func (a *OpenTracingAppLayer) GetTeamsUnreadForUser(excludeTeamId string, userID
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetTeamsUnreadForUser(excludeTeamId, userID)
resultVar0, resultVar1 := a.app.GetTeamsUnreadForUser(excludeTeamId, userID, includeCollapsedThreads)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))

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

@@ -183,7 +183,7 @@ func (api *PluginAPI) GetTeamByName(name string) (*model.Team, *model.AppError)
}
func (api *PluginAPI) GetTeamsUnreadForUser(userID string) ([]*model.TeamUnread, *model.AppError) {
return api.app.GetTeamsUnreadForUser("", userID)
return api.app.GetTeamsUnreadForUser("", userID, false)
}
func (api *PluginAPI) UpdateTeam(team *model.Team) (*model.Team, *model.AppError) {

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

@@ -1654,7 +1654,7 @@ func (a *App) FindTeamByName(name string) bool {
return true
}
func (a *App) GetTeamsUnreadForUser(excludeTeamId string, userID string) ([]*model.TeamUnread, *model.AppError) {
func (a *App) GetTeamsUnreadForUser(excludeTeamId string, userID string, includeCollapsedThreads bool) ([]*model.TeamUnread, *model.AppError) {
data, err := a.Srv().Store.Team().GetChannelUnreadsForAllTeams(excludeTeamId, userID)
if err != nil {
return nil, model.NewAppError("GetTeamsUnreadForUser", "app.team.get_unread.app_error", nil, err.Error(), http.StatusInternalServerError)
@@ -1685,13 +1685,25 @@ func (a *App) GetTeamsUnreadForUser(excludeTeamId string, userID string) ([]*mod
MentionCount: 0,
MentionCountRoot: 0,
MsgCountRoot: 0,
ThreadCount: 0,
ThreadMentionCount: 0,
TeamId: id,
})
}
}
for _, val := range membersMap {
members = append(members, val)
includeCollapsedThreads = includeCollapsedThreads && *a.Config().ServiceSettings.CollapsedThreads != model.COLLAPSED_THREADS_DISABLED
for _, member := range membersMap {
if includeCollapsedThreads {
data, err := a.Srv().Store.Thread().GetThreadsForUser(userID, member.TeamId, model.GetUserThreadsOpts{TotalsOnly: true, TeamOnly: true})
if err != nil {
return nil, model.NewAppError("GetTeamsUnreadForUser", "app.team.get_unread.app_error", nil, err.Error(), http.StatusInternalServerError)
}
member.ThreadCount = data.TotalUnreadThreads
member.ThreadMentionCount = data.TotalUnreadMentions
}
members = append(members, member)
}
return members, nil

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

@@ -1445,14 +1445,20 @@ func (c *Client4) AttachDeviceId(deviceId string) (bool, *Response) {
// GetTeamsUnreadForUser will return an array with TeamUnread objects that contain the amount
// of unread messages and mentions the current user has for the teams it belongs to.
// An optional team ID can be set to exclude that team from the results. Must be authenticated.
func (c *Client4) GetTeamsUnreadForUser(userId, teamIdToExclude string) ([]*TeamUnread, *Response) {
var optional string
// An optional team ID can be set to exclude that team from the results.
// An optional boolean can be set to include collapsed thread unreads. Must be authenticated.
func (c *Client4) GetTeamsUnreadForUser(userId, teamIdToExclude string, includeCollapsedThreads bool) ([]*TeamUnread, *Response) {
query := url.Values{}
if teamIdToExclude != "" {
optional += fmt.Sprintf("?exclude_team=%s", url.QueryEscape(teamIdToExclude))
query.Set("exclude_team", teamIdToExclude)
}
r, err := c.DoApiGet(c.GetUserRoute(userId)+"/teams/unread"+optional, "")
if includeCollapsedThreads {
query.Set("include_collapsed_threads", "true")
}
r, err := c.DoApiGet(c.GetUserRoute(userId)+"/teams/unread?"+query.Encode(), "")
if err != nil {
return nil, BuildErrorResponse(r, err)
}

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

@@ -36,6 +36,8 @@ type TeamUnread struct {
MentionCount int64 `json:"mention_count"`
MentionCountRoot int64 `json:"mention_count_root"`
MsgCountRoot int64 `json:"msg_count_root"`
ThreadCount int64 `json:"thread_count"`
ThreadMentionCount int64 `json:"thread_mention_count"`
}
//msgp:ignore TeamMemberForExport

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

@@ -54,6 +54,12 @@ type GetUserThreadsOpts struct {
// Unread will make sure that only threads with unread replies are returned
Unread bool
// TotalsOnly will not fetch any threads and just fetch the total counts
TotalsOnly bool
// TeamOnly will only fetch threads and unreads for the specified team and excludes DMs/GMs
TeamOnly bool
}
func (o *ThreadResponse) ToJson() string {

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

@@ -131,10 +131,20 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
}
fetchConditions := sq.And{
sq.Or{sq.Eq{"Channels.TeamId": teamId}, sq.Eq{"Channels.TeamId": ""}},
sq.Eq{"ThreadMemberships.UserId": userId},
sq.Eq{"ThreadMemberships.Following": true},
}
if opts.TeamOnly {
fetchConditions = sq.And{
sq.Eq{"Channels.TeamId": teamId},
fetchConditions,
}
} else {
fetchConditions = sq.And{
sq.Or{sq.Eq{"Channels.TeamId": teamId}, sq.Eq{"Channels.TeamId": ""}},
fetchConditions,
}
}
if !opts.Deleted {
fetchConditions = sq.And{
fetchConditions,
@@ -150,7 +160,11 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
totalUnreadThreadsChan := make(chan store.StoreResult, 1)
totalCountChan := make(chan store.StoreResult, 1)
totalUnreadMentionsChan := make(chan store.StoreResult, 1)
threadsChan := make(chan store.StoreResult, 1)
var threadsChan chan store.StoreResult
if !opts.TotalsOnly {
threadsChan = make(chan store.StoreResult, 1)
}
go func() {
repliesQuery, repliesQueryArgs, _ := s.getQueryBuilder().
Select("COUNT(DISTINCT(Posts.RootId))").
@@ -195,6 +209,8 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
totalUnreadMentionsChan <- store.StoreResult{Data: totalUnreadMentions, NErr: err}
close(totalUnreadMentionsChan)
}()
if !opts.TotalsOnly {
go func() {
newFetchConditions := fetchConditions
if opts.Since > 0 {
@@ -254,12 +270,7 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
threadsChan <- store.StoreResult{Data: threads, NErr: err}
close(threadsChan)
}()
threadsResult := <-threadsChan
if threadsResult.NErr != nil {
return nil, threadsResult.NErr
}
threads := threadsResult.Data.([]*JoinedThread)
totalUnreadMentionsResult := <-totalUnreadMentionsChan
if totalUnreadMentionsResult.NErr != nil {
@@ -281,6 +292,20 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
var userIds []string
userIdMap := map[string]bool{}
result := &model.Threads{
Total: totalCount,
Threads: []*model.ThreadResponse{},
TotalUnreadMentions: totalUnreadMentions,
TotalUnreadThreads: totalUnreadThreads,
}
if !opts.TotalsOnly {
threadsResult := <-threadsChan
if threadsResult.NErr != nil {
return nil, threadsResult.NErr
}
threads := threadsResult.Data.([]*JoinedThread)
for _, thread := range threads {
for _, participantId := range thread.Participants {
if _, ok := userIdMap[participantId]; !ok {
@@ -302,13 +327,6 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
}
}
result := &model.Threads{
Total: totalCount,
Threads: []*model.ThreadResponse{},
TotalUnreadMentions: totalUnreadMentions,
TotalUnreadThreads: totalUnreadThreads,
}
for _, thread := range threads {
var participants []*model.User
for _, participantId := range thread.Participants {
@@ -335,6 +353,7 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
Post: thread.Post.ToNilIfInvalid(),
})
}
}
return result, nil
}