From 25b9b7d26b8738343d269c83031ba5a5408d0d58 Mon Sep 17 00:00:00 2001 From: Andrei Stanciu Date: Tue, 28 Feb 2017 17:47:30 +0200 Subject: ApiV4: GET /users/{user_id}/teams/unread (#5539) --- api4/team.go | 24 ++++++++++++++++++++++++ api4/team_test.go | 31 +++++++++++++++++++++++++++++++ model/client4.go | 18 ++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/api4/team.go b/api4/team.go index 9071458cb..c88c553fb 100644 --- a/api4/team.go +++ b/api4/team.go @@ -18,6 +18,7 @@ func InitTeam() { BaseRoutes.Teams.Handle("", ApiSessionRequired(createTeam)).Methods("POST") BaseRoutes.Teams.Handle("", ApiSessionRequired(getAllTeams)).Methods("GET") BaseRoutes.TeamsForUser.Handle("", ApiSessionRequired(getTeamsForUser)).Methods("GET") + BaseRoutes.TeamsForUser.Handle("/unread", ApiSessionRequired(getTeamsUnreadForUser)).Methods("GET") BaseRoutes.Team.Handle("", ApiSessionRequired(getTeam)).Methods("GET") BaseRoutes.Team.Handle("/stats", ApiSessionRequired(getTeamStats)).Methods("GET") @@ -104,6 +105,29 @@ func getTeamsForUser(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getTeamsUnreadForUser(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireUserId() + if c.Err != nil { + return + } + + if c.Session.UserId != c.Params.UserId && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + // optional team id to be excluded from the result + teamId := r.URL.Query().Get("exclude_team") + + unreadTeamsList, err := app.GetTeamsUnreadForUser(teamId, c.Params.UserId) + if err != nil { + c.Err = err + return + } + + w.Write([]byte(model.TeamsUnreadToJson(unreadTeamsList))) +} + func getTeamMember(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireTeamId().RequireUserId() if c.Err != nil { diff --git a/api4/team_test.go b/api4/team_test.go index a4ec1a6e2..e54cf4414 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -463,3 +463,34 @@ func TestUpdateTeamMemberRoles(t *testing.T) { _, resp = Client.UpdateTeamMemberRoles(th.BasicTeam.Id, th.BasicUser.Id, TEAM_MEMBER) CheckNoError(t, resp) } + +func TestGetMyTeamsUnread(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + user := th.BasicUser + Client.Login(user.Email, user.Password) + + teams, resp := Client.GetTeamsUnreadForUser(user.Id, "") + CheckNoError(t, resp) + if len(teams) == 0 { + t.Fatal("should have results") + } + + teams, resp = Client.GetTeamsUnreadForUser(user.Id, th.BasicTeam.Id) + CheckNoError(t, resp) + if len(teams) != 0 { + t.Fatal("should not have results") + } + + _, resp = Client.GetTeamsUnreadForUser("fail", "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetTeamsUnreadForUser(model.NewId(), "") + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetTeamsUnreadForUser(user.Id, "") + CheckUnauthorizedStatus(t, resp) +} \ No newline at end of file diff --git a/model/client4.go b/model/client4.go index 0ccb94009..8d2423ad9 100644 --- a/model/client4.go +++ b/model/client4.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "mime/multipart" "net/http" + "net/url" "strconv" "strings" ) @@ -483,6 +484,23 @@ func (c *Client4) RevokeSession(userId, sessionId string) (bool, *Response) { } } +// getTeamsUnreadForUser will return an array with TeamUnread objects that contain the amount of +// unread messages and mentions the current user has for the teams it belongs to. +// An optional team ID can be set to exclude that team from the results. Must be authenticated. +func (c *Client4) GetTeamsUnreadForUser(userId, teamIdToExclude string) ([]*TeamUnread, *Response) { + optional := "" + if teamIdToExclude != "" { + optional += fmt.Sprintf("?exclude_team=%s", url.QueryEscape(teamIdToExclude)) + } + + if r, err := c.DoApiGet(c.GetUserRoute(userId)+"/teams/unread"+optional, ""); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return TeamsUnreadFromJson(r.Body), BuildResponse(r) + } +} + // GetAudits returns a list of audit based on the provided user id string. func (c *Client4) GetAudits(userId string, page int, perPage int, etag string) (Audits, *Response) { query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage) -- cgit v1.2.3-1-g7c22