From 5507154992eedd323385c37e59b2008586b9aaa0 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 30 Jun 2017 12:07:23 -0400 Subject: Add some basic sorting support for GET /users endpoint (#6801) --- api4/user.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'api4/user.go') diff --git a/api4/user.go b/api4/user.go index 24c1c917b..04faf13c4 100644 --- a/api4/user.go +++ b/api4/user.go @@ -277,9 +277,21 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { inChannelId := r.URL.Query().Get("in_channel") notInChannelId := r.URL.Query().Get("not_in_channel") withoutTeam := r.URL.Query().Get("without_team") + sort := r.URL.Query().Get("sort") if len(notInChannelId) > 0 && len(inTeamId) == 0 { - c.SetInvalidParam("team_id") + c.SetInvalidUrlParam("team_id") + return + } + + if sort != "" && sort != "last_activity_at" && sort != "create_at" { + c.SetInvalidUrlParam("sort") + return + } + + // Currently only supports sorting on a team + if (sort == "last_activity_at" || sort == "create_at") && (inTeamId == "" || notInTeamId != "" || inChannelId != "" || notInChannelId != "" || withoutTeam != "") { + c.SetInvalidUrlParam("sort") return } @@ -287,7 +299,7 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { var err *model.AppError etag := "" - if withoutTeamBool, err := strconv.ParseBool(withoutTeam); err == nil && withoutTeamBool { + if withoutTeamBool, _ := strconv.ParseBool(withoutTeam); withoutTeamBool { // Use a special permission for now if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_LIST_USERS_WITHOUT_TEAM) { c.SetPermissionError(model.PERMISSION_LIST_USERS_WITHOUT_TEAM) @@ -320,12 +332,18 @@ func getUsers(c *Context, w http.ResponseWriter, r *http.Request) { return } - etag = app.GetUsersInTeamEtag(inTeamId) - if HandleEtag(etag, "Get Users in Team", w, r) { - return - } + if sort == "last_activity_at" { + profiles, err = app.GetRecentlyActiveUsersForTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } else if sort == "create_at" { + profiles, err = app.GetNewUsersForTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } else { + etag = app.GetUsersInTeamEtag(inTeamId) + if HandleEtag(etag, "Get Users in Team", w, r) { + return + } - profiles, err = app.GetUsersInTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + profiles, err = app.GetUsersInTeamPage(inTeamId, c.Params.Page, c.Params.PerPage, c.IsSystemAdmin()) + } } else if len(inChannelId) > 0 { if !app.SessionHasPermissionToChannel(c.Session, inChannelId, model.PERMISSION_READ_CHANNEL) { c.SetPermissionError(model.PERMISSION_READ_CHANNEL) -- cgit v1.2.3-1-g7c22