2022-03-22 16:24:34 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/notify"
|
|
|
|
|
2024-06-07 20:00:08 +02:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2022-03-22 16:24:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) CreateBoardsAndBlocks(bab *model.BoardsAndBlocks, userID string, addMember bool) (*model.BoardsAndBlocks, error) {
|
|
|
|
var newBab *model.BoardsAndBlocks
|
|
|
|
var members []*model.BoardMember
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if addMember {
|
|
|
|
newBab, members, err = a.store.CreateBoardsAndBlocksWithAdmin(bab, userID)
|
|
|
|
} else {
|
|
|
|
newBab, err = a.store.CreateBoardsAndBlocks(bab, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// all new boards should belong to the same team
|
|
|
|
teamID := newBab.Boards[0].TeamID
|
|
|
|
|
|
|
|
// This can be synchronous because this action is not common
|
|
|
|
for _, board := range newBab.Boards {
|
|
|
|
a.wsAdapter.BroadcastBoardChange(teamID, board)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range newBab.Blocks {
|
|
|
|
b := block
|
|
|
|
a.wsAdapter.BroadcastBlockChange(teamID, b)
|
|
|
|
a.metrics.IncrementBlocksInserted(1)
|
|
|
|
a.webhook.NotifyUpdate(b)
|
2022-10-25 22:46:43 +02:00
|
|
|
a.notifyBlockChanged(notify.Add, b, nil, userID)
|
2022-03-22 16:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if addMember {
|
|
|
|
for _, member := range members {
|
|
|
|
a.wsAdapter.BroadcastMemberChange(teamID, member.BoardID, member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:08:03 +02:00
|
|
|
for _, board := range newBab.Boards {
|
|
|
|
if !board.IsTemplate {
|
|
|
|
if err := a.addBoardsToDefaultCategory(userID, board.TeamID, []*model.Board{board}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
return newBab, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) PatchBoardsAndBlocks(pbab *model.PatchBoardsAndBlocks, userID string) (*model.BoardsAndBlocks, error) {
|
2022-06-15 12:17:44 +02:00
|
|
|
oldBlocks, err := a.store.GetBlocksByIDs(pbab.BlockIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-25 22:46:43 +02:00
|
|
|
oldBlocksMap := map[string]*model.Block{}
|
2022-06-15 12:17:44 +02:00
|
|
|
for _, block := range oldBlocks {
|
|
|
|
oldBlocksMap[block.ID] = block
|
2022-03-22 16:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bab, err := a.store.PatchBoardsAndBlocks(pbab, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
teamID := bab.Boards[0].TeamID
|
|
|
|
|
|
|
|
for _, block := range bab.Blocks {
|
|
|
|
oldBlock, ok := oldBlocksMap[block.ID]
|
|
|
|
if !ok {
|
|
|
|
a.logger.Error("Error notifying for block change on patch boards and blocks; cannot get old block", mlog.String("blockID", block.ID))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b := block
|
|
|
|
a.metrics.IncrementBlocksPatched(1)
|
|
|
|
a.wsAdapter.BroadcastBlockChange(teamID, b)
|
|
|
|
a.webhook.NotifyUpdate(b)
|
2022-10-25 22:46:43 +02:00
|
|
|
a.notifyBlockChanged(notify.Update, b, oldBlock, userID)
|
2022-03-22 16:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, board := range bab.Boards {
|
|
|
|
a.wsAdapter.BroadcastBoardChange(board.TeamID, board)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return bab, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DeleteBoardsAndBlocks(dbab *model.DeleteBoardsAndBlocks, userID string) error {
|
|
|
|
firstBoard, err := a.store.GetBoard(dbab.Boards[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need the block entity to notify of the block changes, so we
|
|
|
|
// fetch and store the blocks first
|
|
|
|
blocks := []*model.Block{}
|
|
|
|
for _, blockID := range dbab.Blocks {
|
|
|
|
block, err := a.store.GetBlock(blockID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blocks = append(blocks, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.store.DeleteBoardsAndBlocks(dbab, userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.blockChangeNotifier.Enqueue(func() error {
|
|
|
|
for _, block := range blocks {
|
|
|
|
a.wsAdapter.BroadcastBlockDelete(firstBoard.TeamID, block.ID, block.BoardID)
|
|
|
|
a.metrics.IncrementBlocksDeleted(1)
|
|
|
|
a.notifyBlockChanged(notify.Update, block, block, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, boardID := range dbab.Boards {
|
|
|
|
a.wsAdapter.BroadcastBoardDelete(firstBoard.TeamID, boardID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|