From 188182a3a4499965dbbde254aed0ae5e60e71dc5 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 3 Nov 2015 11:34:52 -0500 Subject: Adding ability to fetch posts around a specifc post --- api/post.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'api/post.go') 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") -- cgit v1.2.3-1-g7c22