MM-24037 Adding getKnowUsers API endpoint (#14332)
* Adding getKnowUsers API endpoint * Adding i18n strings * Fixing golint errors * Adding doc strings * Remove debug line * Updating app_iface * Fixing gofmt
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
d3b36e3455
Коммит
224b72c61e
13
api4/user.go
13
api4/user.go
@@ -26,6 +26,7 @@ func (api *API) InitUser() {
|
||||
api.BaseRoutes.Users.Handle("", api.ApiSessionRequired(getUsers)).Methods("GET")
|
||||
api.BaseRoutes.Users.Handle("/ids", api.ApiSessionRequired(getUsersByIds)).Methods("POST")
|
||||
api.BaseRoutes.Users.Handle("/usernames", api.ApiSessionRequired(getUsersByNames)).Methods("POST")
|
||||
api.BaseRoutes.Users.Handle("/known", api.ApiSessionRequired(getKnownUsers)).Methods("GET")
|
||||
api.BaseRoutes.Users.Handle("/search", api.ApiSessionRequiredDisableWhenBusy(searchUsers)).Methods("POST")
|
||||
api.BaseRoutes.Users.Handle("/autocomplete", api.ApiSessionRequired(autocompleteUsers)).Methods("GET")
|
||||
api.BaseRoutes.Users.Handle("/stats", api.ApiSessionRequired(getTotalUsersStats)).Methods("GET")
|
||||
@@ -717,6 +718,18 @@ func getUsersByNames(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(model.UserListToJson(users)))
|
||||
}
|
||||
|
||||
func getKnownUsers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
userIds, err := c.App.GetKnownUsers(c.App.Session().UserId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(userIds)
|
||||
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func searchUsers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
props := model.UserSearchFromJson(r.Body)
|
||||
if props == nil {
|
||||
|
||||
@@ -4528,3 +4528,103 @@ func TestPromoteGuestToUser(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetKnownUsers(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
t1, err := th.App.CreateTeam(&model.Team{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: GenerateTestTeamName(),
|
||||
Email: th.GenerateTestEmail(),
|
||||
Type: model.TEAM_OPEN,
|
||||
})
|
||||
require.Nil(t, err, "failed to create team")
|
||||
|
||||
t2, err := th.App.CreateTeam(&model.Team{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: GenerateTestTeamName(),
|
||||
Email: th.GenerateTestEmail(),
|
||||
Type: model.TEAM_OPEN,
|
||||
})
|
||||
require.Nil(t, err, "failed to create team")
|
||||
|
||||
t3, err := th.App.CreateTeam(&model.Team{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: GenerateTestTeamName(),
|
||||
Email: th.GenerateTestEmail(),
|
||||
Type: model.TEAM_OPEN,
|
||||
})
|
||||
require.Nil(t, err, "failed to create team")
|
||||
|
||||
c1, err := th.App.CreateChannel(&model.Channel{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: "name_" + model.NewId(),
|
||||
Type: model.CHANNEL_OPEN,
|
||||
TeamId: t1.Id,
|
||||
CreatorId: model.NewId(),
|
||||
}, false)
|
||||
require.Nil(t, err, "failed to create channel")
|
||||
|
||||
c2, err := th.App.CreateChannel(&model.Channel{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: "name_" + model.NewId(),
|
||||
Type: model.CHANNEL_OPEN,
|
||||
TeamId: t2.Id,
|
||||
CreatorId: model.NewId(),
|
||||
}, false)
|
||||
require.Nil(t, err, "failed to create channel")
|
||||
|
||||
c3, err := th.App.CreateChannel(&model.Channel{
|
||||
DisplayName: "dn_" + model.NewId(),
|
||||
Name: "name_" + model.NewId(),
|
||||
Type: model.CHANNEL_OPEN,
|
||||
TeamId: t3.Id,
|
||||
CreatorId: model.NewId(),
|
||||
}, false)
|
||||
require.Nil(t, err, "failed to create channel")
|
||||
|
||||
u1 := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(u1)
|
||||
u2 := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(u2)
|
||||
u3 := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(u3)
|
||||
u4 := th.CreateUser()
|
||||
defer th.App.PermanentDeleteUser(u4)
|
||||
|
||||
th.LinkUserToTeam(u1, t1)
|
||||
th.LinkUserToTeam(u1, t2)
|
||||
th.LinkUserToTeam(u2, t1)
|
||||
th.LinkUserToTeam(u3, t2)
|
||||
th.LinkUserToTeam(u4, t3)
|
||||
|
||||
th.App.AddUserToChannel(u1, c1)
|
||||
th.App.AddUserToChannel(u1, c2)
|
||||
th.App.AddUserToChannel(u2, c1)
|
||||
th.App.AddUserToChannel(u3, c2)
|
||||
th.App.AddUserToChannel(u4, c3)
|
||||
|
||||
t.Run("get know users sharing no channels", func(t *testing.T) {
|
||||
_, _ = th.Client.Login(u4.Email, u4.Password)
|
||||
userIds, resp := th.Client.GetKnownUsers()
|
||||
CheckNoError(t, resp)
|
||||
assert.Empty(t, userIds)
|
||||
})
|
||||
|
||||
t.Run("get know users sharing one channel", func(t *testing.T) {
|
||||
_, _ = th.Client.Login(u3.Email, u3.Password)
|
||||
userIds, resp := th.Client.GetKnownUsers()
|
||||
CheckNoError(t, resp)
|
||||
assert.Len(t, userIds, 1)
|
||||
assert.Equal(t, userIds[0], u1.Id)
|
||||
})
|
||||
|
||||
t.Run("get know users sharing multiple channels", func(t *testing.T) {
|
||||
_, _ = th.Client.Login(u1.Email, u1.Password)
|
||||
userIds, resp := th.Client.GetKnownUsers()
|
||||
CheckNoError(t, resp)
|
||||
assert.Len(t, userIds, 2)
|
||||
assert.ElementsMatch(t, userIds, []string{u2.Id, u3.Id})
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user