2022-02-16 18:08:49 +01:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateBlockIDs generates new IDs for all the blocks of the list,
|
|
|
|
// keeping consistent any references that other blocks would made to
|
|
|
|
// the original IDs, so a tree of blocks can get new IDs and maintain
|
|
|
|
// its shape.
|
2022-10-25 22:46:43 +02:00
|
|
|
func GenerateBlockIDs(blocks []*Block, logger mlog.LoggerIFace) []*Block {
|
2022-02-16 18:08:49 +01:00
|
|
|
blockIDs := map[string]BlockType{}
|
|
|
|
referenceIDs := map[string]bool{}
|
|
|
|
for _, block := range blocks {
|
|
|
|
if _, ok := blockIDs[block.ID]; !ok {
|
|
|
|
blockIDs[block.ID] = block.Type
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if _, ok := referenceIDs[block.BoardID]; !ok {
|
|
|
|
referenceIDs[block.BoardID] = true
|
2022-02-16 18:08:49 +01:00
|
|
|
}
|
|
|
|
if _, ok := referenceIDs[block.ParentID]; !ok {
|
|
|
|
referenceIDs[block.ParentID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := block.Fields["contentOrder"]; ok {
|
|
|
|
contentOrder, typeOk := block.Fields["contentOrder"].([]interface{})
|
|
|
|
if !typeOk {
|
|
|
|
logger.Warn(
|
|
|
|
"type assertion failed for content order when saving reference block IDs",
|
|
|
|
mlog.String("blockID", block.ID),
|
|
|
|
mlog.String("actionType", fmt.Sprintf("%T", block.Fields["contentOrder"])),
|
|
|
|
mlog.String("expectedType", "[]interface{}"),
|
|
|
|
mlog.String("contentOrder", fmt.Sprintf("%v", block.Fields["contentOrder"])),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blockID := range contentOrder {
|
|
|
|
switch v := blockID.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, columnBlockID := range v {
|
|
|
|
referenceIDs[columnBlockID.(string)] = true
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
referenceIDs[v] = true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 19:27:18 +02:00
|
|
|
|
|
|
|
if _, ok := block.Fields["defaultTemplateId"]; ok {
|
|
|
|
defaultTemplateID, typeOk := block.Fields["defaultTemplateId"].(string)
|
|
|
|
if !typeOk {
|
|
|
|
logger.Warn(
|
|
|
|
"type assertion failed for default template ID when saving reference block IDs",
|
|
|
|
mlog.String("blockID", block.ID),
|
|
|
|
mlog.String("actionType", fmt.Sprintf("%T", block.Fields["defaultTemplateId"])),
|
|
|
|
mlog.String("expectedType", "string"),
|
|
|
|
mlog.String("defaultTemplateId", fmt.Sprintf("%v", block.Fields["defaultTemplateId"])),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
referenceIDs[defaultTemplateID] = true
|
|
|
|
}
|
2022-02-16 18:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newIDs := map[string]string{}
|
|
|
|
for id, blockType := range blockIDs {
|
|
|
|
for referenceID := range referenceIDs {
|
|
|
|
if id == referenceID {
|
|
|
|
newIDs[id] = utils.NewID(BlockType2IDType(blockType))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getExistingOrOldID := func(id string) string {
|
|
|
|
if existingID, ok := newIDs[id]; ok {
|
|
|
|
return existingID
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
getExistingOrNewID := func(id string) string {
|
|
|
|
if existingID, ok := newIDs[id]; ok {
|
|
|
|
return existingID
|
|
|
|
}
|
|
|
|
return utils.NewID(BlockType2IDType(blockIDs[id]))
|
|
|
|
}
|
|
|
|
|
2022-10-25 22:46:43 +02:00
|
|
|
newBlocks := make([]*Block, len(blocks))
|
2022-02-16 18:08:49 +01:00
|
|
|
for i, block := range blocks {
|
|
|
|
block.ID = getExistingOrNewID(block.ID)
|
2022-03-22 15:24:34 +01:00
|
|
|
block.BoardID = getExistingOrOldID(block.BoardID)
|
2022-02-16 18:08:49 +01:00
|
|
|
block.ParentID = getExistingOrOldID(block.ParentID)
|
|
|
|
|
|
|
|
blockMod := block
|
|
|
|
if _, ok := blockMod.Fields["contentOrder"]; ok {
|
2022-10-25 22:46:43 +02:00
|
|
|
fixFieldIDs(blockMod, "contentOrder", getExistingOrOldID, logger)
|
2022-02-16 18:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := blockMod.Fields["cardOrder"]; ok {
|
2022-10-25 22:46:43 +02:00
|
|
|
fixFieldIDs(blockMod, "cardOrder", getExistingOrOldID, logger)
|
2022-02-16 18:08:49 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 19:27:18 +02:00
|
|
|
if _, ok := blockMod.Fields["defaultTemplateId"]; ok {
|
|
|
|
defaultTemplateID, typeOk := blockMod.Fields["defaultTemplateId"].(string)
|
|
|
|
if !typeOk {
|
|
|
|
logger.Warn(
|
|
|
|
"type assertion failed for default template ID when saving reference block IDs",
|
|
|
|
mlog.String("blockID", blockMod.ID),
|
|
|
|
mlog.String("actionType", fmt.Sprintf("%T", blockMod.Fields["defaultTemplateId"])),
|
|
|
|
mlog.String("expectedType", "string"),
|
|
|
|
mlog.String("defaultTemplateId", fmt.Sprintf("%v", blockMod.Fields["defaultTemplateId"])),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
blockMod.Fields["defaultTemplateId"] = getExistingOrOldID(defaultTemplateID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:08:49 +01:00
|
|
|
newBlocks[i] = blockMod
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBlocks
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:21:57 +02:00
|
|
|
func fixFieldIDs(block *Block, fieldName string, getExistingOrOldID func(string) string, logger mlog.LoggerIFace) {
|
2022-02-16 18:08:49 +01:00
|
|
|
field, typeOk := block.Fields[fieldName].([]interface{})
|
|
|
|
if !typeOk {
|
|
|
|
logger.Warn(
|
|
|
|
"type assertion failed for JSON field when setting new block IDs",
|
|
|
|
mlog.String("blockID", block.ID),
|
|
|
|
mlog.String("fieldName", fieldName),
|
|
|
|
mlog.String("actionType", fmt.Sprintf("%T", block.Fields[fieldName])),
|
|
|
|
mlog.String("expectedType", "[]interface{}"),
|
|
|
|
mlog.String("value", fmt.Sprintf("%v", block.Fields[fieldName])),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
for j := range field {
|
|
|
|
switch v := field[j].(type) {
|
|
|
|
case string:
|
|
|
|
field[j] = getExistingOrOldID(v)
|
|
|
|
case []interface{}:
|
|
|
|
subOrder := field[j].([]interface{})
|
|
|
|
for k := range v {
|
|
|
|
subOrder[k] = getExistingOrOldID(v[k].(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|