Select Git revision
CONTRIBUTORS
-
Richard Hartmann authoredRichard Hartmann authored
folders.go 3.08 KiB
// Package folders are all the routines and sql for managing folders.
package folders
import (
"errors"
"strings"
"git.lyda.ie/kevin/bulletin/storage"
"git.lyda.ie/kevin/bulletin/this"
)
// ValidFolder validates the folder name for this user.
func ValidFolder(folder string) (storage.Folder, error) {
if strings.Contains(folder, "%") {
return storage.Folder{}, errors.New("Folder name cannot contain a %")
}
correct := FindFolder(folder)
if correct.Name == "" {
return storage.Folder{}, errors.New("Unable to select the folder")
}
if !IsFolderAccess(correct.Name, this.User.Login) {
// TODO: Should be:
// WRITE(6,'('' You are not allowed to access folder.'')')
// WRITE(6,'('' See '',A,'' if you wish to access folder.'')')
return storage.Folder{}, errors.New("Unable to select the folder")
}
return correct, nil
}
// Values for FolderVisibility.
const (
FolderPublic int64 = 0
FolderSemiPrivate = 1
FolderPrivate = 2
)
// CreateFolder creates a new folder.
func CreateFolder(owner string, options storage.CreateFolderParams) error {
if !IsAlphaNum(options.Name) {
return errors.New("Folder can only have letters and numbers")
}
options.Name = strings.ToUpper(options.Name)
ctx := storage.Context()
tx, err := this.Store.Begin()
if err != nil {
return err
}
defer tx.Rollback()
qtx := this.Q.WithTx(tx)
err = qtx.CreateFolder(ctx, options)
if err != nil {
return err
}
err = qtx.AddFolderOwner(ctx, storage.AddFolderOwnerParams{
Folder: options.Name,
Login: owner,
})
if err != nil {
return err
}
// TODO: process this error a bit more to give a better error message.
return tx.Commit()
}
// ListFolder provides a list of folders that this.User has access to.
func ListFolder() ([]storage.ListFolderRow, error) {
// TODO: need to check access.
ctx := storage.Context()