2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-03-26 19:01:54 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
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) {
|
2020-10-21 11:32:13 +02:00
|
|
|
if len(blockType) > 0 && len(parentID) > 0 {
|
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
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
if len(blockType) > 0 {
|
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-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-03-26 19:01:54 +01:00
|
|
|
func (a *App) InsertBlock(c store.Container, block model.Block) error {
|
|
|
|
return a.store.InsertBlock(c, block)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *App) InsertBlocks(c store.Container, blocks []model.Block) error {
|
2020-10-22 15:22:36 +02:00
|
|
|
blockIDsToNotify := []string{}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
uniqueBlockIDs := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
if !uniqueBlockIDs[block.ID] {
|
|
|
|
blockIDsToNotify = append(blockIDsToNotify, block.ID)
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-26 18:54:37 +01:00
|
|
|
// ParentID as empty string denotes a block at the root
|
|
|
|
if !uniqueBlockIDs[block.ParentID] {
|
2020-10-21 11:32:13 +02:00
|
|
|
blockIDsToNotify = append(blockIDsToNotify, block.ParentID)
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
err := a.store.InsertBlock(c, block)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-29 21:09:02 +01:00
|
|
|
|
2021-03-27 02:04:23 +01:00
|
|
|
a.wsServer.BroadcastBlockChange(c.WorkspaceID, block)
|
2020-11-02 18:07:05 +01:00
|
|
|
go a.webhook.NotifyUpdate(block)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-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 {
|
2020-10-22 15:22:36 +02:00
|
|
|
blockIDsToNotify := []string{blockID}
|
2021-03-26 19:01:54 +01:00
|
|
|
parentID, err := a.GetParentID(c, blockID)
|
2020-10-21 11:32:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parentID) > 0 {
|
|
|
|
blockIDsToNotify = append(blockIDsToNotify, parentID)
|
|
|
|
}
|
|
|
|
|
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-03-27 02:04:23 +01:00
|
|
|
a.wsServer.BroadcastBlockDelete(c.WorkspaceID, blockID, parentID)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
return nil
|
|
|
|
}
|