2020-10-16 19:12:53 +02:00
|
|
|
package model
|
|
|
|
|
2020-11-09 14:19:03 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-07-28 12:54:38 +02:00
|
|
|
"errors"
|
2020-11-09 14:19:03 +02:00
|
|
|
"io"
|
2022-02-28 13:28:16 +02:00
|
|
|
"strconv"
|
2023-07-28 12:54:38 +02:00
|
|
|
"unicode/utf8"
|
2022-02-28 13:28:16 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/services/audit"
|
2020-11-09 14:19:03 +02:00
|
|
|
)
|
|
|
|
|
2023-07-28 12:54:38 +02:00
|
|
|
const (
|
|
|
|
BlockTitleMaxBytes = 65535 // Maximum size of a TEXT column in MySQL
|
|
|
|
BlockTitleMaxRunes = BlockTitleMaxBytes / 4 // Assume a worst-case representation
|
|
|
|
BlockFieldsMaxRunes = 800000
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrBlockEmptyBoardID = errors.New("boardID is empty")
|
|
|
|
ErrBlockTitleSizeLimitExceeded = errors.New("block title size limit exceeded")
|
|
|
|
ErrBlockFieldsSizeLimitExceeded = errors.New("block fields size limit exceeded")
|
|
|
|
)
|
|
|
|
|
2021-02-17 21:29:20 +02:00
|
|
|
// Block is the basic data unit
|
|
|
|
// swagger:model
|
2020-10-16 19:12:53 +02:00
|
|
|
type Block struct {
|
2021-02-17 21:29:20 +02: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"`
|
|
|
|
|
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 21:29:20 +02: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 21:29:20 +02:00
|
|
|
|
|
|
|
// The display title
|
|
|
|
// required: false
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
|
|
// The block fields
|
|
|
|
// required: false
|
|
|
|
Fields map[string]interface{} `json:"fields"`
|
|
|
|
|
2022-04-15 19:21:10 +02:00
|
|
|
// The creation time in miliseconds since the current epoch
|
2021-02-17 21:29:20 +02:00
|
|
|
// required: true
|
|
|
|
CreateAt int64 `json:"createAt"`
|
|
|
|
|
2022-04-15 19:21:10 +02:00
|
|
|
// The last modified time in miliseconds since the current epoch
|
2021-02-17 21:29:20 +02:00
|
|
|
// required: true
|
|
|
|
UpdateAt int64 `json:"updateAt"`
|
|
|
|
|
2022-04-15 19:21:10 +02:00
|
|
|
// The deleted time in miliseconds since the current epoch. Set to indicate this block is deleted
|
2021-02-17 21:29:20 +02:00
|
|
|
// required: false
|
|
|
|
DeleteAt int64 `json:"deleteAt"`
|
2021-09-22 21:57:00 +02:00
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
// Deprecated. The workspace id that the block belongs to
|
|
|
|
// required: false
|
|
|
|
WorkspaceID string `json:"-"`
|
|
|
|
|
|
|
|
// The board id that the block belongs to
|
2021-09-22 21:57:00 +02:00
|
|
|
// required: true
|
2022-03-22 16:24:34 +02:00
|
|
|
BoardID string `json:"boardId"`
|
2022-06-15 12:17:44 +02:00
|
|
|
|
|
|
|
// Indicates if the card is limited
|
|
|
|
// required: false
|
|
|
|
Limited bool `json:"limited,omitempty"`
|
2020-10-16 19:12:53 +02:00
|
|
|
}
|
2020-11-09 14:19:03 +02: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 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-12-10 16:17:00 +02:00
|
|
|
// BlockPatchBatch is a batch of IDs and patches for modify blocks
|
|
|
|
// swagger:model
|
|
|
|
type BlockPatchBatch struct {
|
|
|
|
// The id's for of the blocks to patch
|
|
|
|
BlockIDs []string `json:"block_ids"`
|
|
|
|
|
|
|
|
// The BlockPatches to be applied
|
|
|
|
BlockPatches []BlockPatch `json:"block_patches"`
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
// BoardModifier is a callback that can modify each board during an import.
|
2022-02-01 20:36:12 +02:00
|
|
|
// A cache of arbitrary data will be passed for each call and any changes
|
|
|
|
// to the cache will be preserved for the next call.
|
|
|
|
// Return true to import the block or false to skip import.
|
2022-03-22 16:24:34 +02:00
|
|
|
type BoardModifier func(board *Board, cache map[string]interface{}) bool
|
2022-02-01 20:36:12 +02:00
|
|
|
|
2022-03-31 00:10:11 +02:00
|
|
|
// BlockModifier is a callback that can modify each block during an import.
|
|
|
|
// A cache of arbitrary data will be passed for each call and any changes
|
|
|
|
// to the cache will be preserved for the next call.
|
|
|
|
// Return true to import the block or false to skip import.
|
|
|
|
type BlockModifier func(block *Block, cache map[string]interface{}) bool
|
|
|
|
|
2022-10-25 22:46:43 +02: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 14:19:03 +02:00
|
|
|
return blocks
|
|
|
|
}
|
2021-06-11 14:24:51 +02:00
|
|
|
|
2023-07-28 12:54:38 +02:00
|
|
|
// IsValid checks the block for errors before inserting, and makes
|
|
|
|
// sure it complies with the requirements of a valid block.
|
|
|
|
func (b *Block) IsValid() error {
|
|
|
|
if b.BoardID == "" {
|
|
|
|
return ErrBlockEmptyBoardID
|
|
|
|
}
|
|
|
|
|
|
|
|
if utf8.RuneCountInString(b.Title) > BlockTitleMaxRunes {
|
|
|
|
return ErrBlockTitleSizeLimitExceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldsJSON, err := json.Marshal(b.Fields)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if utf8.RuneCountInString(string(fieldsJSON)) > BlockFieldsMaxRunes {
|
|
|
|
return ErrBlockFieldsSizeLimitExceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
// LogClone implements the `mlog.LogCloner` interface to provide a subset of Block fields for logging.
|
2022-10-25 22:46:43 +02:00
|
|
|
func (b *Block) LogClone() interface{} {
|
2021-06-11 14:24:51 +02:00
|
|
|
return struct {
|
|
|
|
ID string
|
|
|
|
ParentID string
|
2022-03-22 16:24:34 +02:00
|
|
|
BoardID 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,
|
2022-03-22 16:24:34 +02:00
|
|
|
BoardID: b.BoardID,
|
2021-06-11 14:24:51 +02:00
|
|
|
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.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 12:54:27 +02:00
|
|
|
|
2022-08-30 15:56:41 +02:00
|
|
|
type QueryBlocksOptions struct {
|
|
|
|
BoardID string // if not empty then filter for blocks belonging to specified board
|
|
|
|
ParentID string // if not empty then filter for blocks belonging to specified parent
|
|
|
|
BlockType BlockType // if not empty and not `TypeUnknown` then filter for records of specified block type
|
|
|
|
Page int // page number to select when paginating
|
|
|
|
PerPage int // number of blocks per page (default=-1, meaning unlimited)
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:46:37 +02:00
|
|
|
// QuerySubtreeOptions are query options that can be passed to GetSubTree methods.
|
|
|
|
type QuerySubtreeOptions struct {
|
|
|
|
BeforeUpdateAt int64 // if non-zero then filter for records with update_at less than BeforeUpdateAt
|
|
|
|
AfterUpdateAt int64 // if non-zero then filter for records with update_at greater than AfterUpdateAt
|
|
|
|
Limit uint64 // if non-zero then limit the number of returned records
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryBlockHistoryOptions are query options that can be passed to GetBlockHistory.
|
|
|
|
type QueryBlockHistoryOptions struct {
|
|
|
|
BeforeUpdateAt int64 // if non-zero then filter for records with update_at less than BeforeUpdateAt
|
|
|
|
AfterUpdateAt int64 // if non-zero then filter for records with update_at greater than AfterUpdateAt
|
|
|
|
Limit uint64 // if non-zero then limit the number of returned records
|
|
|
|
Descending bool // if true then the records are sorted by insert_at in descending order
|
|
|
|
}
|
2022-02-28 13:28:16 +02:00
|
|
|
|
2022-04-08 19:31:28 +02:00
|
|
|
// QueryBoardHistoryOptions are query options that can be passed to GetBoardHistory.
|
|
|
|
type QueryBoardHistoryOptions struct {
|
|
|
|
BeforeUpdateAt int64 // if non-zero then filter for records with update_at less than BeforeUpdateAt
|
|
|
|
AfterUpdateAt int64 // if non-zero then filter for records with update_at greater than AfterUpdateAt
|
|
|
|
Limit uint64 // if non-zero then limit the number of returned records
|
|
|
|
Descending bool // if true then the records are sorted by insert_at in descending order
|
|
|
|
}
|
|
|
|
|
2023-02-08 23:37:22 +02:00
|
|
|
// QueryBlockHistoryOptions are query options that can be passed to GetBlockHistory.
|
|
|
|
type QueryBlockHistoryChildOptions struct {
|
|
|
|
BeforeUpdateAt int64 // if non-zero then filter for records with update_at less than BeforeUpdateAt
|
|
|
|
AfterUpdateAt int64 // if non-zero then filter for records with update_at greater than AfterUpdateAt
|
|
|
|
Page int // page number to select when paginating
|
|
|
|
PerPage int // number of blocks per page (default=-1, meaning unlimited)
|
|
|
|
}
|
|
|
|
|
2022-10-25 22:46:43 +02:00
|
|
|
func StampModificationMetadata(userID string, blocks []*Block, auditRec *audit.Record) {
|
2022-02-28 13:28:16 +02:00
|
|
|
if userID == SingleUser {
|
|
|
|
userID = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
now := GetMillis()
|
|
|
|
for i := range blocks {
|
|
|
|
blocks[i].ModifiedBy = userID
|
|
|
|
blocks[i].UpdateAt = now
|
|
|
|
|
|
|
|
if auditRec != nil {
|
|
|
|
auditRec.AddMeta("block_"+strconv.FormatInt(int64(i), 10), blocks[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 12:17:44 +02:00
|
|
|
|
2022-10-25 22:46:43 +02:00
|
|
|
func (b *Block) ShouldBeLimited(cardLimitTimestamp int64) bool {
|
2022-06-15 12:17:44 +02:00
|
|
|
return b.Type == TypeCard &&
|
|
|
|
b.UpdateAt < cardLimitTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a limited version of the block that doesn't contain the
|
|
|
|
// contents of the block, only its IDs and type.
|
2022-10-25 22:46:43 +02:00
|
|
|
func (b *Block) GetLimited() *Block {
|
|
|
|
newBlock := &Block{
|
2022-06-15 12:17:44 +02:00
|
|
|
Title: b.Title,
|
|
|
|
ID: b.ID,
|
|
|
|
ParentID: b.ParentID,
|
2022-07-11 14:01:46 +02:00
|
|
|
BoardID: b.BoardID,
|
2022-06-15 12:17:44 +02:00
|
|
|
Schema: b.Schema,
|
|
|
|
Type: b.Type,
|
|
|
|
CreateAt: b.CreateAt,
|
|
|
|
UpdateAt: b.UpdateAt,
|
|
|
|
DeleteAt: b.DeleteAt,
|
|
|
|
WorkspaceID: b.WorkspaceID,
|
|
|
|
Limited: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if iconField, ok := b.Fields["icon"]; ok {
|
|
|
|
newBlock.Fields = map[string]interface{}{
|
|
|
|
"icon": iconField,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBlock
|
|
|
|
}
|