focalboard/server/app/app.go

138 lines
3.3 KiB
Go
Raw Normal View History

package app
2020-10-16 16:21:42 +02:00
2020-10-16 16:36:08 +02:00
import (
"crypto/rand"
2020-10-16 16:36:08 +02:00
"fmt"
"io"
"io/ioutil"
"log"
2020-10-16 16:36:08 +02:00
"os"
"path/filepath"
"strings"
"github.com/mattermost/mattermost-octo-tasks/server/model"
"github.com/mattermost/mattermost-octo-tasks/server/services/config"
"github.com/mattermost/mattermost-octo-tasks/server/services/store"
"github.com/mattermost/mattermost-octo-tasks/server/ws"
2020-10-16 16:36:08 +02:00
)
2020-10-16 16:21:42 +02:00
type App struct {
config *config.Configuration
2020-10-16 19:20:43 +02:00
store store.Store
wsServer *ws.WSServer
}
2020-10-16 19:20:43 +02:00
func New(config *config.Configuration, store store.Store, wsServer *ws.WSServer) *App {
return &App{config: config, store: store, wsServer: wsServer}
2020-10-16 16:21:42 +02:00
}
func (a *App) GetBlocks(parentID string, blockType string) ([]model.Block, error) {
2020-10-16 16:21:42 +02:00
if len(blockType) > 0 && len(parentID) > 0 {
return a.store.GetBlocksWithParentAndType(parentID, blockType)
2020-10-16 16:21:42 +02:00
}
if len(blockType) > 0 {
return a.store.GetBlocksWithType(blockType)
2020-10-16 16:21:42 +02:00
}
return a.store.GetBlocksWithParent(parentID)
2020-10-16 16:21:42 +02:00
}
func (a *App) GetParentID(blockID string) (string, error) {
return a.store.GetParentID(blockID)
2020-10-16 16:21:42 +02:00
}
func (a *App) InsertBlock(block model.Block) error {
return a.store.InsertBlock(block)
2020-10-16 16:21:42 +02:00
}
func (a *App) InsertBlocks(blocks []model.Block) error {
2020-10-16 16:21:42 +02:00
var blockIDsToNotify = []string{}
uniqueBlockIDs := make(map[string]bool)
for _, block := range blocks {
if !uniqueBlockIDs[block.ID] {
blockIDsToNotify = append(blockIDsToNotify, block.ID)
}
if len(block.ParentID) > 0 && !uniqueBlockIDs[block.ParentID] {
blockIDsToNotify = append(blockIDsToNotify, block.ParentID)
}
err := a.store.InsertBlock(block)
2020-10-16 16:21:42 +02:00
if err != nil {
return err
}
}
a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify)
2020-10-16 16:21:42 +02:00
return nil
}
func (a *App) GetSubTree(blockID string) ([]model.Block, error) {
return a.store.GetSubTree(blockID)
2020-10-16 16:21:42 +02:00
}
func (a *App) GetAllBlocks() ([]model.Block, error) {
return a.store.GetAllBlocks()
2020-10-16 16:21:42 +02:00
}
func (a *App) DeleteBlock(blockID string) error {
var blockIDsToNotify = []string{blockID}
parentID, err := a.GetParentID(blockID)
if err != nil {
return err
}
if len(parentID) > 0 {
blockIDsToNotify = append(blockIDsToNotify, parentID)
}
err = a.store.DeleteBlock(blockID)
2020-10-16 16:21:42 +02:00
if err != nil {
return err
}
a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify)
2020-10-16 16:21:42 +02:00
return nil
}
2020-10-16 16:36:08 +02:00
func (a *App) SaveFile(reader io.Reader, filename string) (string, error) {
data, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
// NOTE: File extension includes the dot
fileExtension := strings.ToLower(filepath.Ext(filename))
if fileExtension == ".jpeg" {
fileExtension = ".jpg"
}
createdFilename := fmt.Sprintf(`%s%s`, createGUID(), fileExtension)
folderPath := a.config.FilesPath
filePath := filepath.Join(folderPath, createdFilename)
os.MkdirAll(folderPath, os.ModePerm)
err = ioutil.WriteFile(filePath, data, 0666)
if err != nil {
return "", err
}
return fmt.Sprintf(`%s/files/%s`, a.config.ServerRoot, createdFilename), nil
}
func (a *App) GetFilePath(filename string) string {
folderPath := a.config.FilesPath
return filepath.Join(folderPath, filename)
}
// CreateGUID returns a random GUID
func createGUID() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid
}