main

mattermost/focalboard

Last updated at: 29/12/2023 09:46

category.go

TLDR

This file contains the definition of the Category struct, which represents a board category. It also includes methods to hydrate the struct, validate its properties, and parse JSON data into a Category object.

Methods

Hydrate

This method populates default values for the Category struct properties if they are not already set. It assigns a new ID using the utils.NewID function if the ID is empty. It sets the creation time (CreateAt) and last modified time (UpdateAt) to the current time if they are not already set. It sets the sort order to 0 if it is negative. Finally, if the Type property is empty, it sets it to "custom".

IsValid

This method validates the Category struct properties. It returns an error if any of the following conditions are met:

  • The ID is empty.
  • The Name is empty.
  • The UserID is empty.
  • The TeamID is empty.
  • The Type is not "custom" nor "system".

CategoryFromJSON

This method parses the provided JSON data and returns a Category object. It uses the json.NewDecoder function to decode the JSON from the specified io.Reader.

package model

import (
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"github.com/mattermost/focalboard/server/utils"
)

const (
	CategoryTypeSystem = "system"
	CategoryTypeCustom = "custom"
)

// Category is a board category
// swagger:model
type Category struct {
	// The id for this category
	// required: true
	ID string `json:"id"`

	// The name for this category
	// required: true
	Name string `json:"name"`

	// The user's id for this category
	// required: true
	UserID string `json:"userID"`

	// The team id for this category
	// required: true
	TeamID string `json:"teamID"`

	// The creation time in miliseconds since the current epoch
	// required: true
	CreateAt int64 `json:"createAt"`

	// The last modified time in miliseconds since the current epoch
	// required: true
	UpdateAt int64 `json:"updateAt"`

	// The deleted time in miliseconds since the current epoch. Set to indicate this category is deleted
	// required: false
	DeleteAt int64 `json:"deleteAt"`

	// Category's state in client side
	// required: true
	Collapsed bool `json:"collapsed"`

	// Inter-category sort order per user
	// required: true
	SortOrder int `json:"sortOrder"`

	// The sorting method applied on this category
	// required: true
	Sorting string `json:"sorting"`

	// Category's type
	// required: true
	Type string `json:"type"`
}

func (c *Category) Hydrate() {
	if c.ID == "" {
		c.ID = utils.NewID(utils.IDTypeNone)
	}

	if c.CreateAt == 0 {
		c.CreateAt = utils.GetMillis()
	}

	if c.UpdateAt == 0 {
		c.UpdateAt = c.CreateAt
	}

	if c.SortOrder < 0 {
		c.SortOrder = 0
	}

	if strings.TrimSpace(c.Type) == "" {
		c.Type = CategoryTypeCustom
	}
}

func (c *Category) IsValid() error {
	if strings.TrimSpace(c.ID) == "" {
		return NewErrInvalidCategory("category ID cannot be empty")
	}

	if strings.TrimSpace(c.Name) == "" {
		return NewErrInvalidCategory("category name cannot be empty")
	}

	if strings.TrimSpace(c.UserID) == "" {
		return NewErrInvalidCategory("category user ID cannot be empty")
	}

	if strings.TrimSpace(c.TeamID) == "" {
		return NewErrInvalidCategory("category team id ID cannot be empty")
	}

	if c.Type != CategoryTypeCustom && c.Type != CategoryTypeSystem {
		return NewErrInvalidCategory(fmt.Sprintf("category type is invalid. Allowed types: %s and %s", CategoryTypeSystem, CategoryTypeCustom))
	}

	return nil
}

func CategoryFromJSON(data io.Reader) *Category {
	var category *Category
	_ = json.NewDecoder(data).Decode(&category)
	return category
}