MM-16224: Adds new API endpoint + (App & Client & Store) to retrieve the difference between the set of channel members and given group members. (#11186)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4d223ba3a2
Коммит
e15a75a2ec
@@ -4,7 +4,9 @@
|
||||
package api4
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
@@ -37,6 +39,7 @@ func (api *API) InitChannel() {
|
||||
api.BaseRoutes.Channel.Handle("/stats", api.ApiSessionRequired(getChannelStats)).Methods("GET")
|
||||
api.BaseRoutes.Channel.Handle("/pinned", api.ApiSessionRequired(getPinnedPosts)).Methods("GET")
|
||||
api.BaseRoutes.Channel.Handle("/timezones", api.ApiSessionRequired(getChannelMembersTimezones)).Methods("GET")
|
||||
api.BaseRoutes.Channel.Handle("/members_minus_group_members", api.ApiSessionRequired(channelMembersMinusGroupMembers)).Methods("GET")
|
||||
api.BaseRoutes.ChannelForUser.Handle("/unread", api.ApiSessionRequired(getChannelUnread)).Methods("GET")
|
||||
|
||||
api.BaseRoutes.ChannelByName.Handle("", api.ApiSessionRequired(getChannelByName)).Methods("GET")
|
||||
@@ -1295,3 +1298,53 @@ func updateChannelScheme(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func channelMembersMinusGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireChannelId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
groupIDsParam := groupIDsQueryParamRegex.ReplaceAllString(c.Params.GroupIDs, "")
|
||||
|
||||
if len(groupIDsParam) < 26 {
|
||||
c.SetInvalidParam("group_ids")
|
||||
return
|
||||
}
|
||||
|
||||
groupIDs := []string{}
|
||||
for _, gid := range strings.Split(c.Params.GroupIDs, ",") {
|
||||
if len(gid) != 26 {
|
||||
c.SetInvalidParam("group_ids")
|
||||
return
|
||||
}
|
||||
groupIDs = append(groupIDs, gid)
|
||||
}
|
||||
|
||||
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
||||
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
||||
return
|
||||
}
|
||||
|
||||
users, totalCount, err := c.App.ChannelMembersMinusGroupMembers(
|
||||
c.Params.ChannelId,
|
||||
groupIDs,
|
||||
c.Params.Page,
|
||||
c.Params.PerPage,
|
||||
)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
b, marshalErr := json.Marshal(&model.UsersWithGroupsAndCount{
|
||||
Users: users,
|
||||
Count: totalCount,
|
||||
})
|
||||
if marshalErr != nil {
|
||||
c.Err = model.NewAppError("Api4.channelMembersMinusGroupMembers", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
@@ -2637,3 +2637,97 @@ func TestGetChannelMembersTimezones(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestChannelMembersMinusGroupMembers(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
user1 := th.BasicUser
|
||||
user2 := th.BasicUser2
|
||||
|
||||
channel := th.CreatePrivateChannel()
|
||||
|
||||
_, err := th.App.AddChannelMember(user1.Id, channel, "", "")
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddChannelMember(user2.Id, channel, "", "")
|
||||
require.Nil(t, err)
|
||||
|
||||
channel.GroupConstrained = model.NewBool(true)
|
||||
channel, err = th.App.UpdateChannel(channel)
|
||||
require.Nil(t, err)
|
||||
|
||||
group1 := th.CreateGroup()
|
||||
group2 := th.CreateGroup()
|
||||
|
||||
_, err = th.App.CreateOrRestoreGroupMember(group1.Id, user1.Id)
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.CreateOrRestoreGroupMember(group2.Id, user2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
// No permissions
|
||||
_, _, res := th.Client.ChannelMembersMinusGroupMembers(channel.Id, []string{group1.Id, group2.Id}, 0, 100, "")
|
||||
require.Equal(t, "api.context.permissions.app_error", res.Error.Id)
|
||||
|
||||
testCases := map[string]struct {
|
||||
groupIDs []string
|
||||
page int
|
||||
perPage int
|
||||
length int
|
||||
count int
|
||||
otherAssertions func([]*model.UserWithGroups)
|
||||
}{
|
||||
"All groups, expect no users removed": {
|
||||
groupIDs: []string{group1.Id, group2.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 0,
|
||||
count: 0,
|
||||
},
|
||||
"Some nonexistent group, page 0": {
|
||||
groupIDs: []string{model.NewId()},
|
||||
page: 0,
|
||||
perPage: 1,
|
||||
length: 1,
|
||||
count: 2,
|
||||
},
|
||||
"Some nonexistent group, page 1": {
|
||||
groupIDs: []string{model.NewId()},
|
||||
page: 1,
|
||||
perPage: 1,
|
||||
length: 1,
|
||||
count: 2,
|
||||
},
|
||||
"One group, expect one user removed": {
|
||||
groupIDs: []string{group1.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 1,
|
||||
count: 1,
|
||||
otherAssertions: func(uwg []*model.UserWithGroups) {
|
||||
require.Equal(t, uwg[0].Id, user2.Id)
|
||||
},
|
||||
},
|
||||
"Other group, expect other user removed": {
|
||||
groupIDs: []string{group2.Id},
|
||||
page: 0,
|
||||
perPage: 100,
|
||||
length: 1,
|
||||
count: 1,
|
||||
otherAssertions: func(uwg []*model.UserWithGroups) {
|
||||
require.Equal(t, uwg[0].Id, user1.Id)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
uwg, count, res := th.SystemAdminClient.ChannelMembersMinusGroupMembers(channel.Id, tc.groupIDs, tc.page, tc.perPage, "")
|
||||
require.Nil(t, res.Error)
|
||||
require.Len(t, uwg, tc.length)
|
||||
require.Equal(t, tc.count, int(count))
|
||||
if tc.otherAssertions != nil {
|
||||
tc.otherAssertions(uwg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user