2021-10-27 03:11:19 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2021-11-03 17:01:39 +01:00
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-10-27 03:11:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockType represents a block type.
|
|
|
|
type BlockType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeUnknown = "unknown"
|
|
|
|
TypeBoard = "board"
|
|
|
|
TypeCard = "card"
|
|
|
|
TypeView = "view"
|
|
|
|
TypeText = "text"
|
|
|
|
TypeComment = "comment"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (bt BlockType) String() string {
|
|
|
|
return string(bt)
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:01:39 +01:00
|
|
|
// BlockTypeFromString returns an appropriate BlockType for the specified string.
|
2021-10-27 03:11:19 +02:00
|
|
|
func BlockTypeFromString(s string) (BlockType, error) {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "board":
|
|
|
|
return TypeBoard, nil
|
|
|
|
case "card":
|
|
|
|
return TypeCard, nil
|
|
|
|
case "view":
|
|
|
|
return TypeView, nil
|
|
|
|
case "text":
|
|
|
|
return TypeText, nil
|
|
|
|
case "comment":
|
|
|
|
return TypeComment, nil
|
|
|
|
}
|
|
|
|
return TypeUnknown, ErrInvalidBlockType{s}
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:01:39 +01:00
|
|
|
// BlockType2IDType returns an appropriate IDType for the specified BlockType.
|
|
|
|
func BlockType2IDType(blockType BlockType) utils.IDType {
|
|
|
|
switch blockType {
|
|
|
|
case TypeBoard:
|
|
|
|
return utils.IDTypeBoard
|
|
|
|
case TypeCard:
|
|
|
|
return utils.IDTypeCard
|
|
|
|
case TypeView:
|
|
|
|
return utils.IDTypeView
|
|
|
|
case TypeText, TypeComment:
|
|
|
|
return utils.IDTypeBlock
|
|
|
|
}
|
|
|
|
return utils.IDTypeNone
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:11:19 +02:00
|
|
|
// ErrInvalidBlockType is returned wherever an invalid block type was provided.
|
|
|
|
type ErrInvalidBlockType struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrInvalidBlockType) Error() string {
|
|
|
|
return e.Type + " is an invalid block type."
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrInvalidBlockType returns true if `err` is a IsErrInvalidBlockType or wraps one.
|
|
|
|
func IsErrInvalidBlockType(err error) bool {
|
|
|
|
var eibt *ErrInvalidBlockType
|
|
|
|
return errors.As(err, &eibt)
|
|
|
|
}
|