Skip to content

Commit

Permalink
[APIV4] GET /users/{user_id}/status - user status endpoint for apiV4 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato authored and grundleborg committed Mar 24, 2017
1 parent 28ad645 commit 5bf6ae0
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 1 deletion.
1 change: 1 addition & 0 deletions api4/api.go
Expand Up @@ -171,6 +171,7 @@ func InitApi(full bool) {
InitLdap()
InitBrand()
InitCommand()
InitStatus()

app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))

Expand Down
40 changes: 40 additions & 0 deletions 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()))
}
}
}
55 changes: 55 additions & 0 deletions 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")
}
}
53 changes: 53 additions & 0 deletions app/status.go
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions i18n/en.json
Expand Up @@ -2483,6 +2483,10 @@
"id": "api.user.init.debug",
"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"
Expand Down Expand Up @@ -2863,6 +2867,10 @@
"id": "api.websocket_handler.invalid_param.app_error",
"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"
Expand Down
16 changes: 16 additions & 0 deletions model/client4.go
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion model/status.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 5bf6ae0

Please sign in to comment.