diff options
author | Ruzette Tanyag <ruzette@users.noreply.github.com> | 2017-02-21 07:36:52 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-02-21 07:36:52 -0500 |
commit | 9646bddd21bf778349d1563e4fde756d4e981dd2 (patch) | |
tree | 13193200a3d7be4c5d9017f20ea8239f14d14d4e /api4/post.go | |
parent | a14e44b4ec19f7005328aa28d9137d25d30dd4eb (diff) | |
download | chat-9646bddd21bf778349d1563e4fde756d4e981dd2.tar.gz chat-9646bddd21bf778349d1563e4fde756d4e981dd2.tar.bz2 chat-9646bddd21bf778349d1563e4fde756d4e981dd2.zip |
Implement posts endpoints for APIv4 (#5480)
* Implement delete post endpoint for apiv4
* Implement POST search post endpoint for APIv4
* removed delete post quotes
* rearrange formatting
Diffstat (limited to 'api4/post.go')
-rw-r--r-- | api4/post.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/api4/post.go b/api4/post.go index 9510fe0c6..7290ce8ef 100644 --- a/api4/post.go +++ b/api4/post.go @@ -5,6 +5,7 @@ package api4 import ( "net/http" + "strconv" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/app" @@ -17,8 +18,11 @@ func InitPost() { BaseRoutes.Posts.Handle("", ApiSessionRequired(createPost)).Methods("POST") BaseRoutes.Post.Handle("", ApiSessionRequired(getPost)).Methods("GET") + BaseRoutes.Post.Handle("", ApiSessionRequired(deletePost)).Methods("DELETE") BaseRoutes.Post.Handle("/thread", ApiSessionRequired(getPostThread)).Methods("GET") BaseRoutes.PostsForChannel.Handle("", ApiSessionRequired(getPostsForChannel)).Methods("GET") + + BaseRoutes.Team.Handle("/posts/search", ApiSessionRequired(searchPosts)).Methods("POST") } func createPost(c *Context, w http.ResponseWriter, r *http.Request) { @@ -96,6 +100,25 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePostId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToPost(c.Session, c.Params.PostId, model.PERMISSION_DELETE_OTHERS_POSTS) { + c.SetPermissionError(model.PERMISSION_DELETE_OTHERS_POSTS) + return + } + + if _, err := app.DeletePost(c.Params.PostId); err != nil { + c.Err = err + return + } + + ReturnStatusOK(w) +} + func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) { c.RequirePostId() if c.Err != nil { @@ -117,3 +140,37 @@ func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(list.ToJson())) } } + +func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireTeamId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) { + c.SetPermissionError(model.PERMISSION_VIEW_TEAM) + return + } + + props := model.MapFromJson(r.Body) + terms := props["terms"] + + if len(terms) == 0 { + c.SetInvalidParam("terms") + return + } + + isOrSearch := false + if val, ok := props["is_or_search"]; ok && val != "" { + isOrSearch, _ = strconv.ParseBool(val) + } + + posts, err := app.SearchPostsInTeam(terms, c.Session.UserId, c.Params.TeamId, isOrSearch) + if err != nil { + c.Err = err + return + } + + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + w.Write([]byte(posts.ToJson())) +} |