summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-03-30 00:09:05 +0900
committerCorey Hulen <corey@hulen.com>2017-03-29 08:09:05 -0700
commit8a31718db11c803fa3b14f85c760ca9db6083670 (patch)
tree80a006c8994ef57ccd6d09fc7b0d00ebe6969f39
parent64f80decaf4c20c643e43426e3c4285b2d501a90 (diff)
downloadchat-8a31718db11c803fa3b14f85c760ca9db6083670.tar.gz
chat-8a31718db11c803fa3b14f85c760ca9db6083670.tar.bz2
chat-8a31718db11c803fa3b14f85c760ca9db6083670.zip
APIv4 get /channels/{channel_id}/pinned (#5893)
-rw-r--r--api4/apitestlib.go22
-rw-r--r--api4/channel.go23
-rw-r--r--api4/channel_test.go39
-rw-r--r--app/channel.go8
-rw-r--r--model/client4.go10
5 files changed, 102 insertions, 0 deletions
diff --git a/api4/apitestlib.go b/api4/apitestlib.go
index 87a3976f5..749053271 100644
--- a/api4/apitestlib.go
+++ b/api4/apitestlib.go
@@ -247,6 +247,10 @@ func (me *TestHelper) CreatePost() *model.Post {
return me.CreatePostWithClient(me.Client, me.BasicChannel)
}
+func (me *TestHelper) CreatePinnedPost() *model.Post {
+ return me.CreatePinnedPostWithClient(me.Client, me.BasicChannel)
+}
+
func (me *TestHelper) CreateMessagePost(message string) *model.Post {
return me.CreateMessagePostWithClient(me.Client, me.BasicChannel, message)
}
@@ -268,6 +272,24 @@ func (me *TestHelper) CreatePostWithClient(client *model.Client4, channel *model
return rpost
}
+func (me *TestHelper) CreatePinnedPostWithClient(client *model.Client4, channel *model.Channel) *model.Post {
+ id := model.NewId()
+
+ post := &model.Post{
+ ChannelId: channel.Id,
+ Message: "message_" + id,
+ IsPinned: true,
+ }
+
+ utils.DisableDebugLogForTest()
+ rpost, resp := client.CreatePost(post)
+ if resp.Error != nil {
+ panic(resp.Error)
+ }
+ utils.EnableDebugLogForTest()
+ return rpost
+}
+
func (me *TestHelper) CreateMessagePostWithClient(client *model.Client4, channel *model.Channel, message string) *model.Post {
post := &model.Post{
ChannelId: channel.Id,
diff --git a/api4/channel.go b/api4/channel.go
index 278bf1d2e..ea4f2783a 100644
--- a/api4/channel.go
+++ b/api4/channel.go
@@ -29,6 +29,7 @@ func InitChannel() {
BaseRoutes.Channel.Handle("/patch", ApiSessionRequired(patchChannel)).Methods("PUT")
BaseRoutes.Channel.Handle("", ApiSessionRequired(deleteChannel)).Methods("DELETE")
BaseRoutes.Channel.Handle("/stats", ApiSessionRequired(getChannelStats)).Methods("GET")
+ BaseRoutes.Channel.Handle("/pinned", ApiSessionRequired(getPinnedPosts)).Methods("GET")
BaseRoutes.ChannelForUser.Handle("/unread", ApiSessionRequired(getChannelUnread)).Methods("GET")
@@ -303,6 +304,28 @@ func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(stats.ToJson()))
}
+func getPinnedPosts(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireChannelId()
+ if c.Err != nil {
+ return
+ }
+
+ if !app.SessionHasPermissionToChannel(c.Session, c.Params.ChannelId, model.PERMISSION_READ_CHANNEL) {
+ c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
+ return
+ }
+
+ if posts, err := app.GetPinnedPosts(c.Params.ChannelId); err != nil {
+ c.Err = err
+ return
+ } else if HandleEtag(posts.Etag(), "Get Pinned Posts", w, r) {
+ return
+ } else {
+ w.Header().Set(model.HEADER_ETAG_SERVER, posts.Etag())
+ w.Write([]byte(posts.ToJson()))
+ }
+}
+
func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId()
if c.Err != nil {
diff --git a/api4/channel_test.go b/api4/channel_test.go
index 3d501b313..3cc1cb64f 100644
--- a/api4/channel_test.go
+++ b/api4/channel_test.go
@@ -1322,6 +1322,45 @@ func TestGetChannelStats(t *testing.T) {
CheckNoError(t, resp)
}
+func TestGetPinnedPosts(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+ channel := th.BasicChannel
+
+ posts, resp := Client.GetPinnedPosts(channel.Id, "")
+ CheckNoError(t, resp)
+ if len(posts.Posts) != 0 {
+ t.Fatal("should not have gotten a pinned post")
+ }
+
+ pinnedPost := th.CreatePinnedPost()
+ posts, resp = Client.GetPinnedPosts(channel.Id, "")
+ CheckNoError(t, resp)
+ if len(posts.Posts) != 1 {
+ t.Fatal("should have returned 1 pinned post")
+ }
+ if _, ok := posts.Posts[pinnedPost.Id]; !ok {
+ t.Fatal("missing pinned post")
+ }
+
+ posts, resp = Client.GetPinnedPosts(channel.Id, resp.Etag)
+ CheckEtag(t, posts, resp)
+
+ _, resp = Client.GetPinnedPosts(GenerateTestId(), "")
+ CheckForbiddenStatus(t, resp)
+
+ _, resp = Client.GetPinnedPosts("junk", "")
+ CheckBadRequestStatus(t, resp)
+
+ Client.Logout()
+ _, resp = Client.GetPinnedPosts(channel.Id, "")
+ CheckUnauthorizedStatus(t, resp)
+
+ _, resp = th.SystemAdminClient.GetPinnedPosts(channel.Id, "")
+ CheckNoError(t, resp)
+}
+
func TestUpdateChannelRoles(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
diff --git a/app/channel.go b/app/channel.go
index 5278abbfe..61b139e5b 100644
--- a/app/channel.go
+++ b/app/channel.go
@@ -1053,3 +1053,11 @@ func PermanentDeleteChannel(channel *model.Channel) *model.AppError {
return nil
}
+
+func GetPinnedPosts(channelId string) (*model.PostList, *model.AppError) {
+ if result := <-Srv.Store.Channel().GetPinnedPosts(channelId); result.Err != nil {
+ return nil, result.Err
+ } else {
+ return result.Data.(*model.PostList), nil
+ }
+}
diff --git a/model/client4.go b/model/client4.go
index 5259cb915..516a5d23c 100644
--- a/model/client4.go
+++ b/model/client4.go
@@ -1004,6 +1004,16 @@ func (c *Client4) GetChannelStats(channelId string, etag string) (*ChannelStats,
}
}
+// GetPinnedPosts gets a list of pinned posts.
+func (c *Client4) GetPinnedPosts(channelId string, etag string) (*PostList, *Response) {
+ if r, err := c.DoApiGet(c.GetChannelRoute(channelId)+"/pinned", etag); err != nil {
+ return nil, &Response{StatusCode: r.StatusCode, Error: err}
+ } else {
+ defer closeBody(r)
+ return PostListFromJson(r.Body), BuildResponse(r)
+ }
+}
+
// 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)