Skip to content

Commit

Permalink
Update root post update at on reply delete (mattermost#28688)
Browse files Browse the repository at this point in the history
* Update root post update at on reply delete

* Address feedback
  • Loading branch information
larkox authored Oct 11, 2024
1 parent d634383 commit 42c9905
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/channels/store/sqlstore/post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,13 @@ func (s *SqlPostStore) Delete(rctx request.CTX, postID string, time int64, delet
err = s.deleteThread(transaction, postID, time)
} else {
err = s.updateThreadAfterReplyDeletion(transaction, id.RootId, id.UserId)
updatePostQuery := s.getQueryBuilder().
Update("Posts").
Set("UpdateAt", time).
Where(sq.Eq{"Id": id.RootId})
if _, err = transaction.ExecBuilder(updatePostQuery); err != nil {
mlog.Warn("Error updating Post UpdateAt.", mlog.Err(err))
}
}

if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions server/channels/store/storetest/post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,66 @@ func testPostStoreDelete(t *testing.T, rctx request.CTX, ss store.Store) {
require.NoError(t, err)
})

t.Run("root post update at is updated upon reply delete", func(t *testing.T) {
teamId := model.NewId()
channel, err := ss.Channel().Save(rctx, &model.Channel{
TeamId: teamId,
DisplayName: "DisplayName1",
Name: "channel" + model.NewId(),
Type: model.ChannelTypeOpen,
}, -1)
require.NoError(t, err)

// Create a root post
rootPost1, err := ss.Post().Save(rctx, &model.Post{
ChannelId: channel.Id,
UserId: model.NewId(),
Message: NewTestId(),
})
require.NoError(t, err)

// Reply to that root post
_, err = ss.Post().Save(rctx, &model.Post{
ChannelId: rootPost1.ChannelId,
UserId: model.NewId(),
Message: NewTestId(),
RootId: rootPost1.Id,
})
require.NoError(t, err)

// Reply to that root post a second time
replyPost2, err := ss.Post().Save(rctx, &model.Post{
ChannelId: rootPost1.ChannelId,
UserId: model.NewId(),
Message: NewTestId(),
RootId: rootPost1.Id,
})
require.NoError(t, err)

// Reply to that root post a third time
_, err = ss.Post().Save(rctx, &model.Post{
ChannelId: rootPost1.ChannelId,
UserId: model.NewId(),
Message: NewTestId(),
RootId: rootPost1.Id,
})
require.NoError(t, err)

updatedRootPost, err := ss.Post().GetSingle(rctx, rootPost1.Id, false)
require.NoError(t, err)

beforeDeleteTime := updatedRootPost.UpdateAt

// Delete the reply previous to last
err = ss.Post().Delete(rctx, replyPost2.Id, model.GetMillis(), "")
require.NoError(t, err)

updatedRootPost, err = ss.Post().GetSingle(rctx, rootPost1.Id, false)
require.NoError(t, err)

require.Greater(t, updatedRootPost.UpdateAt, beforeDeleteTime)
})

t.Run("thread with multiple replies, update thread last reply at", func(t *testing.T) {
teamId := model.NewId()
channel, err := ss.Channel().Save(rctx, &model.Channel{
Expand Down

0 comments on commit 42c9905

Please sign in to comment.