mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[APIV4] GET /users/{user_id}/status - user status endpoint for apiV4 (#5824)
This commit is contained in:
committed by
George Goldberg
parent
28ad645153
commit
5bf6ae04df
@@ -171,6 +171,7 @@ func InitApi(full bool) {
|
||||
InitLdap()
|
||||
InitBrand()
|
||||
InitCommand()
|
||||
InitStatus()
|
||||
|
||||
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
|
||||
|
||||
|
||||
40
api4/status.go
Normal file
40
api4/status.go
Normal file
@@ -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
api4/status_test.go
Normal file
55
api4/status_test.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user