summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-03-24 18:23:32 +0100
committerGeorge Goldberg <george@gberg.me>2017-03-24 17:23:32 +0000
commit5bf6ae04dfedc2e504ea8af5c71b2e9a8287e2b5 (patch)
tree19445c4de5d31670ede7429a97e292b72c022368
parent28ad645153b206ba84ddc4935280eaed94bb0138 (diff)
downloadchat-5bf6ae04dfedc2e504ea8af5c71b2e9a8287e2b5.tar.gz
chat-5bf6ae04dfedc2e504ea8af5c71b2e9a8287e2b5.tar.bz2
chat-5bf6ae04dfedc2e504ea8af5c71b2e9a8287e2b5.zip
[APIV4] GET /users/{user_id}/status - user status endpoint for apiV4 (#5824)
-rw-r--r--api4/api.go1
-rw-r--r--api4/status.go40
-rw-r--r--api4/status_test.go55
-rw-r--r--app/status.go53
-rw-r--r--i18n/en.json8
-rw-r--r--model/client4.go16
-rw-r--r--model/status.go2
7 files changed, 174 insertions, 1 deletions
diff --git a/api4/api.go b/api4/api.go
index 1ff4fa10e..6a55fbcbb 100644
--- a/api4/api.go
+++ b/api4/api.go
@@ -171,6 +171,7 @@ func InitApi(full bool) {
InitLdap()
InitBrand()
InitCommand()
+ InitStatus()
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
diff --git a/api4/status.go b/api4/status.go
new file mode 100644
index 000000000..5cd6a4536
--- /dev/null
+++ b/api4/status.go
@@ -0,0 +1,40 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api4
+
+import (
+ "net/http"
+
+ l4g "github.com/alecthomas/log4go"
+
+ "github.com/mattermost/platform/app"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+func InitStatus() {
+ l4g.Debug(utils.T("api.status.init.debug"))
+
+ BaseRoutes.User.Handle("/status", ApiHandler(getUserStatus)).Methods("GET")
+
+}
+
+func getUserStatus(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireUserId()
+ if c.Err != nil {
+ return
+ }
+
+ if statusMap, err := app.GetUserStatusesByIds([]string{c.Params.UserId}); err != nil {
+ c.Err = err
+ return
+ } else {
+ if len(statusMap) == 0 {
+ c.Err = model.NewAppError("UserStatus", "api.status.user_not_found.app_error", nil, "", http.StatusNotFound)
+ return
+ } else {
+ w.Write([]byte(statusMap[0].ToJson()))
+ }
+ }
+}
diff --git a/api4/status_test.go b/api4/status_test.go
new file mode 100644
index 000000000..da56ae435
--- /dev/null
+++ b/api4/status_test.go
@@ -0,0 +1,55 @@
+package api4
+
+import (
+ "testing"
+
+ "github.com/mattermost/platform/app"
+)
+
+func TestGetUserStatus(t *testing.T) {
+ th := Setup().InitBasic()
+ defer TearDown()
+ Client := th.Client
+
+ userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "offline" {
+ t.Fatal("Should return offline status")
+ }
+
+ app.SetStatusOnline(th.BasicUser.Id, "", true)
+ userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "online" {
+ t.Fatal("Should return online status")
+ }
+
+ app.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
+ userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "away" {
+ t.Fatal("Should return away status")
+ }
+
+ app.SetStatusOffline(th.BasicUser.Id, true)
+ userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "offline" {
+ t.Fatal("Should return offline status")
+ }
+
+ //Get user2 status logged as user1
+ userStatus, resp = Client.GetUserStatus(th.BasicUser2.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "offline" {
+ t.Fatal("Should return offline status")
+ }
+
+ Client.Logout()
+ th.LoginBasic2()
+ userStatus, resp = Client.GetUserStatus(th.BasicUser2.Id, "")
+ CheckNoError(t, resp)
+ if userStatus.Status != "offline" {
+ t.Fatal("Should return offline status")
+ }
+}
diff --git a/app/status.go b/app/status.go
index 51e7d1ed8..3adf643f8 100644
--- a/app/status.go
+++ b/app/status.go
@@ -90,6 +90,59 @@ func GetStatusesByIds(userIds []string) (map[string]interface{}, *model.AppError
return statusMap, nil
}
+//GetUserStatusesByIds used by apiV4
+func GetUserStatusesByIds(userIds []string) ([]*model.Status, *model.AppError) {
+ var statusMap []*model.Status
+ metrics := einterfaces.GetMetricsInterface()
+
+ missingUserIds := []string{}
+ for _, userId := range userIds {
+ if result, ok := statusCache.Get(userId); ok {
+ statusMap = append(statusMap, result.(*model.Status))
+ if metrics != nil {
+ metrics.IncrementMemCacheHitCounter("Status")
+ }
+ } else {
+ missingUserIds = append(missingUserIds, userId)
+ if metrics != nil {
+ metrics.IncrementMemCacheMissCounter("Status")
+ }
+ }
+ }
+
+ if len(missingUserIds) > 0 {
+ if result := <-Srv.Store.Status().GetByIds(missingUserIds); result.Err != nil {
+ return nil, result.Err
+ } else {
+ statuses := result.Data.([]*model.Status)
+
+ for _, s := range statuses {
+ AddStatusCache(s)
+ }
+
+ statusMap = append(statusMap, statuses...)
+ }
+ }
+
+ // For the case where the user does not have a row in the Status table and cache
+ // remove the existing ids from missingUserIds and then create a offline state for the missing ones
+ for i := 0; i < len(missingUserIds); i++ {
+ missingUserId := missingUserIds[i]
+ for _, userMap := range statusMap {
+ if missingUserId == userMap.UserId {
+ missingUserIds = append(missingUserIds[:i], missingUserIds[i+1:]...)
+ i--
+ break
+ }
+ }
+ }
+ for _, userId := range missingUserIds {
+ statusMap = append(statusMap, &model.Status{UserId: userId, Status: "offline"})
+ }
+
+ return statusMap, nil
+}
+
func SetStatusOnline(userId string, sessionId string, manual bool) {
broadcast := false
diff --git a/i18n/en.json b/i18n/en.json
index 5d1889585..d16a288da 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2484,6 +2484,10 @@
"translation": "Initializing user API routes"
},
{
+ "id": "api.status.init.debug",
+ "translation": "Initializing status API routes"
+ },
+ {
"id": "api.user.ldap_to_email.not_available.app_error",
"translation": "AD/LDAP not available on this server"
},
@@ -2864,6 +2868,10 @@
"translation": "Invalid {{.Name}} parameter"
},
{
+ "id": "api.status.user_not_found.app_error",
+ "translation": "User not found"
+ },
+ {
"id": "app.channel.create_channel.no_team_id.app_error",
"translation": "Must specify the team ID to create a channel"
},
diff --git a/model/client4.go b/model/client4.go
index 214c31865..3aef5019c 100644
--- a/model/client4.go
+++ b/model/client4.go
@@ -190,6 +190,10 @@ func (c *Client4) GetPreferencesRoute(userId string) string {
return fmt.Sprintf(c.GetUserRoute(userId) + "/preferences")
}
+func (c *Client4) GetStatusRoute(userId string) string {
+ return fmt.Sprintf(c.GetUserRoute(userId) + "/status")
+}
+
func (c *Client4) GetSamlRoute() string {
return fmt.Sprintf("/saml")
}
@@ -1764,3 +1768,15 @@ func (c *Client4) CreateCommand(cmd *Command) (*Command, *Response) {
return CommandFromJson(r.Body), BuildResponse(r)
}
}
+
+// Status Section
+
+// GetUserStatus returns a user based on the provided user id string.
+func (c *Client4) GetUserStatus(userId, etag string) (*Status, *Response) {
+ if r, err := c.DoApiGet(c.GetStatusRoute(userId), etag); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return StatusFromJson(r.Body), BuildResponse(r)
+ }
+}
diff --git a/model/status.go b/model/status.go
index fc1557887..e38f43fe4 100644
--- a/model/status.go
+++ b/model/status.go
@@ -22,7 +22,7 @@ type Status struct {
Status string `json:"status"`
Manual bool `json:"manual"`
LastActivityAt int64 `json:"last_activity_at"`
- ActiveChannel string `json:"active_channel" db:"-"`
+ ActiveChannel string `json:"-" db:"-"`
}
func (o *Status) ToJson() string {