MM-66424: Improve team filtering in common teams API (#34454) (#34854)

Cherry-pick 6404ab29acc04901c5cb1cf5ad97fc3c0693e2cd into release-10.11
Этот коммит содержится в:
Jesse Hallam
2026-01-06 12:50:30 -04:00
коммит произвёл GitHub
родитель 989f3a36dc
Коммит a07b1d7a8c
6 изменённых файлов: 371 добавлений и 43 удалений

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

@@ -3725,8 +3725,26 @@ func (a *App) getDirectChannel(c request.CTX, userID, otherUserID string) (*mode
return a.Srv().getDirectChannel(c, userID, otherUserID)
}
func (a *App) GetGroupMessageMembersCommonTeams(c request.CTX, channelID string) ([]*model.Team, *model.AppError) {
channel, appErr := a.GetChannel(c, channelID)
// GetGroupMessageMembersCommonTeamsAsUser is a variant of GetGroupMessageMembersCommonTeams
// that returns results relative to the requesting user from the session in the request context.
func (a *App) GetGroupMessageMembersCommonTeamsAsUser(rctx request.CTX, channelID string) ([]*model.Team, *model.AppError) {
return a.getGroupMessageMembersCommonTeams(rctx, rctx.Session().UserId, channelID)
}
// GetGroupMessageMembersCommonTeams returns the set of teams in common for the members of the given GM channel.
//
// Prefer GetGroupMessageMembersCommonTeamsAsUser unless the request context is independent of any given user.
func (a *App) GetGroupMessageMembersCommonTeams(rctx request.CTX, channelID string) ([]*model.Team, *model.AppError) {
return a.getGroupMessageMembersCommonTeams(rctx, "", channelID)
}
// getGroupMessageMembersCommonTeams returns the set of teams common to the members of the given GM channel.
//
// If a requesting user id is specified, but the user isn't an active member of the channel, we return an empty
// set of channels. We don't just exclude all inactive users to offer more flexibility to the remaining users
// on where to create the replacement channel.
func (a *App) getGroupMessageMembersCommonTeams(rctx request.CTX, requestingUserID, channelID string) ([]*model.Team, *model.AppError) {
channel, appErr := a.GetChannel(rctx, channelID)
if appErr != nil {
return nil, appErr
}
@@ -3742,12 +3760,22 @@ func (a *App) GetGroupMessageMembersCommonTeams(c request.CTX, channelID string)
Inactive: false,
Active: true,
})
if appErr != nil {
return nil, appErr
}
userIDs := make([]string, len(users))
for i := 0; i < len(users); i++ {
userIDs[i] = users[i].Id
}
// If a requesting user is specified, but we don't find them above as an active member
// of the channel, just short-circuit and return an empty set. We don't return an error
// as this is a valid result for some callers.
if requestingUserID != "" && !slices.Contains(userIDs, requestingUserID) {
return nil, nil
}
commonTeamIDs, err := a.Srv().Store().Team().GetCommonTeamIDsForMultipleUsers(userIDs)
if err != nil {
return nil, model.NewAppError("GetGroupMessageMembersCommonTeams", "app.channel.get_common_teams.store_get_common_teams_error", nil, "", http.StatusInternalServerError).Wrap(err)

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

@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"sort"
"strings"
"sync"
@@ -2864,59 +2865,156 @@ func TestIsCRTEnabledForUser(t *testing.T) {
func TestGetGroupMessageMembersCommonTeams(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic()
defer th.TearDown()
teamsToCreate := 2
usersToCreate := 4 // at least 3 users to create a GM channel, last user is not in any team
teams := make([]string, 0, teamsToCreate)
for i := 0; i < cap(teams); i++ {
team := th.CreateTeam()
defer func(team *model.Team) {
appErr := th.App.PermanentDeleteTeam(th.Context, team)
require.Nil(t, appErr)
}(team)
teams = append(teams, team.Id)
team1 := th.CreateTeam()
team2 := th.CreateTeam()
user1 := th.CreateUser()
user2 := th.CreateUser()
user3 := th.CreateUser()
user4NotInAnyTeams := th.CreateUser()
unrelatedUser := th.CreateUser()
// All of user1, user2 and user3, and unrelatedUser on team1
th.LinkUserToTeam(user1, team1)
th.LinkUserToTeam(user2, team1)
th.LinkUserToTeam(user3, team1)
th.LinkUserToTeam(unrelatedUser, team1)
// Only user2, user3, and unrelatedUser on team2
th.LinkUserToTeam(user2, team2)
th.LinkUserToTeam(user3, team2)
th.LinkUserToTeam(unrelatedUser, team2)
assertNoTeamsInCommon := func(t *testing.T, commonTeams []*model.Team) {
t.Helper()
assert.Empty(t, commonTeams, "expected no teams in common")
}
users := make([]string, 0, usersToCreate)
for i := 0; i < cap(users); i++ {
user := th.CreateUser()
defer func(user *model.User) {
appErr := th.App.PermanentDeleteUser(th.Context, user)
require.Nil(t, appErr)
}(user)
users = append(users, user.Id)
}
for _, teamId := range teams {
// add first 3 users to each team, last user is not in any team
for i := range 3 {
_, _, appErr := th.App.AddUserToTeam(th.Context, teamId, users[i], "")
require.Nil(t, appErr)
assertTeam1InCommon := func(t *testing.T, commonTeams []*model.Team) {
t.Helper()
if assert.Len(t, commonTeams, 1, "expected 1 team in common") {
assert.Equal(t, team1.Id, commonTeams[0].Id, "expected team1 in common")
}
}
// create GM channel with first 3 users who share common teams
gmChannel, appErr := th.App.createGroupChannel(th.Context, users[:3], users[0])
require.Nil(t, appErr)
require.NotNil(t, gmChannel)
assertTeam1And2InCommon := func(t *testing.T, commonTeams []*model.Team) {
t.Helper()
if assert.Len(t, commonTeams, 2, "expected 2 teams in common") {
assert.True(t, slices.ContainsFunc(commonTeams, func(team *model.Team) bool {
return team.Id == team1.Id
}), "expected team1 in common")
assert.True(t, slices.ContainsFunc(commonTeams, func(team *model.Team) bool {
return team.Id == team2.Id
}), "expected team2 in common")
}
}
// normally you can't create a GM channel with users that don't share any teams, but we do it here to test the edge case
// create GM channel with last 3 users, where last member is not in any team
otherGMChannel, appErr := th.App.createGroupChannel(th.Context, users[1:], users[0])
require.Nil(t, appErr)
require.NotNil(t, otherGMChannel)
t.Run("teams for gm with user1, user2 and user3", func(t *testing.T) {
gmChannel, appErr := th.App.createGroupChannel(th.Context, []string{user1.Id, user2.Id, user3.Id}, user1.Id)
require.Nil(t, appErr)
require.NotNil(t, gmChannel)
t.Run("Get teams for GM channel", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeams(th.Context, gmChannel.Id)
require.Nil(t, appErr)
require.Equal(t, 2, len(commonTeams))
assertTeam1InCommon(t, commonTeams)
t.Run("as user1", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user1.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertTeam1InCommon(t, commonTeams)
})
t.Run("as user2", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user2.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertTeam1InCommon(t, commonTeams)
})
t.Run("as user3", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user3.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertTeam1InCommon(t, commonTeams)
})
t.Run("as unrelatedUser", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: unrelatedUser.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
})
})
t.Run("No common teams", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeams(th.Context, otherGMChannel.Id)
t.Run("teams for gm with user2, user3, and user4NotInAnyTeams", func(t *testing.T) {
gmChannel, appErr := th.App.createGroupChannel(th.Context, []string{user2.Id, user3.Id, user4NotInAnyTeams.Id}, user1.Id)
require.Nil(t, appErr)
require.Equal(t, 0, len(commonTeams))
require.NotNil(t, gmChannel)
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeams(th.Context, gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
t.Run("as user2", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user2.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
})
t.Run("as user3", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user3.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
})
t.Run("as unrelatedUser", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: unrelatedUser.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
})
})
t.Run("teams for gm with user2, user3, and deactivatedUser", func(t *testing.T) {
deactivatedUser := th.CreateUser()
// deactiverUser on team2 only
th.LinkUserToTeam(deactivatedUser, team2)
gmChannel, appErr := th.App.createGroupChannel(th.Context, []string{user2.Id, user3.Id, deactivatedUser.Id}, user1.Id)
require.Nil(t, appErr)
require.NotNil(t, gmChannel)
_, appErr = th.App.UpdateActive(th.Context, deactivatedUser, false)
require.Nil(t, appErr)
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeams(th.Context, gmChannel.Id)
require.Nil(t, appErr)
// By default, we return the teams common only to active users.
assertTeam1And2InCommon(t, commonTeams)
t.Run("as user2", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user2.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertTeam1And2InCommon(t, commonTeams)
})
t.Run("as user3", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: user3.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertTeam1And2InCommon(t, commonTeams)
})
t.Run("as deactivatedUser", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: deactivatedUser.Id}), gmChannel.Id)
require.Nil(t, appErr)
// When requesting as deactivated user in the gm, no teams are considered in common.
assertNoTeamsInCommon(t, commonTeams)
})
t.Run("as unrelatedUser", func(t *testing.T) {
commonTeams, appErr := th.App.GetGroupMessageMembersCommonTeamsAsUser(th.Context.WithSession(&model.Session{UserId: unrelatedUser.Id}), gmChannel.Id)
require.Nil(t, appErr)
assertNoTeamsInCommon(t, commonTeams)
})
})
}