diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-02-13 10:52:50 -0500 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-02-13 10:52:50 -0500 |
commit | e4effd0c15a188eeec7a11f281e529afd6a54f80 (patch) | |
tree | 2c9b0f451366c54e163ac6f4743adf9e1cbd9a26 /api4/post.go | |
parent | 260f1111e8988b177550d2621fbf99df178ca57a (diff) | |
download | chat-e4effd0c15a188eeec7a11f281e529afd6a54f80.tar.gz chat-e4effd0c15a188eeec7a11f281e529afd6a54f80.tar.bz2 chat-e4effd0c15a188eeec7a11f281e529afd6a54f80.zip |
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
Diffstat (limited to 'api4/post.go')
-rw-r--r-- | api4/post.go | 119 |
1 files changed, 119 insertions, 0 deletions
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())) + } +} |