From 6ff350380b209266282cff32872f94bb9cd31d57 Mon Sep 17 00:00:00 2001 From: Andrei Stanciu Date: Tue, 28 Feb 2017 11:34:32 +0200 Subject: ApiV4: PUT /posts/{post_id} (#5521) --- api4/post.go | 27 ++++++++++++++++++++++ api4/post_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ model/client4.go | 10 ++++++++ 3 files changed, 106 insertions(+) diff --git a/api4/post.go b/api4/post.go index 6a8b1bff2..4632cb6df 100644 --- a/api4/post.go +++ b/api4/post.go @@ -24,6 +24,7 @@ func InitPost() { BaseRoutes.PostsForChannel.Handle("", ApiSessionRequired(getPostsForChannel)).Methods("GET") BaseRoutes.Team.Handle("/posts/search", ApiSessionRequired(searchPosts)).Methods("POST") + BaseRoutes.Post.Handle("", ApiSessionRequired(updatePost)).Methods("PUT") } func createPost(c *Context, w http.ResponseWriter, r *http.Request) { @@ -176,6 +177,31 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(posts.ToJson())) } +func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePostId() + post := model.PostFromJson(r.Body) + + if post == nil { + c.SetInvalidParam("post") + return + } + + if !app.SessionHasPermissionToChannelByPost(c.Session, c.Params.PostId, model.PERMISSION_EDIT_POST) { + c.SetPermissionError(model.PERMISSION_EDIT_POST) + return + } + + post.UserId = c.Session.UserId + + rpost, err := app.UpdatePost(post) + if err != nil { + c.Err = err + return + } + + w.Write([]byte(rpost.ToJson())) +} + func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) { c.RequirePostId() if c.Err != nil { @@ -198,3 +224,4 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.FileInfosToJson(infos))) } } + diff --git a/api4/post_test.go b/api4/post_test.go index 67f815b7f..4f8242564 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -11,6 +11,7 @@ import ( "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestCreatePost(t *testing.T) { @@ -98,6 +99,74 @@ func TestCreatePost(t *testing.T) { } } +func TestUpdatePost(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + channel := th.BasicChannel + + isLicensed := utils.IsLicensed + license := utils.License + allowEditPost := *utils.Cfg.ServiceSettings.AllowEditPost + defer func() { + utils.IsLicensed = isLicensed + utils.License = license + *utils.Cfg.ServiceSettings.AllowEditPost = allowEditPost + utils.SetDefaultRolesBasedOnConfig() + }() + utils.IsLicensed = true + utils.License = &model.License{Features: &model.Features{}} + utils.License.Features.SetDefaults() + + *utils.Cfg.ServiceSettings.AllowEditPost = model.ALLOW_EDIT_POST_ALWAYS + utils.SetDefaultRolesBasedOnConfig() + + post := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a"} + rpost, resp := Client.CreatePost(post) + CheckNoError(t, resp) + + if rpost.Message != post.Message { + t.Fatal("full name didn't match") + } + + if rpost.EditAt != 0 { + t.Fatal("Newly created post shouldn't have EditAt set") + } + + msg := "a" + model.NewId() + " update post" + rpost.Message = msg + rupost, resp := Client.UpdatePost(rpost.Id, rpost); + CheckNoError(t, resp) + + if rupost.Message != msg { + t.Fatal("failed to updates") + } + if rupost.EditAt == 0 { + t.Fatal("EditAt not updated for post") + } + + msg1 := "#hashtag a" + model.NewId() + " update post again" + rpost.Message = msg1 + rrupost, resp := Client.UpdatePost(rpost.Id, rpost); + CheckNoError(t, resp) + + if rrupost.Message != msg1 && rrupost.Hashtags != "#hashtag" { + t.Fatal("failed to updates") + } + + post2 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Type: model.POST_JOIN_LEAVE} + rpost2, resp := Client.CreatePost(post2) + CheckNoError(t, resp) + + up2 := &model.Post{Id: rpost2.Id, ChannelId: channel.Id, Message: "a" + model.NewId() + " update post 2"} + _, resp = Client.UpdatePost(rpost2.Id, up2); + CheckBadRequestStatus(t, resp) + + Client.Logout() + _, resp = Client.UpdatePost(rpost.Id, rpost) + CheckUnauthorizedStatus(t, resp) +} + func TestGetPostsForChannel(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() diff --git a/model/client4.go b/model/client4.go index cbaf8234b..e6c9192b9 100644 --- a/model/client4.go +++ b/model/client4.go @@ -731,6 +731,16 @@ func (c *Client4) CreatePost(post *Post) (*Post, *Response) { } } +// UpdatePost updates a post based on the provided post struct. +func (c *Client4) UpdatePost(postId string, post *Post) (*Post, *Response) { + if r, err := c.DoApiPut(c.GetPostRoute(postId), post.ToJson()); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return PostFromJson(r.Body), BuildResponse(r) + } +} + // GetPost gets a single post. func (c *Client4) GetPost(postId string, etag string) (*Post, *Response) { if r, err := c.DoApiGet(c.GetPostRoute(postId), etag); err != nil { -- cgit v1.2.3-1-g7c22