2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) GetBlocks(parentID string, blockType string) ([]model.Block, error) {
|
|
|
|
if len(blockType) > 0 && len(parentID) > 0 {
|
|
|
|
return a.store.GetBlocksWithParentAndType(parentID, blockType)
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
if len(blockType) > 0 {
|
|
|
|
return a.store.GetBlocksWithType(blockType)
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
return a.store.GetBlocksWithParent(parentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetParentID(blockID string) (string, error) {
|
|
|
|
return a.store.GetParentID(blockID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) InsertBlock(block model.Block) error {
|
|
|
|
return a.store.InsertBlock(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) InsertBlocks(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := a.store.InsertBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-29 21:09:02 +01:00
|
|
|
|
2020-11-06 19:11:01 +01:00
|
|
|
a.wsServer.BroadcastBlockChange(block)
|
2020-11-02 18:07:05 +01:00
|
|
|
go a.webhook.NotifyUpdate(block)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetSubTree(blockID string) ([]model.Block, error) {
|
|
|
|
return a.store.GetSubTree(blockID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetAllBlocks() ([]model.Block, error) {
|
|
|
|
return a.store.GetAllBlocks()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DeleteBlock(blockID string) error {
|
2020-10-22 15:22:36 +02:00
|
|
|
blockIDsToNotify := []string{blockID}
|
2020-10-21 11:32:13 +02:00
|
|
|
parentID, err := a.GetParentID(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parentID) > 0 {
|
|
|
|
blockIDsToNotify = append(blockIDsToNotify, parentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.store.DeleteBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:11:01 +01:00
|
|
|
a.wsServer.BroadcastBlockDelete(blockID, parentID)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
return nil
|
|
|
|
}
|