From 9e8cd937908d5d2e730e94f761d6533eb2d95e28 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 18 Nov 2015 17:29:06 -0500 Subject: Implementing Permalinks and jumping to post from search. Performance improvements. --- api/context.go | 2 ++ api/post.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'api') diff --git a/api/context.go b/api/context.go index a5d4169cb..a6f9bc1e1 100644 --- a/api/context.go +++ b/api/context.go @@ -37,6 +37,8 @@ type Page struct { ClientCfg map[string]string User *model.User Team *model.Team + Channel *model.Channel + PostID string SessionTokenIndex int64 } diff --git a/api/post.go b/api/post.go index 0860fd299..ca99eb15b 100644 --- a/api/post.go +++ b/api/post.go @@ -23,6 +23,7 @@ func InitPost(r *mux.Router) { l4g.Debug("Initializing post api routes") r.Handle("/posts/search", ApiUserRequired(searchPosts)).Methods("GET") + r.Handle("/posts/{post_id}", ApiUserRequired(getPostById)).Methods("GET") sr := r.PathPrefix("/channels/{id:[A-Za-z0-9]+}").Subrouter() sr.Handle("/create", ApiUserRequired(createPost)).Methods("POST") @@ -767,6 +768,41 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) { } } +func getPostById(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + + postId := params["post_id"] + if len(postId) != 26 { + c.SetInvalidParam("getPostById", "postId") + return + } + + if result := <-Srv.Store.Post().Get(postId); result.Err != nil { + c.Err = result.Err + return + } else { + list := result.Data.(*model.PostList) + + if len(list.Order) != 1 { + c.Err = model.NewAppError("getPostById", "Unable to get post", "") + return + } + post := list.Posts[list.Order[0]] + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, post.ChannelId, c.Session.UserId) + if !c.HasPermissionsToChannel(cchan, "getPostById") { + return + } + + if HandleEtag(list.Etag(), w, r) { + return + } + + w.Header().Set(model.HEADER_ETAG_SERVER, list.Etag()) + w.Write([]byte(list.ToJson())) + } +} + func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) -- cgit v1.2.3-1-g7c22