From 9646bddd21bf778349d1563e4fde756d4e981dd2 Mon Sep 17 00:00:00 2001 From: Ruzette Tanyag Date: Tue, 21 Feb 2017 07:36:52 -0500 Subject: 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 --- api4/post.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'api4/post.go') 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())) +} -- cgit v1.2.3-1-g7c22