2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-03-22 15:24:34 +01:00
|
|
|
"errors"
|
2022-02-04 23:12:28 +01:00
|
|
|
"fmt"
|
2021-12-10 14:28:52 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-09-13 21:36:36 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/notify"
|
2022-02-04 23:12:28 +01:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-21 11:32:13 +02:00
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
var ErrBlocksFromMultipleBoards = errors.New("the block set contain blocks from multiple boards")
|
|
|
|
|
|
|
|
func (a *App) GetBlocks(boardID, parentID string, blockType string) ([]model.Block, error) {
|
|
|
|
if boardID == "" {
|
|
|
|
return []model.Block{}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 17:46:00 +02:00
|
|
|
if blockType != "" && parentID != "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
return a.store.GetBlocksWithParentAndType(boardID, parentID, blockType)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-08-02 17:46:00 +02:00
|
|
|
if blockType != "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
return a.store.GetBlocksWithType(boardID, blockType)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
return a.store.GetBlocksWithParent(boardID, parentID)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) DuplicateBlock(boardID string, blockID string, userID string, asTemplate bool) ([]model.Block, error) {
|
|
|
|
board, err := a.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
return nil, fmt.Errorf("cannot fetch board %s for DuplicateBlock: %w", boardID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks, err := a.store.DuplicateBlock(boardID, blockID, userID, asTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-13 23:04:49 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
for _, block := range blocks {
|
|
|
|
a.wsAdapter.BroadcastBlockChange(board.TeamID, block)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return blocks, err
|
2021-01-13 03:49:08 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) GetBlocksWithBoardID(boardID string) ([]model.Block, error) {
|
|
|
|
return a.store.GetBlocksWithBoardID(boardID)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) PatchBlock(blockID string, blockPatch *model.BlockPatch, modifiedByID string) error {
|
|
|
|
oldBlock, err := a.store.GetBlock(blockID)
|
2021-09-13 21:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.store.GetBoard(oldBlock.BoardID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.store.PatchBlock(blockID, blockPatch, modifiedByID)
|
2021-08-06 14:10:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-13 21:36:36 +02:00
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
a.metrics.IncrementBlocksPatched(1)
|
2022-03-22 15:24:34 +01:00
|
|
|
block, err := a.store.GetBlock(blockID)
|
2021-08-06 14:10:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
// broadcast on websocket
|
|
|
|
a.wsAdapter.BroadcastBlockChange(board.TeamID, *block)
|
|
|
|
|
|
|
|
// broadcast on webhooks
|
2021-09-13 21:36:36 +02:00
|
|
|
a.webhook.NotifyUpdate(*block)
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
// send notifications
|
|
|
|
a.notifyBlockChanged(notify.Update, block, oldBlock, modifiedByID)
|
|
|
|
return nil
|
|
|
|
})
|
2021-08-06 14:10:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) PatchBlocks(teamID string, blockPatches *model.BlockPatchBatch, modifiedByID string) error {
|
2021-12-10 15:17:00 +01:00
|
|
|
oldBlocks := make([]model.Block, 0, len(blockPatches.BlockIDs))
|
|
|
|
for _, blockID := range blockPatches.BlockIDs {
|
2022-03-22 15:24:34 +01:00
|
|
|
oldBlock, err := a.store.GetBlock(blockID)
|
2021-12-10 15:17:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
oldBlocks = append(oldBlocks, *oldBlock)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err := a.store.PatchBlocks(blockPatches, modifiedByID)
|
2021-12-10 15:17:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
a.metrics.IncrementBlocksPatched(len(oldBlocks))
|
|
|
|
for i, blockID := range blockPatches.BlockIDs {
|
|
|
|
newBlock, err := a.store.GetBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a.wsAdapter.BroadcastBlockChange(teamID, *newBlock)
|
2021-12-10 15:17:00 +01:00
|
|
|
a.webhook.NotifyUpdate(*newBlock)
|
2022-03-22 15:24:34 +01:00
|
|
|
a.notifyBlockChanged(notify.Update, newBlock, &oldBlocks[i], modifiedByID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-12-10 15:17:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) InsertBlock(block model.Block, modifiedByID string) error {
|
|
|
|
board, bErr := a.store.GetBoard(block.BoardID)
|
|
|
|
if bErr != nil {
|
|
|
|
return bErr
|
|
|
|
}
|
|
|
|
|
|
|
|
err := a.store.InsertBlock(&block, modifiedByID)
|
2021-06-04 16:38:49 +02:00
|
|
|
if err == nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
a.wsAdapter.BroadcastBlockChange(board.TeamID, block)
|
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
2021-09-13 21:36:36 +02:00
|
|
|
a.webhook.NotifyUpdate(block)
|
2022-03-22 15:24:34 +01:00
|
|
|
a.notifyBlockChanged(notify.Add, &block, nil, modifiedByID)
|
|
|
|
return nil
|
|
|
|
})
|
2021-06-04 16:38:49 +02:00
|
|
|
}
|
|
|
|
return err
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) InsertBlocks(blocks []model.Block, modifiedByID string, allowNotifications bool) ([]model.Block, error) {
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
return []model.Block{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// all blocks must belong to the same board
|
|
|
|
boardID := blocks[0].BoardID
|
|
|
|
for _, block := range blocks {
|
|
|
|
if block.BoardID != boardID {
|
|
|
|
return nil, ErrBlocksFromMultipleBoards
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := a.store.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:36:36 +02:00
|
|
|
needsNotify := make([]model.Block, 0, len(blocks))
|
2021-07-08 16:36:43 +02:00
|
|
|
for i := range blocks {
|
2022-03-22 15:24:34 +01:00
|
|
|
err := a.store.InsertBlock(&blocks[i], modifiedByID)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
2021-11-05 11:54:27 +01:00
|
|
|
return nil, err
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
2021-09-13 21:36:36 +02:00
|
|
|
needsNotify = append(needsNotify, blocks[i])
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.wsAdapter.BroadcastBlockChange(board.TeamID, blocks[i])
|
2021-09-13 21:36:36 +02:00
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
2021-09-13 21:36:36 +02:00
|
|
|
for _, b := range needsNotify {
|
|
|
|
block := b
|
|
|
|
a.webhook.NotifyUpdate(block)
|
2021-10-15 23:09:43 +02:00
|
|
|
if allowNotifications {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.notifyBlockChanged(notify.Add, &block, nil, modifiedByID)
|
2021-10-15 23:09:43 +02:00
|
|
|
}
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
return nil
|
|
|
|
})
|
2021-09-13 21:36:36 +02:00
|
|
|
|
2021-11-05 11:54:27 +01:00
|
|
|
return blocks, nil
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) CopyCardFiles(sourceBoardID string, blocks []model.Block) error {
|
2022-02-02 18:12:59 +01:00
|
|
|
// Images attached in cards have a path comprising the card's board ID.
|
|
|
|
// When we create a template from this board, we need to copy the files
|
|
|
|
// with the new board ID in path.
|
|
|
|
// Not doing so causing images in templates (and boards created from this
|
|
|
|
// template) to fail to load.
|
|
|
|
|
2022-02-04 23:12:28 +01:00
|
|
|
// look up ID of source board, which may be different than the blocks.
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.GetBlockByID(sourceBoardID)
|
2022-02-04 23:12:28 +01:00
|
|
|
if err != nil || board == nil {
|
|
|
|
return fmt.Errorf("cannot fetch board %s for CopyCardFiles: %w", sourceBoardID, err)
|
|
|
|
}
|
|
|
|
|
2022-02-02 18:12:59 +01:00
|
|
|
for i := range blocks {
|
|
|
|
block := blocks[i]
|
|
|
|
|
|
|
|
fileName, ok := block.Fields["fileId"]
|
|
|
|
if block.Type == model.TypeImage && ok {
|
2022-02-04 23:12:28 +01:00
|
|
|
// create unique filename in case we are copying cards within the same board.
|
|
|
|
ext := filepath.Ext(fileName.(string))
|
|
|
|
destFilename := utils.NewID(utils.IDTypeNone) + ext
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
sourceFilePath := filepath.Join(sourceBoardID, fileName.(string))
|
|
|
|
destinationFilePath := filepath.Join(block.BoardID, destFilename)
|
2022-02-04 23:12:28 +01:00
|
|
|
|
|
|
|
a.logger.Debug(
|
|
|
|
"Copying card file",
|
|
|
|
mlog.String("sourceFilePath", sourceFilePath),
|
|
|
|
mlog.String("destinationFilePath", destinationFilePath),
|
|
|
|
)
|
|
|
|
|
2022-02-02 18:12:59 +01:00
|
|
|
if err := a.filesBackend.CopyFile(sourceFilePath, destinationFilePath); err != nil {
|
|
|
|
a.logger.Error(
|
|
|
|
"CopyCardFiles failed to copy file",
|
|
|
|
mlog.String("sourceFilePath", sourceFilePath),
|
|
|
|
mlog.String("destinationFilePath", destinationFilePath),
|
|
|
|
mlog.Err(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-02-04 23:12:28 +01:00
|
|
|
block.Fields["fileId"] = destFilename
|
2022-02-02 18:12:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) GetSubTree(boardID, blockID string, levels int, opts model.QuerySubtreeOptions) ([]model.Block, error) {
|
2020-11-12 19:16:59 +01:00
|
|
|
// Only 2 or 3 levels are supported for now
|
|
|
|
if levels >= 3 {
|
2022-03-22 15:24:34 +01:00
|
|
|
return a.store.GetSubTree3(boardID, blockID, opts)
|
2020-11-12 19:16:59 +01:00
|
|
|
}
|
2020-10-21 11:32:13 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
return a.store.GetSubTree2(boardID, blockID, opts)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) GetBlockByID(blockID string) (*model.Block, error) {
|
|
|
|
return a.store.GetBlock(blockID)
|
2022-02-02 01:01:29 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) DeleteBlock(blockID string, modifiedBy string) error {
|
|
|
|
block, err := a.store.GetBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := a.store.GetBoard(block.BoardID)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:02:06 +01:00
|
|
|
if block == nil {
|
|
|
|
// deleting non-existing block not considered an error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err = a.store.DeleteBlock(blockID, modifiedBy)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 14:28:52 +01:00
|
|
|
if block.Type == model.TypeImage {
|
|
|
|
fileName, fileIDExists := block.Fields["fileId"]
|
|
|
|
if fileName, fileIDIsString := fileName.(string); fileIDExists && fileIDIsString {
|
2022-03-22 15:24:34 +01:00
|
|
|
filePath := filepath.Join(block.BoardID, fileName)
|
2021-12-10 14:28:52 +01:00
|
|
|
err = a.filesBackend.RemoveFile(filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Error("Error deleting image file",
|
|
|
|
mlog.String("FilePath", filePath),
|
|
|
|
mlog.Err(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
a.wsAdapter.BroadcastBlockDelete(board.TeamID, blockID, block.BoardID)
|
|
|
|
a.metrics.IncrementBlocksDeleted(1)
|
|
|
|
a.notifyBlockChanged(notify.Delete, block, block, modifiedBy)
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-21 11:32:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-04 16:38:49 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) UndeleteBlock(blockID string, modifiedBy string) error {
|
|
|
|
blocks, err := a.store.GetBlockHistory(blockID, model.QueryBlockHistoryOptions{Limit: 1, Descending: true})
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blocks) == 0 {
|
2022-03-22 15:24:34 +01:00
|
|
|
// undeleting non-existing block not considered an error
|
2022-02-22 18:42:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err = a.store.UndeleteBlock(blockID, modifiedBy)
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
block, err := a.store.GetBlock(blockID)
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
a.logger.Error("Error loading the block after undelete, not propagating through websockets or notifications")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.store.GetBoard(block.BoardID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
a.wsAdapter.BroadcastBlockChange(board.TeamID, *block)
|
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
2022-02-22 18:42:49 +01:00
|
|
|
a.webhook.NotifyUpdate(*block)
|
2022-03-22 15:24:34 +01:00
|
|
|
a.notifyBlockChanged(notify.Add, block, nil, modifiedBy)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-02-22 18:42:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
func (a *App) GetBlockCountsByType() (map[string]int64, error) {
|
|
|
|
return a.store.GetBlockCountsByType()
|
|
|
|
}
|
2021-09-13 21:36:36 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *App) GetBlocksForBoard(boardID string) ([]model.Block, error) {
|
|
|
|
return a.store.GetBlocksForBoard(boardID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) notifyBlockChanged(action notify.Action, block *model.Block, oldBlock *model.Block, modifiedByID string) {
|
2021-09-13 21:36:36 +02:00
|
|
|
if a.notifications == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find card and board for the changed block.
|
2022-03-22 15:24:34 +01:00
|
|
|
board, card, err := a.getBoardAndCard(block)
|
2021-09-13 21:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
a.logger.Error("Error notifying for block change; cannot determine board or card", mlog.Err(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
evt := notify.BlockChangeEvent{
|
|
|
|
Action: action,
|
2022-03-22 15:24:34 +01:00
|
|
|
TeamID: board.TeamID,
|
2021-09-13 21:36:36 +02:00
|
|
|
Board: board,
|
|
|
|
Card: card,
|
|
|
|
BlockChanged: block,
|
|
|
|
BlockOld: oldBlock,
|
2021-12-10 16:46:37 +01:00
|
|
|
ModifiedByID: modifiedByID,
|
2021-09-13 21:36:36 +02:00
|
|
|
}
|
|
|
|
a.notifications.BlockChanged(evt)
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
maxSearchDepth = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
// getBoardAndCard returns the first parent of type `card` its board for the specified block.
|
|
|
|
// `board` and/or `card` may return nil without error if the block does not belong to a board or card.
|
|
|
|
func (a *App) getBoardAndCard(block *model.Block) (board *model.Board, card *model.Block, err error) {
|
|
|
|
board, err = a.store.GetBoard(block.BoardID)
|
|
|
|
if err != nil {
|
|
|
|
return board, card, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int // don't let invalid blocks hierarchy cause infinite loop.
|
|
|
|
iter := block
|
|
|
|
for {
|
|
|
|
count++
|
|
|
|
if card == nil && iter.Type == model.TypeCard {
|
|
|
|
card = iter
|
|
|
|
}
|
|
|
|
|
|
|
|
if iter.ParentID == "" || (board != nil && card != nil) || count > maxSearchDepth {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err = a.store.GetBlock(iter.ParentID)
|
|
|
|
if err != nil || iter == nil {
|
|
|
|
return board, card, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return board, card, nil
|
|
|
|
}
|