MM-13718 Prevent files from being attached to multiple posts (#10094)
* MM-13718 Prevent files from being attached to multiple posts * Switch back to non-batched AttachToPost * Change status code when failing to attach a file
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
f12680103a
Коммит
6325c5b569
@@ -1028,7 +1028,7 @@ func (a *App) uploadAttachments(attachments *[]AttachmentImportData, post *model
|
||||
|
||||
func (a *App) UpdateFileInfoWithPostId(post *model.Post) {
|
||||
for _, fileId := range post.FileIds {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); result.Err != nil {
|
||||
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, result.Err), mlog.String("post_id", post.Id))
|
||||
}
|
||||
}
|
||||
|
||||
34
app/post.go
34
app/post.go
@@ -273,13 +273,8 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
|
||||
}
|
||||
|
||||
if len(post.FileIds) > 0 {
|
||||
// There's a rare bug where the client sends up duplicate FileIds so protect against that
|
||||
post.FileIds = utils.RemoveDuplicatesFromStringArray(post.FileIds)
|
||||
|
||||
for _, fileId := range post.FileIds {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil {
|
||||
mlog.Error(fmt.Sprintf("Encountered error attaching files to post, post_id=%s, user_id=%s, file_ids=%v, err=%v", post.Id, post.FileIds, post.UserId, result.Err), mlog.String("post_id", post.Id))
|
||||
}
|
||||
if err := a.attachFilesToPost(post); err != nil {
|
||||
mlog.Error("Encountered error attaching files to post", mlog.String("post_id", post.Id), mlog.Any("file_ids", post.FileIds), mlog.Err(result.Err))
|
||||
}
|
||||
|
||||
if a.Metrics != nil {
|
||||
@@ -298,6 +293,31 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
|
||||
return rpost, nil
|
||||
}
|
||||
|
||||
func (a *App) attachFilesToPost(post *model.Post) *model.AppError {
|
||||
var attachedIds []string
|
||||
for _, fileId := range post.FileIds {
|
||||
result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId)
|
||||
if result.Err != nil {
|
||||
mlog.Warn("Failed to attach file to post", mlog.String("file_id", fileId), mlog.String("post_id", post.Id), mlog.Err(result.Err))
|
||||
continue
|
||||
}
|
||||
|
||||
attachedIds = append(attachedIds, fileId)
|
||||
}
|
||||
|
||||
if len(post.FileIds) != len(attachedIds) {
|
||||
// We couldn't attach all files to the post, so ensure that post.FileIds reflects what was actually attached
|
||||
post.FileIds = attachedIds
|
||||
|
||||
result := <-a.Srv.Store.Post().Overwrite(post)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FillInPostProps should be invoked before saving posts to fill in properties such as
|
||||
// channel_mentions.
|
||||
//
|
||||
|
||||
@@ -187,6 +187,63 @@ func TestCreatePostDeduplicate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAttachFilesToPost(t *testing.T) {
|
||||
t.Run("should attach files", func(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
info1 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Path: "path.txt",
|
||||
})).(*model.FileInfo)
|
||||
info2 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Path: "path.txt",
|
||||
})).(*model.FileInfo)
|
||||
|
||||
post := th.BasicPost
|
||||
post.FileIds = []string{info1.Id, info2.Id}
|
||||
|
||||
err := th.App.attachFilesToPost(post)
|
||||
assert.Nil(t, err)
|
||||
|
||||
infos, err := th.App.GetFileInfosForPost(post.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, infos, 2)
|
||||
})
|
||||
|
||||
t.Run("should update File.PostIds after failing to add files", func(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
info1 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Path: "path.txt",
|
||||
PostId: model.NewId(),
|
||||
})).(*model.FileInfo)
|
||||
info2 := store.Must(th.App.Srv.Store.FileInfo().Save(&model.FileInfo{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
Path: "path.txt",
|
||||
})).(*model.FileInfo)
|
||||
|
||||
post := th.BasicPost
|
||||
post.FileIds = []string{info1.Id, info2.Id}
|
||||
|
||||
err := th.App.attachFilesToPost(post)
|
||||
assert.Nil(t, err)
|
||||
|
||||
infos, err := th.App.GetFileInfosForPost(post.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, infos, 1)
|
||||
assert.Equal(t, info2.Id, infos[0].Id)
|
||||
|
||||
updated, err := th.App.GetSinglePost(post.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Len(t, updated.FileIds, 1)
|
||||
assert.Contains(t, updated.FileIds, info2.Id)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdatePostEditAt(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
@@ -248,7 +248,7 @@ func (a *App) SlackAddPosts(teamId string, channel *model.Channel, posts []Slack
|
||||
}
|
||||
a.OldImportPost(&newPost)
|
||||
for _, fileId := range newPost.FileIds {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, newPost.Id); result.Err != nil {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, newPost.Id, newPost.UserId); result.Err != nil {
|
||||
mlog.Error(fmt.Sprintf("Slack Import: An error occurred when attaching files to a message, post_id=%s, file_ids=%v, err=%v.", newPost.Id, newPost.FileIds, result.Err))
|
||||
}
|
||||
}
|
||||
@@ -723,7 +723,7 @@ func (a *App) OldImportPost(post *model.Post) {
|
||||
}
|
||||
|
||||
for _, fileId := range post.FileIds {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil {
|
||||
if result := <-a.Srv.Store.FileInfo().AttachToPost(fileId, post.Id, post.UserId); result.Err != nil {
|
||||
mlog.Error(fmt.Sprintf("Error attaching files to post. postId=%v, fileIds=%v, message=%v", post.Id, post.FileIds, result.Err), mlog.String("post_id", post.Id))
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user