Files
team/server/channels/app/post_file_change.go
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

71 lines
3.0 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/shared/request"
"github.com/hanzoteam/server/server/public/utils"
)
func (a *App) processPostFileChanges(rctx request.CTX, newPost, oldPost *model.Post, updatePostOptions *model.UpdatePostOptions) (model.StringArray, *model.AppError) {
newFileIDs := model.RemoveDuplicateStrings(newPost.FileIds)
oldFileIDs := model.RemoveDuplicateStrings(oldPost.FileIds)
addedFileIDs, removedFileIDs, unchangedFileIDs := utils.FindExclusives(newFileIDs, oldFileIDs)
if len(addedFileIDs) > 0 {
if updatePostOptions != nil && updatePostOptions.IsRestorePost {
err := a.Srv().Store().FileInfo().RestoreForPostByIds(rctx, newPost.Id, addedFileIDs)
if err != nil {
return nil, model.NewAppError("app.processPostFileChanges", "app.file_info.undelete_for_post_ids.app_error", map[string]any{"post_id": newPost.Id}, "", 0).Wrap(err)
}
} else {
a.attachNewFilesToPost(rctx, newPost, addedFileIDs, unchangedFileIDs)
}
}
if len(removedFileIDs) > 0 {
if appErr := a.detachFilesFromPost(rctx, newPost.Id, removedFileIDs); appErr != nil {
return nil, appErr
}
}
filesChanged := len(addedFileIDs) > 0 || len(removedFileIDs) > 0
if filesChanged {
// if files were modified, invalidate the file metadata cache for the post
// so that the updated file metadata can be returned.
a.Srv().Store().FileInfo().InvalidateFileInfosForPostCache(newPost.Id, false)
}
return newPost.FileIds, nil
}
func (a *App) attachNewFilesToPost(rctx request.CTX, post *model.Post, addedFileIDs, unchangedFileIDs []string) {
// for newly added files, we need to attach them to the post
// intentionally using UserID from session instead of post.UserID
// to support admin attaching files in someone else's post.
// Admins can edit other's posts, including message, removing existing files,
// and attaching new files.
// When an admin uploads new files, they are associated with their user ID. So, when attaching
// these file to a post, we need to search for their FileInfo entry
// by the admin's user ID and not the post author's user ID.
userId := rctx.Session().UserId
attachedFileIDs := a.attachFileIDsToPost(rctx, post.Id, post.ChannelId, userId, addedFileIDs)
if len(attachedFileIDs) != len(addedFileIDs) {
// if not all files could be attached, the final list of files
// is those that could be attached + the existing, unchanged files
post.FileIds = append(attachedFileIDs, unchangedFileIDs...)
}
}
func (a *App) detachFilesFromPost(rctx request.CTX, postId string, removedFileIDs []string) *model.AppError {
if err := a.Srv().Store().FileInfo().DeleteForPostByIds(rctx, postId, removedFileIDs); err != nil {
return model.NewAppError("app.detachFilesFromPost", "app.file_info.delete_for_post_ids.app_error", map[string]any{"post_id": postId}, "", 0).Wrap(err)
}
return nil
}