api4/user: Deduplicate userids for getUsersByIds endpoint (#24344)

This commit is contained in:
Ibrahim Serdar Acikgoz 2023-08-24 15:14:01 +03:00 committed by GitHub
parent 4145fd2f4e
commit 639d73b3e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -957,6 +957,10 @@ func getUsersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
// we remove the duplicate IDs as it can bring a significant load to the
// database.
userIDs = model.RemoveDuplicateStrings(userIDs)
sinceString := r.URL.Query().Get("since")
options := &store.UserGetByIdsOpts{

View File

@ -231,13 +231,17 @@ func localGetUsers(c *Context, w http.ResponseWriter, r *http.Request) {
}
func localGetUsersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
userIds := model.ArrayFromJSON(r.Body)
userIDs := model.ArrayFromJSON(r.Body)
if len(userIds) == 0 {
if len(userIDs) == 0 {
c.SetInvalidParam("user_ids")
return
}
// we remove the duplicate IDs as it can bring a significant load to the
// database.
userIDs = model.RemoveDuplicateStrings(userIDs)
sinceString := r.URL.Query().Get("since")
options := &store.UserGetByIdsOpts{
@ -253,7 +257,7 @@ func localGetUsersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
options.Since = since
}
users, appErr := c.App.GetUsersByIds(userIds, options)
users, appErr := c.App.GetUsersByIds(userIDs, options)
if appErr != nil {
c.Err = appErr
return

View File

@ -1725,6 +1725,13 @@ func TestGetUsersByIds(t *testing.T) {
require.Len(t, users, 1, "1 user should be returned")
})
t.Run("should only return unique users when multiple IDs are requested", func(t *testing.T) {
users, _, err := client.GetUsersByIds(context.Background(), []string{th.BasicUser.Id, th.BasicUser.Id, th.BasicUser.Id})
require.NoError(t, err)
require.Len(t, users, 1, "1 user should be returned")
})
})
t.Run("should return error when not logged in", func(t *testing.T) {