diff options
author | Joram Wilander <jwawilander@gmail.com> | 2015-11-04 07:51:19 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2015-11-04 07:51:19 -0500 |
commit | 0cc985f6f4c439eb9a951d007628849d15a16087 (patch) | |
tree | dc9ef286cdfaa4a86a9ccab1d6a4ef3d3c0134ca /api/post.go | |
parent | 87c4b4fbf44662b1ff70e245126921f24d0829a4 (diff) | |
parent | 188182a3a4499965dbbde254aed0ae5e60e71dc5 (diff) | |
download | chat-0cc985f6f4c439eb9a951d007628849d15a16087.tar.gz chat-0cc985f6f4c439eb9a951d007628849d15a16087.tar.bz2 chat-0cc985f6f4c439eb9a951d007628849d15a16087.zip |
Merge pull request #1285 from mattermost/plt-824
PLT-824 Adding ability to fetch posts around a specifc post
Diffstat (limited to 'api/post.go')
-rw-r--r-- | api/post.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/api/post.go b/api/post.go index c98bf2d71..31a7ab3b5 100644 --- a/api/post.go +++ b/api/post.go @@ -31,6 +31,8 @@ func InitPost(r *mux.Router) { sr.Handle("/posts/{time:[0-9]+}", ApiUserRequiredActivity(getPostsSince, false)).Methods("GET") sr.Handle("/post/{post_id:[A-Za-z0-9]+}", ApiUserRequired(getPost)).Methods("GET") sr.Handle("/post/{post_id:[A-Za-z0-9]+}/delete", ApiUserRequired(deletePost)).Methods("POST") + sr.Handle("/post/{post_id:[A-Za-z0-9]+}/before/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsBefore)).Methods("GET") + sr.Handle("/post/{post_id:[A-Za-z0-9]+}/after/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsAfter)).Methods("GET") } func createPost(c *Context, w http.ResponseWriter, r *http.Request) { @@ -812,6 +814,70 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getPostsBefore(c *Context, w http.ResponseWriter, r *http.Request) { + getPostsBeforeOrAfter(c, w, r, true) +} +func getPostsAfter(c *Context, w http.ResponseWriter, r *http.Request) { + getPostsBeforeOrAfter(c, w, r, false) +} +func getPostsBeforeOrAfter(c *Context, w http.ResponseWriter, r *http.Request, before bool) { + params := mux.Vars(r) + + id := params["id"] + if len(id) != 26 { + c.SetInvalidParam("getPostsBeforeOrAfter", "channelId") + return + } + + postId := params["post_id"] + if len(postId) != 26 { + c.SetInvalidParam("getPostsBeforeOrAfter", "postId") + return + } + + numPosts, err := strconv.Atoi(params["num_posts"]) + if err != nil || numPosts <= 0 { + c.SetInvalidParam("getPostsBeforeOrAfter", "numPosts") + return + } + + offset, err := strconv.Atoi(params["offset"]) + if err != nil || offset < 0 { + c.SetInvalidParam("getPostsBeforeOrAfter", "offset") + return + } + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, id, c.Session.UserId) + // We can do better than this etag in this situation + etagChan := Srv.Store.Post().GetEtag(id) + + if !c.HasPermissionsToChannel(cchan, "getPostsBeforeOrAfter") { + return + } + + etag := (<-etagChan).Data.(string) + if HandleEtag(etag, w, r) { + return + } + + var pchan store.StoreChannel + if before { + pchan = Srv.Store.Post().GetPostsBefore(id, postId, numPosts, offset) + } else { + pchan = Srv.Store.Post().GetPostsAfter(id, postId, numPosts, offset) + } + + if result := <-pchan; result.Err != nil { + c.Err = result.Err + return + } else { + list := result.Data.(*model.PostList) + + w.Header().Set(model.HEADER_ETAG_SERVER, etag) + w.Write([]byte(list.ToJson())) + } +} + func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { terms := r.FormValue("terms") |