APIv4 get /channels/{channel_id}/pinned (#5893)

This commit is contained in:
Saturnino Abril
2017-03-30 00:09:05 +09:00
committed by Corey Hulen
parent 64f80decaf
commit 8a31718db1
5 changed files with 102 additions and 0 deletions

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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
}
}

View File

@@ -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)