92 lines
3.2 KiB
Go
92 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"html"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var ErrTaskNoteExists = errors.New("task note path already exists")
|
|
|
|
func validTaskNotesSubpath(p string) bool {
|
|
if p == "" {
|
|
return true
|
|
}
|
|
for _, part := range strings.Split(p, "/") {
|
|
if part == ".." || part == "." || part == "" || part == "_" {
|
|
return false
|
|
}
|
|
}
|
|
return len(p) <= 500 && path.Clean(p) == p && !strings.HasPrefix(p, "/") &&
|
|
!strings.ContainsAny(p, "\\?#%") && strings.IndexFunc(p, unicode.IsControl) < 0
|
|
}
|
|
|
|
func TaskNotePath(base string, project, item TaskRecord) string {
|
|
sub := project.NotesSubpath
|
|
if sub == "" {
|
|
sub = "tasks"
|
|
}
|
|
name := strings.TrimSpace(strings.Map(func(r rune) rune {
|
|
if unicode.IsControl(r) || strings.ContainsRune("/\\?#%", r) {
|
|
return '-'
|
|
}
|
|
return r
|
|
}, item.Title))
|
|
if name == "" || name == "." || name == ".." || name == "_" {
|
|
name = "task-" + item.UUID
|
|
}
|
|
return strings.TrimRight(base, "/") + "/" + sub + "/" + name
|
|
}
|
|
|
|
// CreateTaskNote binds a title-initialized Wiki page and its task in one transaction.
|
|
// The caller must authorize read/create/write at target and verify the project
|
|
// alias. Recheck the alias and project version under the writer lock.
|
|
func (s *Store) CreateTaskNote(ctx context.Context, domain, actor string, item TaskRecord, expected int64, project TaskRecord, base, target string) (TaskRecord, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if item.Kind != "task" || item.PageUUID != "" || project.Kind != "project" ||
|
|
item.ProjectUUID != project.UUID || project.PageUUID == "" ||
|
|
path.Clean(target) != target || target != TaskNotePath(base, project, item) ||
|
|
!strings.HasPrefix(target, strings.TrimRight(base, "/")+"/") {
|
|
return TaskRecord{}, ErrTaskInvalid
|
|
}
|
|
saved, err := s.saveTask(ctx, domain, actor, item, expected, func(tx *sql.Tx, current *TaskRecord, old TaskRecord) error {
|
|
var version int64
|
|
var projectPage string
|
|
if err := tx.QueryRowContext(ctx, `SELECT version,`+s.taskJSONText("page_uuid")+` FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND uuid=`+s.sqlParam(3)+` AND kind='project' AND archived=0`, domain, actor, project.UUID).Scan(&version, &projectPage); err != nil {
|
|
return err
|
|
}
|
|
if version != project.Version || projectPage != project.PageUUID || old.PageUUID != "" {
|
|
return ErrTaskConflict
|
|
}
|
|
var bound string
|
|
if err := tx.QueryRowContext(ctx, `SELECT object_uuid FROM paths WHERE domain_uuid=`+s.sqlParam(1)+` AND path=`+s.sqlParam(2), domain, base).Scan(&bound); err != nil {
|
|
return err
|
|
}
|
|
if bound != project.PageUUID {
|
|
return ErrTaskConflict
|
|
}
|
|
var exists bool
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM paths WHERE domain_uuid=`+s.sqlParam(1)+` AND path=`+s.sqlParam(2)+`)`, domain, target).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return ErrTaskNoteExists
|
|
}
|
|
current.PageUUID = uuid.NewString()
|
|
body := "<h1>" + html.EscapeString(current.Title) + "</h1>\n"
|
|
return s.createTaskWikiPageTx(ctx, tx, domain, actor, current.PageUUID, target, body, "Create task note")
|
|
})
|
|
if err == nil {
|
|
s.InvalidatePathCaches()
|
|
}
|
|
return saved, err
|
|
}
|