summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrei Stanciu <andrei.stanciu@geminisols.ro>2017-02-28 17:47:30 +0200
committerGeorge Goldberg <george@gberg.me>2017-02-28 15:47:30 +0000
commit25b9b7d26b8738343d269c83031ba5a5408d0d58 (patch)
tree7f70c50bfdf9fe588268426005bb6305f6a074a7
parentb0410615b8206ea2706312a9f413785d86134f6d (diff)
downloadchat-25b9b7d26b8738343d269c83031ba5a5408d0d58.tar.gz
chat-25b9b7d26b8738343d269c83031ba5a5408d0d58.tar.bz2
chat-25b9b7d26b8738343d269c83031ba5a5408d0d58.zip
ApiV4: GET /users/{user_id}/teams/unread (#5539)
-rw-r--r--api4/team.go24
-rw-r--r--api4/team_test.go31
-rw-r--r--model/client4.go18
3 files changed, 73 insertions, 0 deletions
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)