summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-03-24 16:45:34 -0400
committerCorey Hulen <corey@hulen.com>2017-03-24 13:45:34 -0700
commit28a78d76074749a3b7f1ef2a56617b0a1c7fd623 (patch)
tree4adc9c167991a9cea04fa9cc3c01e247a50293c8 /model
parent22715a31ed6238eb4f8f0dd8125bf23958345e78 (diff)
downloadchat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.tar.gz
chat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.tar.bz2
chat-28a78d76074749a3b7f1ef2a56617b0a1c7fd623.zip
Implement some channel endpoints for APIv4 (#5846)
* Add v4 endpoint for getting the channels on a team for a user * Implement PUT /channels/{channel_id}/patch endpoint for APIv4 * Implement POST /teams/{team_id}/channels/search endpoint for APIv4 * Update permission check
Diffstat (limited to 'model')
-rw-r--r--model/channel.go45
-rw-r--r--model/channel_test.go33
-rw-r--r--model/client4.go32
3 files changed, 109 insertions, 1 deletions
diff --git a/model/channel.go b/model/channel.go
index d24fdb2b4..d80674444 100644
--- a/model/channel.go
+++ b/model/channel.go
@@ -46,6 +46,13 @@ type Channel struct {
CreatorId string `json:"creator_id"`
}
+type ChannelPatch struct {
+ DisplayName *string `json:"display_name"`
+ Name *string `json:"name"`
+ Header *string `json:"header"`
+ Purpose *string `json:"purpose"`
+}
+
func (o *Channel) ToJson() string {
b, err := json.Marshal(o)
if err != nil {
@@ -55,6 +62,15 @@ func (o *Channel) ToJson() string {
}
}
+func (o *ChannelPatch) ToJson() string {
+ b, err := json.Marshal(o)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
func ChannelFromJson(data io.Reader) *Channel {
decoder := json.NewDecoder(data)
var o Channel
@@ -66,6 +82,17 @@ func ChannelFromJson(data io.Reader) *Channel {
}
}
+func ChannelPatchFromJson(data io.Reader) *ChannelPatch {
+ decoder := json.NewDecoder(data)
+ var o ChannelPatch
+ err := decoder.Decode(&o)
+ if err == nil {
+ return &o
+ } else {
+ return nil
+ }
+}
+
func (o *Channel) Etag() string {
return Etag(o.Id, o.UpdateAt)
}
@@ -137,6 +164,24 @@ func (o *Channel) IsGroupOrDirect() bool {
return o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP
}
+func (o *Channel) Patch(patch *ChannelPatch) {
+ if patch.DisplayName != nil {
+ o.DisplayName = *patch.DisplayName
+ }
+
+ if patch.Name != nil {
+ o.Name = *patch.Name
+ }
+
+ if patch.Header != nil {
+ o.Header = *patch.Header
+ }
+
+ if patch.Purpose != nil {
+ o.Purpose = *patch.Purpose
+ }
+}
+
func GetDMNameFromIds(userId1, userId2 string) string {
if userId1 > userId2 {
return userId2 + "__" + userId1
diff --git a/model/channel_test.go b/model/channel_test.go
index deb36633c..207ce4639 100644
--- a/model/channel_test.go
+++ b/model/channel_test.go
@@ -16,6 +16,39 @@ func TestChannelJson(t *testing.T) {
if o.Id != ro.Id {
t.Fatal("Ids do not match")
}
+
+ p := ChannelPatch{Name: new(string)}
+ *p.Name = NewId()
+ json = p.ToJson()
+ rp := ChannelPatchFromJson(strings.NewReader(json))
+
+ if *p.Name != *rp.Name {
+ t.Fatal("names do not match")
+ }
+}
+
+func TestChannelPatch(t *testing.T) {
+ p := &ChannelPatch{Name: new(string), DisplayName: new(string), Header: new(string), Purpose: new(string)}
+ *p.Name = NewId()
+ *p.DisplayName = NewId()
+ *p.Header = NewId()
+ *p.Purpose = NewId()
+
+ o := Channel{Id: NewId(), Name: NewId()}
+ o.Patch(p)
+
+ if *p.Name != o.Name {
+ t.Fatal("do not match")
+ }
+ if *p.DisplayName != o.DisplayName {
+ t.Fatal("do not match")
+ }
+ if *p.Header != o.Header {
+ t.Fatal("do not match")
+ }
+ if *p.Purpose != o.Purpose {
+ t.Fatal("do not match")
+ }
}
func TestChannelIsValid(t *testing.T) {
diff --git a/model/client4.go b/model/client4.go
index b269f2f34..c567ab755 100644
--- a/model/client4.go
+++ b/model/client4.go
@@ -897,6 +897,16 @@ func (c *Client4) UpdateChannel(channel *Channel) (*Channel, *Response) {
}
}
+// PatchChannel partially updates a channel. Any missing fields are not updated.
+func (c *Client4) PatchChannel(channelId string, patch *ChannelPatch) (*Channel, *Response) {
+ if r, err := c.DoApiPut(c.GetChannelRoute(channelId)+"/patch", patch.ToJson()); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return ChannelFromJson(r.Body), BuildResponse(r)
+ }
+}
+
// CreateDirectChannel creates a direct message channel based on the two user
// ids provided.
func (c *Client4) CreateDirectChannel(userId1, userId2 string) (*Channel, *Response) {
@@ -929,7 +939,7 @@ func (c *Client4) GetChannelStats(channelId string, etag string) (*ChannelStats,
}
}
-// GetPublicChannelsForTeam returns a channel based on the provided team id string.
+// GetPublicChannelsForTeam returns a list of public channels based on the provided team id string.
func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, etag string) (*ChannelList, *Response) {
query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage)
if r, err := c.DoApiGet(c.GetPublicChannelsForTeamRoute(teamId)+query, etag); err != nil {
@@ -940,6 +950,26 @@ func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int,
}
}
+// GetChannelsForTeamForUser returns a list channels of on a team for a user.
+func (c *Client4) GetChannelsForTeamForUser(teamId, userId, etag string) (*ChannelList, *Response) {
+ if r, err := c.DoApiGet(c.GetUserRoute(userId)+c.GetTeamRoute(teamId)+"/channels", etag); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return ChannelListFromJson(r.Body), BuildResponse(r)
+ }
+}
+
+// SearchChannels returns the channels on a team matching the provided search term.
+func (c *Client4) SearchChannels(teamId string, search *ChannelSearch) (*ChannelList, *Response) {
+ if r, err := c.DoApiPost(c.GetTeamRoute(teamId)+"/channels/search", search.ToJson()); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return ChannelListFromJson(r.Body), BuildResponse(r)
+ }
+}
+
// DeleteChannel deletes channel based on the provided channel id string.
func (c *Client4) DeleteChannel(channelId string) (bool, *Response) {
if r, err := c.DoApiDelete(c.GetChannelRoute(channelId)); err != nil {