mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
ApiV4: GET /users/{user_id}/teams/unread (#5539)
This commit is contained in:
committed by
George Goldberg
parent
b0410615b8
commit
25b9b7d26b
24
api4/team.go
24
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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user