MM-45494: Add endpoint for message history (#20945)

* add api url

* write sql in store

* update app and client golang driver

* update layers and tests

* change sql query sort

* update layers

* fix style

* fix style post_store.go

* fix comments

* add test for app/post_test.go

* update layers

* fix test

* fix style

* fix style again :)

* add permission check

* add additional checks and tests

* change from nil to empty

* update app-layers

* fix style

* add index for OriginalId for Posts table

* fix postgres query

* update migration file names

* update app-layers

* address PR reviews

* write tests for post store

* fix style

* Update file name

* Update file name 2

* Update file name 3

* Update file name 4

* change the error orders

* update migration file name

---------

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Sinan Sonmez (Chaush)
2023-02-07 15:30:37 +01:00
коммит произвёл GitHub
родитель eabf454764
Коммит 50fec7c892
19 изменённых файлов: 433 добавлений и 0 удалений

Просмотреть файл

@@ -22,6 +22,7 @@ func (api *API) InitPost() {
api.BaseRoutes.Post.Handle("", api.APISessionRequired(deletePost)).Methods("DELETE")
api.BaseRoutes.Posts.Handle("/ids", api.APISessionRequired(getPostsByIds)).Methods("POST")
api.BaseRoutes.Posts.Handle("/ephemeral", api.APISessionRequired(createEphemeralPost)).Methods("POST")
api.BaseRoutes.Post.Handle("/edit_history", api.APISessionRequired(getEditHistoryForPost)).Methods("GET")
api.BaseRoutes.Post.Handle("/thread", api.APISessionRequired(getPostThread)).Methods("GET")
api.BaseRoutes.Post.Handle("/info", api.APISessionRequired(getPostInfo)).Methods("GET")
api.BaseRoutes.Post.Handle("/files/info", api.APISessionRequired(getFileInfosForPost)).Methods("GET")
@@ -495,6 +496,39 @@ func getPostsByIds(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
func getEditHistoryForPost(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequirePostId()
if c.Err != nil {
return
}
if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.PostId, model.PermissionEditPost) {
c.SetPermissionError(model.PermissionEditPost)
return
}
originalPost, err := c.App.GetSinglePost(c.Params.PostId, false)
if err != nil {
c.SetPermissionError(model.PermissionEditPost)
return
}
if c.AppContext.Session().UserId != originalPost.UserId {
c.SetPermissionError(model.PermissionEditPost)
return
}
postsList, err := c.App.GetEditHistoryForPost(c.Params.PostId)
if err != nil {
c.Err = err
return
}
if err := json.NewEncoder(w).Encode(postsList); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
}
func deletePost(c *Context, w http.ResponseWriter, _ *http.Request) {
c.RequirePostId()
if c.Err != nil {

Просмотреть файл

@@ -3073,6 +3073,73 @@ func TestGetPostsByIds(t *testing.T) {
CheckNotFoundStatus(t, response)
}
func TestGetEditHistoryForPost(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
client := th.Client
post := &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "new message",
UserId: th.BasicUser.Id,
}
rpost, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
time.Sleep(1 * time.Millisecond)
t.Run("unedited post", func(t *testing.T) {
history, resp, err := client.GetEditHistoryForPost(rpost.Id)
require.Error(t, err)
CheckNotFoundStatus(t, resp)
require.Len(t, history, 0)
})
// update the post message
patch := &model.PostPatch{
Message: model.NewString("new message edited"),
}
// Patch the post
_, response1, err1 := client.PatchPost(rpost.Id, patch)
require.NoError(t, err1)
CheckOKStatus(t, response1)
// update the post message again
patch = &model.PostPatch{
Message: model.NewString("new message edited again"),
}
_, response2, err2 := client.PatchPost(rpost.Id, patch)
require.NoError(t, err2)
CheckOKStatus(t, response2)
t.Run("update history correctly", func(t *testing.T) {
history, response3, err3 := client.GetEditHistoryForPost(rpost.Id)
require.NoError(t, err3)
CheckOKStatus(t, response3)
require.Len(t, history, 2)
require.Equal(t, "new message edited", history[0].Message)
require.Equal(t, "new message", history[1].Message)
})
t.Run("logged out", func(t *testing.T) {
client.Logout()
_, resp, err := client.GetEditHistoryForPost(rpost.Id)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})
t.Run("different user", func(t *testing.T) {
th.LoginBasic2()
_, resp, err := client.GetEditHistoryForPost(rpost.Id)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
}
func TestCreatePostNotificationsWithCRT(t *testing.T) {
th := Setup(t).InitBasic()
rpost := th.CreatePost()