diff options
Diffstat (limited to 'api4')
-rw-r--r-- | api4/apitestlib.go | 22 | ||||
-rw-r--r-- | api4/channel.go | 23 | ||||
-rw-r--r-- | api4/channel_test.go | 39 |
3 files changed, 84 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() |