[MM-44159] Add user sort by admin status in channels (#20562)
Этот коммит содержится в:
@@ -10719,6 +10719,24 @@ func (s *OpenTracingLayerUserStore) GetProfilesInChannel(options *model.UserGetO
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerUserStore) GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.GetProfilesInChannelByAdmin")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.UserStore.GetProfilesInChannelByAdmin(options)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerUserStore) GetProfilesInChannelByStatus(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.GetProfilesInChannelByStatus")
|
||||
|
||||
@@ -12230,6 +12230,27 @@ func (s *RetryLayerUserStore) GetProfilesInChannel(options *model.UserGetOptions
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerUserStore) GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.UserStore.GetProfilesInChannelByAdmin(options)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerUserStore) GetProfilesInChannelByStatus(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
|
||||
tries := 0
|
||||
|
||||
@@ -771,6 +771,37 @@ func (us SqlUserStore) GetProfilesInChannelByStatus(options *model.UserGetOption
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (us SqlUserStore) GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
query := us.usersQuery.
|
||||
Join("ChannelMembers cm ON ( cm.UserId = u.Id )").
|
||||
Where("cm.ChannelId = ?", options.InChannelId).
|
||||
OrderBy(`cm.SchemeAdmin DESC`).
|
||||
OrderBy("u.Username ASC").
|
||||
Offset(uint64(options.Page * options.PerPage)).Limit(uint64(options.PerPage))
|
||||
|
||||
if options.Inactive && !options.Active {
|
||||
query = query.Where("u.DeleteAt != 0")
|
||||
} else if options.Active && !options.Inactive {
|
||||
query = query.Where("u.DeleteAt = 0")
|
||||
}
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get_profiles_in_channel_by_admin_tosql")
|
||||
}
|
||||
|
||||
users := []*model.User{}
|
||||
if err := us.GetReplicaX().Select(&users, queryString, args...); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Users")
|
||||
}
|
||||
|
||||
for _, u := range users {
|
||||
u.Sanitize(map[string]bool{})
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (us SqlUserStore) GetAllProfilesInChannel(ctx context.Context, channelID string, allowFromCache bool) (map[string]*model.User, error) {
|
||||
query := us.usersQuery.
|
||||
Join("ChannelMembers cm ON ( cm.UserId = u.Id )").
|
||||
|
||||
@@ -407,6 +407,7 @@ type UserStore interface {
|
||||
InvalidateProfilesInChannelCache(channelID string)
|
||||
GetProfilesInChannel(options *model.UserGetOptions) ([]*model.User, error)
|
||||
GetProfilesInChannelByStatus(options *model.UserGetOptions) ([]*model.User, error)
|
||||
GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error)
|
||||
GetAllProfilesInChannel(ctx context.Context, channelID string, allowFromCache bool) (map[string]*model.User, error)
|
||||
GetProfilesNotInChannel(teamID string, channelId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, error)
|
||||
GetProfilesWithoutTeam(options *model.UserGetOptions) ([]*model.User, error)
|
||||
|
||||
@@ -776,6 +776,29 @@ func (_m *UserStore) GetProfilesInChannel(options *model.UserGetOptions) ([]*mod
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetProfilesInChannelByAdmin provides a mock function with given fields: options
|
||||
func (_m *UserStore) GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
ret := _m.Called(options)
|
||||
|
||||
var r0 []*model.User
|
||||
if rf, ok := ret.Get(0).(func(*model.UserGetOptions) []*model.User); ok {
|
||||
r0 = rf(options)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.User)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*model.UserGetOptions) error); ok {
|
||||
r1 = rf(options)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetProfilesInChannelByStatus provides a mock function with given fields: options
|
||||
func (_m *UserStore) GetProfilesInChannelByStatus(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
ret := _m.Called(options)
|
||||
|
||||
@@ -54,6 +54,7 @@ func TestUserStore(t *testing.T, ss store.Store, s SqlStore) {
|
||||
t.Run("GetProfiles", func(t *testing.T) { testUserStoreGetProfiles(t, ss) })
|
||||
t.Run("GetProfilesInChannel", func(t *testing.T) { testUserStoreGetProfilesInChannel(t, ss) })
|
||||
t.Run("GetProfilesInChannelByStatus", func(t *testing.T) { testUserStoreGetProfilesInChannelByStatus(t, ss, s) })
|
||||
t.Run("GetProfilesInChannelByAdmin", func(t *testing.T) { testUserStoreGetProfilesInChannelByAdmin(t, ss, s) })
|
||||
t.Run("GetProfilesWithoutTeam", func(t *testing.T) { testUserStoreGetProfilesWithoutTeam(t, ss) })
|
||||
t.Run("GetAllProfilesInChannel", func(t *testing.T) { testUserStoreGetAllProfilesInChannel(t, ss) })
|
||||
t.Run("GetProfilesNotInChannel", func(t *testing.T) { testUserStoreGetProfilesNotInChannel(t, ss) })
|
||||
@@ -981,6 +982,85 @@ func testUserStoreGetProfilesInChannel(t *testing.T, ss store.Store) {
|
||||
})
|
||||
}
|
||||
|
||||
func testUserStoreGetProfilesInChannelByAdmin(t *testing.T, ss store.Store, s SqlStore) {
|
||||
|
||||
cleanupStatusStore(t, s)
|
||||
|
||||
teamId := model.NewId()
|
||||
|
||||
user1, err := ss.User().Save(&model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: "aaa" + model.NewId(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ss.User().PermanentDelete(user1.Id)) }()
|
||||
_, nErr := ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: user1.Id}, -1)
|
||||
require.NoError(t, nErr)
|
||||
|
||||
user2Admin, err := ss.User().Save(&model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: "bbb" + model.NewId(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ss.User().PermanentDelete(user2Admin.Id)) }()
|
||||
_, nErr = ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: user2Admin.Id}, -1)
|
||||
require.NoError(t, nErr)
|
||||
|
||||
user3, err := ss.User().Save(&model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: "ccc" + model.NewId(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, ss.User().PermanentDelete(user3.Id)) }()
|
||||
_, nErr = ss.Team().SaveMember(&model.TeamMember{TeamId: teamId, UserId: user3.Id}, -1)
|
||||
require.NoError(t, nErr)
|
||||
|
||||
ch1 := &model.Channel{
|
||||
TeamId: teamId,
|
||||
DisplayName: "Profiles in channel by admin",
|
||||
Name: "profiles-" + model.NewId(),
|
||||
Type: model.ChannelTypeOpen,
|
||||
}
|
||||
c1, nErr := ss.Channel().Save(ch1, -1)
|
||||
require.NoError(t, nErr)
|
||||
|
||||
_, nErr = ss.Channel().SaveMember(&model.ChannelMember{
|
||||
ChannelId: c1.Id,
|
||||
UserId: user1.Id,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
})
|
||||
require.NoError(t, nErr)
|
||||
|
||||
_, nErr = ss.Channel().SaveMember(&model.ChannelMember{
|
||||
ChannelId: c1.Id,
|
||||
UserId: user2Admin.Id,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
ExplicitRoles: "channel_admin",
|
||||
})
|
||||
require.NoError(t, nErr)
|
||||
ss.Channel().UpdateMembersRole(c1.Id, []string{user2Admin.Id})
|
||||
|
||||
_, nErr = ss.Channel().SaveMember(&model.ChannelMember{
|
||||
ChannelId: c1.Id,
|
||||
UserId: user3.Id,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
})
|
||||
require.NoError(t, nErr)
|
||||
|
||||
t.Run("get users in admin, offset 0, limit 100", func(t *testing.T) {
|
||||
users, err := ss.User().GetProfilesInChannelByAdmin(&model.UserGetOptions{
|
||||
InChannelId: c1.Id,
|
||||
Page: 0,
|
||||
PerPage: 100,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, users, 3)
|
||||
require.Equal(t, user2Admin.Username, users[0].Username)
|
||||
require.Equal(t, user1.Username, users[1].Username)
|
||||
require.Equal(t, user3.Username, users[2].Username)
|
||||
})
|
||||
}
|
||||
|
||||
func testUserStoreGetProfilesInChannelByStatus(t *testing.T, ss store.Store, s SqlStore) {
|
||||
|
||||
cleanupStatusStore(t, s)
|
||||
|
||||
@@ -9653,6 +9653,22 @@ func (s *TimerLayerUserStore) GetProfilesInChannel(options *model.UserGetOptions
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerUserStore) GetProfilesInChannelByAdmin(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.UserStore.GetProfilesInChannelByAdmin(options)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("UserStore.GetProfilesInChannelByAdmin", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerUserStore) GetProfilesInChannelByStatus(options *model.UserGetOptions) ([]*model.User, error) {
|
||||
start := time.Now()
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user