From e4effd0c15a188eeec7a11f281e529afd6a54f80 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 13 Feb 2017 10:52:50 -0500 Subject: Implement some post endpoints for APIv4 (#5353) * Implement POST /posts endpoint for APIv4 * Implement GET /channels/{channel_id}/posts endpoint for APIv4 * Implement GET /posts/{post_id} endpoint for APIv4 * Implement GET /posts/{post_id}/thread endpoint for APIv4 * Skip team get if it's a DM channel in handlePostEvents --- api4/post.go | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 api4/post.go (limited to 'api4/post.go') diff --git a/api4/post.go b/api4/post.go new file mode 100644 index 000000000..9510fe0c6 --- /dev/null +++ b/api4/post.go @@ -0,0 +1,119 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api4 + +import ( + "net/http" + + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/app" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" +) + +func InitPost() { + l4g.Debug(utils.T("api.post.init.debug")) + + BaseRoutes.Posts.Handle("", ApiSessionRequired(createPost)).Methods("POST") + BaseRoutes.Post.Handle("", ApiSessionRequired(getPost)).Methods("GET") + BaseRoutes.Post.Handle("/thread", ApiSessionRequired(getPostThread)).Methods("GET") + BaseRoutes.PostsForChannel.Handle("", ApiSessionRequired(getPostsForChannel)).Methods("GET") +} + +func createPost(c *Context, w http.ResponseWriter, r *http.Request) { + post := model.PostFromJson(r.Body) + if post == nil { + c.SetInvalidParam("post") + return + } + + post.UserId = c.Session.UserId + + if !app.SessionHasPermissionToChannel(c.Session, post.ChannelId, model.PERMISSION_CREATE_POST) { + c.SetPermissionError(model.PERMISSION_CREATE_POST) + return + } + + if post.CreateAt != 0 && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + post.CreateAt = 0 + } + + rp, err := app.CreatePostAsUser(post) + if err != nil { + c.Err = err + return + } + + w.Write([]byte(rp.ToJson())) +} + +func getPostsForChannel(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 + } + + etag := app.GetPostsEtag(c.Params.ChannelId) + + if HandleEtag(etag, "Get Posts", w, r) { + return + } + + if list, err := app.GetPostsPage(c.Params.ChannelId, c.Params.Page, c.Params.PerPage); err != nil { + c.Err = err + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, etag) + w.Write([]byte(list.ToJson())) + } +} + +func getPost(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePostId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_READ_CHANNEL) { + c.SetPermissionError(model.PERMISSION_READ_CHANNEL) + return + } + + if post, err := app.GetSinglePost(c.Params.PostId); err != nil { + c.Err = err + return + } else if HandleEtag(post.Etag(), "Get Post", w, r) { + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, post.Etag()) + w.Write([]byte(post.ToJson())) + } +} + +func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePostId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_READ_CHANNEL) { + c.SetPermissionError(model.PERMISSION_READ_CHANNEL) + return + } + + if list, err := app.GetPostThread(c.Params.PostId); err != nil { + c.Err = err + return + } else if HandleEtag(list.Etag(), "Get Post Thread", w, r) { + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, list.Etag()) + w.Write([]byte(list.ToJson())) + } +} -- cgit v1.2.3-1-g7c22