diff options
author | Carlos Tadeu Panato Junior <ctadeu@gmail.com> | 2017-03-30 17:09:39 +0200 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-03-30 08:09:39 -0700 |
commit | 29e6db5713c57d7bb46d7aea38b1001b9e8a1212 (patch) | |
tree | 66d0a06a35068ce26024b0b9c030de0b1c8bab5c /model | |
parent | ee3b983a6344d78bafa553607133020e8e1fb0ed (diff) | |
download | chat-29e6db5713c57d7bb46d7aea38b1001b9e8a1212.tar.gz chat-29e6db5713c57d7bb46d7aea38b1001b9e8a1212.tar.bz2 chat-29e6db5713c57d7bb46d7aea38b1001b9e8a1212.zip |
Implement POST /users/status/ids for apiv4 (#5894)
Diffstat (limited to 'model')
-rw-r--r-- | model/client4.go | 18 | ||||
-rw-r--r-- | model/status.go | 20 | ||||
-rw-r--r-- | model/status_test.go | 42 |
3 files changed, 78 insertions, 2 deletions
diff --git a/model/client4.go b/model/client4.go index 27f8328f4..91b538ff8 100644 --- a/model/client4.go +++ b/model/client4.go @@ -194,10 +194,14 @@ func (c *Client4) GetPreferencesRoute(userId string) string { 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") } +func (c *Client4) GetUserStatusesRoute() string { + return fmt.Sprintf(c.GetUsersRoute() + "/status") +} + func (c *Client4) GetSamlRoute() string { 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. 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} } else { defer closeBody(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) + } +} diff --git a/model/status.go b/model/status.go index e38f43fe4..59115b42d 100644 --- a/model/status.go +++ b/model/status.go @@ -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{} { interfaceMap := map[string]interface{}{} for _, s := range statusMap { diff --git a/model/status_test.go b/model/status_test.go index 0b4a33d5d..cb1720fd2 100644 --- a/model/status_test.go +++ b/model/status_test.go @@ -4,6 +4,7 @@ package model import ( + "encoding/json" "strings" "testing" ) @@ -29,3 +30,44 @@ func TestStatus(t *testing.T) { 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") + } +} |