Skip to content

Commit

Permalink
ApiV4: GET /users/{user_id}/teams/unread (#5539)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreistanciu24 authored and grundleborg committed Feb 28, 2017
1 parent b041061 commit 25b9b7d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api4/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions api4/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions model/client4.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 25b9b7d

Please sign in to comment.