2020-10-16 19:12:53 +02:00
|
|
|
package model
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2021-11-05 11:54:27 +01:00
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2020-11-09 13:19:03 +01:00
|
|
|
)
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
// Block is the basic data unit
|
|
|
|
// swagger:model
|
2020-10-16 19:12:53 +02:00
|
|
|
type Block struct {
|
2021-02-17 20:29:20 +01:00
|
|
|
// The id for this block
|
|
|
|
// required: true
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
// The id for this block's parent block. Empty for root blocks
|
|
|
|
// required: false
|
|
|
|
ParentID string `json:"parentId"`
|
|
|
|
|
|
|
|
// The id for this block's root block
|
|
|
|
// required: true
|
|
|
|
RootID string `json:"rootId"`
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
// The id for user who created this block
|
|
|
|
// required: true
|
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
// The id for user who last modified this block
|
|
|
|
// required: true
|
|
|
|
ModifiedBy string `json:"modifiedBy"`
|
|
|
|
|
|
|
|
// The schema version of this block
|
|
|
|
// required: true
|
|
|
|
Schema int64 `json:"schema"`
|
|
|
|
|
|
|
|
// The block type
|
|
|
|
// required: true
|
2021-10-27 03:11:19 +02:00
|
|
|
Type BlockType `json:"type"`
|
2021-02-17 20:29:20 +01:00
|
|
|
|
|
|
|
// The display title
|
|
|
|
// required: false
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
|
|
// The block fields
|
|
|
|
// required: false
|
|
|
|
Fields map[string]interface{} `json:"fields"`
|
|
|
|
|
|
|
|
// The creation time
|
|
|
|
// required: true
|
|
|
|
CreateAt int64 `json:"createAt"`
|
|
|
|
|
|
|
|
// The last modified time
|
|
|
|
// required: true
|
|
|
|
UpdateAt int64 `json:"updateAt"`
|
|
|
|
|
|
|
|
// The deleted time. Set to indicate this block is deleted
|
|
|
|
// required: false
|
|
|
|
DeleteAt int64 `json:"deleteAt"`
|
2021-09-22 21:57:00 +02:00
|
|
|
|
|
|
|
// The workspace id that the block belongs to
|
|
|
|
// required: true
|
|
|
|
WorkspaceID string `json:"workspaceId"`
|
2020-10-16 19:12:53 +02:00
|
|
|
}
|
2020-11-09 13:19:03 +01:00
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
// BlockPatch is a patch for modify blocks
|
|
|
|
// swagger:model
|
|
|
|
type BlockPatch struct {
|
|
|
|
// The id for this block's parent block. Empty for root blocks
|
|
|
|
// required: false
|
|
|
|
ParentID *string `json:"parentId"`
|
|
|
|
|
|
|
|
// The id for this block's root block
|
|
|
|
// required: false
|
|
|
|
RootID *string `json:"rootId"`
|
|
|
|
|
|
|
|
// The schema version of this block
|
|
|
|
// required: false
|
|
|
|
Schema *int64 `json:"schema"`
|
|
|
|
|
|
|
|
// The block type
|
|
|
|
// required: false
|
2021-10-27 03:11:19 +02:00
|
|
|
Type *BlockType `json:"type"`
|
2021-08-06 14:10:24 +02:00
|
|
|
|
|
|
|
// The display title
|
|
|
|
// required: false
|
|
|
|
Title *string `json:"title"`
|
|
|
|
|
|
|
|
// The block updated fields
|
|
|
|
// required: false
|
|
|
|
UpdatedFields map[string]interface{} `json:"updatedFields"`
|
|
|
|
|
|
|
|
// The block removed fields
|
|
|
|
// required: false
|
|
|
|
DeletedFields []string `json:"deletedFields"`
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Archive is an import / export archive.
|
2020-12-10 21:41:06 +01:00
|
|
|
type Archive struct {
|
|
|
|
Version int64 `json:"version"`
|
|
|
|
Date int64 `json:"date"`
|
|
|
|
Blocks []Block `json:"blocks"`
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
func BlocksFromJSON(data io.Reader) []Block {
|
|
|
|
var blocks []Block
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = json.NewDecoder(data).Decode(&blocks)
|
2020-11-09 13:19:03 +01:00
|
|
|
return blocks
|
|
|
|
}
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
// LogClone implements the `mlog.LogCloner` interface to provide a subset of Block fields for logging.
|
|
|
|
func (b Block) LogClone() interface{} {
|
|
|
|
return struct {
|
|
|
|
ID string
|
|
|
|
ParentID string
|
|
|
|
RootID string
|
2021-10-27 03:11:19 +02:00
|
|
|
Type BlockType
|
2021-06-11 14:24:51 +02:00
|
|
|
}{
|
|
|
|
ID: b.ID,
|
|
|
|
ParentID: b.ParentID,
|
|
|
|
RootID: b.RootID,
|
|
|
|
Type: b.Type,
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 14:10:24 +02:00
|
|
|
|
|
|
|
// Patch returns an update version of the block.
|
|
|
|
func (p *BlockPatch) Patch(block *Block) *Block {
|
|
|
|
if p.ParentID != nil {
|
|
|
|
block.ParentID = *p.ParentID
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.RootID != nil {
|
|
|
|
block.RootID = *p.RootID
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Schema != nil {
|
|
|
|
block.Schema = *p.Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Type != nil {
|
|
|
|
block.Type = *p.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Title != nil {
|
|
|
|
block.Title = *p.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, field := range p.UpdatedFields {
|
|
|
|
block.Fields[key] = field
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range p.DeletedFields {
|
|
|
|
delete(block.Fields, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
2021-11-05 11:54:27 +01:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
func GenerateBlockIDs(blocks []Block) []Block {
|
|
|
|
blockIDs := map[string]BlockType{}
|
|
|
|
referenceIDs := map[string]bool{}
|
|
|
|
for _, block := range blocks {
|
|
|
|
if _, ok := blockIDs[block.ID]; !ok {
|
|
|
|
blockIDs[block.ID] = block.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := referenceIDs[block.RootID]; !ok {
|
|
|
|
referenceIDs[block.RootID] = true
|
|
|
|
}
|
|
|
|
if _, ok := referenceIDs[block.ParentID]; !ok {
|
|
|
|
referenceIDs[block.ParentID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]))
|
|
|
|
}
|
|
|
|
|
|
|
|
newBlocks := make([]Block, len(blocks))
|
|
|
|
for i, block := range blocks {
|
|
|
|
block.ID = getExistingOrNewID(block.ID)
|
|
|
|
block.RootID = getExistingOrOldID(block.RootID)
|
|
|
|
block.ParentID = getExistingOrOldID(block.ParentID)
|
|
|
|
|
|
|
|
newBlocks[i] = block
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBlocks
|
|
|
|
}
|