Implement POST /users/status/ids for apiv4 (#5894)
Этот коммит содержится в:
коммит произвёл
Corey Hulen
родитель
ee3b983a63
Коммит
29e6db5713
@@ -17,6 +17,7 @@ func InitStatus() {
|
|||||||
l4g.Debug(utils.T("api.status.init.debug"))
|
l4g.Debug(utils.T("api.status.init.debug"))
|
||||||
|
|
||||||
BaseRoutes.User.Handle("/status", ApiHandler(getUserStatus)).Methods("GET")
|
BaseRoutes.User.Handle("/status", ApiHandler(getUserStatus)).Methods("GET")
|
||||||
|
BaseRoutes.Users.Handle("/status/ids", ApiHandler(getUserStatusesByIds)).Methods("POST")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +27,8 @@ func getUserStatus(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No permission check required
|
||||||
|
|
||||||
if statusMap, err := app.GetUserStatusesByIds([]string{c.Params.UserId}); err != nil {
|
if statusMap, err := app.GetUserStatusesByIds([]string{c.Params.UserId}); err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
return
|
return
|
||||||
@@ -38,3 +41,21 @@ func getUserStatus(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUserStatusesByIds(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
userIds := model.ArrayFromJson(r.Body)
|
||||||
|
|
||||||
|
if len(userIds) == 0 {
|
||||||
|
c.SetInvalidParam("user_ids")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// No permission check required
|
||||||
|
|
||||||
|
if statusMap, err := app.GetUserStatusesByIds(userIds); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.Write([]byte(model.StatusListToJson(statusMap)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,3 +53,39 @@ func TestGetUserStatus(t *testing.T) {
|
|||||||
t.Fatal("Should return offline status")
|
t.Fatal("Should return offline status")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetUsersStatusesByIds(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
|
||||||
|
usersIds := []string{th.BasicUser.Id, th.BasicUser2.Id}
|
||||||
|
|
||||||
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
for _, userStatus := range usersStatuses {
|
||||||
|
if userStatus.Status != "offline" {
|
||||||
|
t.Fatal("Status should be offline")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.SetStatusOnline(th.BasicUser.Id, "", true)
|
||||||
|
app.SetStatusOnline(th.BasicUser2.Id, "", true)
|
||||||
|
usersStatuses, resp = Client.GetUsersStatusesByIds(usersIds)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
for _, userStatus := range usersStatuses {
|
||||||
|
if userStatus.Status != "online" {
|
||||||
|
t.Fatal("Status should be offline")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
|
||||||
|
app.SetStatusAwayIfNeeded(th.BasicUser2.Id, true)
|
||||||
|
usersStatuses, resp = Client.GetUsersStatusesByIds(usersIds)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
for _, userStatus := range usersStatuses {
|
||||||
|
if userStatus.Status != "away" {
|
||||||
|
t.Fatal("Status should be offline")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ func GetUserStatusesByIds(userIds []string) ([]*model.Status, *model.AppError) {
|
|||||||
|
|
||||||
// For the case where the user does not have a row in the Status table and cache
|
// 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
|
// remove the existing ids from missingUserIds and then create a offline state for the missing ones
|
||||||
|
// This also return the status offline for the non-existing Ids in the system
|
||||||
for i := 0; i < len(missingUserIds); i++ {
|
for i := 0; i < len(missingUserIds); i++ {
|
||||||
missingUserId := missingUserIds[i]
|
missingUserId := missingUserIds[i]
|
||||||
for _, userMap := range statusMap {
|
for _, userMap := range statusMap {
|
||||||
|
|||||||
@@ -194,10 +194,14 @@ func (c *Client4) GetPreferencesRoute(userId string) string {
|
|||||||
return fmt.Sprintf(c.GetUserRoute(userId) + "/preferences")
|
return fmt.Sprintf(c.GetUserRoute(userId) + "/preferences")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client4) GetStatusRoute(userId string) string {
|
func (c *Client4) GetUserStatusRoute(userId string) string {
|
||||||
return fmt.Sprintf(c.GetUserRoute(userId) + "/status")
|
return fmt.Sprintf(c.GetUserRoute(userId) + "/status")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client4) GetUserStatusesRoute() string {
|
||||||
|
return fmt.Sprintf(c.GetUsersRoute() + "/status")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client4) GetSamlRoute() string {
|
func (c *Client4) GetSamlRoute() string {
|
||||||
return fmt.Sprintf("/saml")
|
return fmt.Sprintf("/saml")
|
||||||
}
|
}
|
||||||
@@ -1989,10 +1993,20 @@ func (c *Client4) CreateCommand(cmd *Command) (*Command, *Response) {
|
|||||||
|
|
||||||
// GetUserStatus returns a user based on the provided user id string.
|
// GetUserStatus returns a user based on the provided user id string.
|
||||||
func (c *Client4) GetUserStatus(userId, etag string) (*Status, *Response) {
|
func (c *Client4) GetUserStatus(userId, etag string) (*Status, *Response) {
|
||||||
if r, err := c.DoApiGet(c.GetStatusRoute(userId), etag); err != nil {
|
if r, err := c.DoApiGet(c.GetUserStatusRoute(userId), etag); err != nil {
|
||||||
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
} else {
|
} else {
|
||||||
defer closeBody(r)
|
defer closeBody(r)
|
||||||
return StatusFromJson(r.Body), BuildResponse(r)
|
return StatusFromJson(r.Body), BuildResponse(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUsersStatusesByIds returns a list of users status based on the provided user ids.
|
||||||
|
func (c *Client4) GetUsersStatusesByIds(userIds []string) ([]*Status, *Response) {
|
||||||
|
if r, err := c.DoApiPost(c.GetUserStatusesRoute()+"/ids", ArrayToJson(userIds)); err != nil {
|
||||||
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
|
} else {
|
||||||
|
defer closeBody(r)
|
||||||
|
return StatusListFromJson(r.Body), BuildResponse(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,6 +45,26 @@ func StatusFromJson(data io.Reader) *Status {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StatusListToJson(u []*Status) string {
|
||||||
|
b, err := json.Marshal(u)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func StatusListFromJson(data io.Reader) []*Status {
|
||||||
|
decoder := json.NewDecoder(data)
|
||||||
|
var statuses []*Status
|
||||||
|
err := decoder.Decode(&statuses)
|
||||||
|
if err == nil {
|
||||||
|
return statuses
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} {
|
func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} {
|
||||||
interfaceMap := map[string]interface{}{}
|
interfaceMap := map[string]interface{}{}
|
||||||
for _, s := range statusMap {
|
for _, s := range statusMap {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -29,3 +30,44 @@ func TestStatus(t *testing.T) {
|
|||||||
t.Fatal("Manual should have matched")
|
t.Fatal("Manual should have matched")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStatusListToJson(t *testing.T) {
|
||||||
|
statuses := []*Status{{NewId(), STATUS_ONLINE, true, 0, ""}, {NewId(), STATUS_OFFLINE, true, 0, ""}}
|
||||||
|
jsonStatuses := StatusListToJson(statuses)
|
||||||
|
|
||||||
|
var dat []map[string]interface{}
|
||||||
|
if err := json.Unmarshal([]byte(jsonStatuses), &dat); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dat) != 2 {
|
||||||
|
t.Fatal("Status array should contain 2 elements")
|
||||||
|
}
|
||||||
|
if statuses[0].UserId != dat[0]["user_id"] {
|
||||||
|
t.Fatal("UserId should be equal")
|
||||||
|
}
|
||||||
|
if statuses[1].UserId != dat[1]["user_id"] {
|
||||||
|
t.Fatal("UserId should be equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStatusListFromJson(t *testing.T) {
|
||||||
|
const jsonStream = `
|
||||||
|
[{"user_id":"k39fowpzhfffjxeaw8ecyrygme","status":"online","manual":true,"last_activity_at":0},{"user_id":"e9f1bbg8wfno7b3k7yk79bbwfy","status":"offline","manual":true,"last_activity_at":0}]
|
||||||
|
`
|
||||||
|
var dat []map[string]interface{}
|
||||||
|
if err := json.Unmarshal([]byte(jsonStream), &dat); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
toDec := strings.NewReader(jsonStream)
|
||||||
|
var statusesFromJson []*Status
|
||||||
|
statusesFromJson = StatusListFromJson(toDec)
|
||||||
|
|
||||||
|
if statusesFromJson[0].UserId != dat[0]["user_id"] {
|
||||||
|
t.Fatal("UserId should be equal")
|
||||||
|
}
|
||||||
|
if statusesFromJson[1].UserId != dat[1]["user_id"] {
|
||||||
|
t.Fatal("UserId should be equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user