2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
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"
|
2021-03-26 19:01:54 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-09-13 21:36:36 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-21 11:32:13 +02:00
|
|
|
)
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) GetBlocks(c store.Container, parentID string, blockType string) ([]model.Block, error) {
|
2021-08-02 17:46:00 +02:00
|
|
|
if blockType != "" && parentID != "" {
|
2021-03-26 19:01:54 +01:00
|
|
|
return a.store.GetBlocksWithParentAndType(c, 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 != "" {
|
2021-03-26 19:01:54 +01:00
|
|
|
return a.store.GetBlocksWithType(c, blockType)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
return a.store.GetBlocksWithParent(c, parentID)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-22 18:13:31 +02:00
|
|
|
func (a *App) GetBlockWithID(c store.Container, blockID string) (*model.Block, error) {
|
|
|
|
return a.store.GetBlock(c, blockID)
|
|
|
|
}
|
|
|
|
|
2021-05-13 23:04:49 +02:00
|
|
|
func (a *App) GetBlocksWithRootID(c store.Container, rootID string) ([]model.Block, error) {
|
|
|
|
return a.store.GetBlocksWithRootID(c, rootID)
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) GetRootID(c store.Container, blockID string) (string, error) {
|
|
|
|
return a.store.GetRootID(c, blockID)
|
2021-01-13 03:49:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) GetParentID(c store.Container, blockID string) (string, error) {
|
|
|
|
return a.store.GetParentID(c, blockID)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (a *App) PatchBlock(c store.Container, blockID string, blockPatch *model.BlockPatch, userID string) error {
|
2021-09-13 21:36:36 +02:00
|
|
|
oldBlock, err := a.store.GetBlock(c, blockID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.store.PatchBlock(c, blockID, blockPatch, userID)
|
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)
|
|
|
|
block, err := a.store.GetBlock(c, blockID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-27 10:59:14 +02:00
|
|
|
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, *block)
|
2021-09-13 21:36:36 +02:00
|
|
|
go func() {
|
|
|
|
a.webhook.NotifyUpdate(*block)
|
|
|
|
a.notifyBlockChanged(notify.Update, c, block, oldBlock, userID)
|
|
|
|
}()
|
2021-08-06 14:10:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 15:17:00 +01:00
|
|
|
func (a *App) PatchBlocks(c store.Container, blockPatches *model.BlockPatchBatch, userID string) error {
|
|
|
|
oldBlocks := make([]model.Block, 0, len(blockPatches.BlockIDs))
|
|
|
|
for _, blockID := range blockPatches.BlockIDs {
|
|
|
|
oldBlock, err := a.store.GetBlock(c, blockID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
oldBlocks = append(oldBlocks, *oldBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := a.store.PatchBlocks(c, blockPatches, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.metrics.IncrementBlocksPatched(len(oldBlocks))
|
|
|
|
for i, blockID := range blockPatches.BlockIDs {
|
|
|
|
newBlock, err := a.store.GetBlock(c, blockID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, *newBlock)
|
|
|
|
go func(currentIndex int) {
|
|
|
|
a.webhook.NotifyUpdate(*newBlock)
|
|
|
|
a.notifyBlockChanged(notify.Update, c, newBlock, &oldBlocks[currentIndex], userID)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
func (a *App) InsertBlock(c store.Container, block model.Block, userID string) error {
|
|
|
|
err := a.store.InsertBlock(c, &block, userID)
|
2021-06-04 16:38:49 +02:00
|
|
|
if err == nil {
|
2021-09-13 21:36:36 +02:00
|
|
|
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, block)
|
2021-06-04 16:38:49 +02:00
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
2021-09-13 21:36:36 +02:00
|
|
|
go func() {
|
|
|
|
a.webhook.NotifyUpdate(block)
|
|
|
|
a.notifyBlockChanged(notify.Add, c, &block, nil, userID)
|
|
|
|
}()
|
2021-06-04 16:38:49 +02:00
|
|
|
}
|
|
|
|
return err
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:54:27 +01:00
|
|
|
func (a *App) InsertBlocks(c store.Container, blocks []model.Block, userID string, allowNotifications bool) ([]model.Block, error) {
|
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 {
|
|
|
|
err := a.store.InsertBlock(c, &blocks[i], userID)
|
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-22 21:57:00 +02:00
|
|
|
blocks[i].WorkspaceID = c.WorkspaceID
|
2021-09-13 21:36:36 +02:00
|
|
|
needsNotify = append(needsNotify, blocks[i])
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, blocks[i])
|
2021-09-13 21:36:36 +02:00
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 21:36:36 +02:00
|
|
|
go func() {
|
|
|
|
for _, b := range needsNotify {
|
|
|
|
block := b
|
|
|
|
a.webhook.NotifyUpdate(block)
|
2021-10-15 23:09:43 +02:00
|
|
|
if allowNotifications {
|
|
|
|
a.notifyBlockChanged(notify.Add, c, &block, nil, userID)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) GetSubTree(c store.Container, blockID string, levels int) ([]model.Block, error) {
|
2020-11-12 19:16:59 +01:00
|
|
|
// Only 2 or 3 levels are supported for now
|
|
|
|
if levels >= 3 {
|
2021-03-26 19:01:54 +01:00
|
|
|
return a.store.GetSubTree3(c, blockID)
|
2020-11-12 19:16:59 +01:00
|
|
|
}
|
2021-10-22 18:13:31 +02:00
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
return a.store.GetSubTree2(c, blockID)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) GetAllBlocks(c store.Container) ([]model.Block, error) {
|
|
|
|
return a.store.GetAllBlocks(c)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) DeleteBlock(c store.Container, blockID string, modifiedBy string) error {
|
2021-09-13 21:36:36 +02:00
|
|
|
block, err := a.store.GetBlock(c, blockID)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
err = a.store.DeleteBlock(c, 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 {
|
|
|
|
filePath := filepath.Join(block.WorkspaceID, block.RootID, fileName)
|
|
|
|
err = a.filesBackend.RemoveFile(filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Error("Error deleting image file",
|
|
|
|
mlog.String("FilePath", filePath),
|
|
|
|
mlog.Err(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:36:36 +02:00
|
|
|
a.wsAdapter.BroadcastBlockDelete(c.WorkspaceID, blockID, block.ParentID)
|
2021-06-04 16:38:49 +02:00
|
|
|
a.metrics.IncrementBlocksDeleted(1)
|
2021-09-13 21:36:36 +02:00
|
|
|
go func() {
|
|
|
|
a.notifyBlockChanged(notify.Update, c, block, block, modifiedBy)
|
|
|
|
}()
|
2020-10-21 11:32:13 +02: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
|
|
|
|
|
|
|
func (a *App) notifyBlockChanged(action notify.Action, c store.Container, block *model.Block, oldBlock *model.Block, userID string) {
|
|
|
|
if a.notifications == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find card and board for the changed block.
|
|
|
|
board, card, err := a.getBoardAndCard(c, block)
|
|
|
|
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,
|
|
|
|
Workspace: c.WorkspaceID,
|
|
|
|
Board: board,
|
|
|
|
Card: card,
|
|
|
|
BlockChanged: block,
|
|
|
|
BlockOld: oldBlock,
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
a.notifications.BlockChanged(evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxSearchDepth = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
// getBoardAndCard returns the first parent of type `card` and first parent of type `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(c store.Container, block *model.Block) (board *model.Block, card *model.Block, err error) {
|
|
|
|
var count int // don't let invalid blocks hierarchy cause infinite loop.
|
|
|
|
iter := block
|
|
|
|
for {
|
|
|
|
count++
|
2021-10-27 03:11:19 +02:00
|
|
|
if board == nil && iter.Type == model.TypeBoard {
|
2021-09-13 21:36:36 +02:00
|
|
|
board = iter
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:11:19 +02:00
|
|
|
if card == nil && iter.Type == model.TypeCard {
|
2021-09-13 21:36:36 +02:00
|
|
|
card = iter
|
|
|
|
}
|
|
|
|
|
|
|
|
if iter.ParentID == "" || (board != nil && card != nil) || count > maxSearchDepth {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err = a.store.GetBlock(c, iter.ParentID)
|
|
|
|
if err != nil {
|
|
|
|
return board, card, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return board, card, nil
|
|
|
|
}
|