Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
109 lines
4.2 KiB
Go
109 lines
4.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"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) {
|
|
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,json_extract(data,'$.page_uuid') FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND uuid=? 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=? AND path=?`, 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=? AND path=?)`, domain, target).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return ErrTaskNoteExists
|
|
}
|
|
current.PageUUID = uuid.NewString()
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',strftime('%s','now'))`, current.PageUUID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO paths(domain_uuid,path,object_uuid,created_at) VALUES(?,?,?,strftime('%s','now'))`, domain, target, current.PageUUID); err != nil {
|
|
return err
|
|
}
|
|
body := "<h1>" + html.EscapeString(current.Title) + "</h1>\n"
|
|
now := time.Now()
|
|
rev := &Revision{UUID: uuid.NewString(), ObjectUUID: current.PageUUID, DocumentHash: revisionHash(body), Language: "und", ContentType: "text/html; charset=utf-8", Body: body, Message: "Create task note", CreatedAt: now.Unix(), CreatedAtMS: now.UnixMilli()}
|
|
if err := s.persistDocument(rev.DocumentHash, rev.Body); err != nil {
|
|
return err
|
|
}
|
|
if err := insertRevision(tx, rev, nil); err != nil {
|
|
return err
|
|
}
|
|
if err := s.appendRevisionActivityOutboxIfAvailable(tx, rev); err != nil {
|
|
return err
|
|
}
|
|
payload, _ := json.Marshal(map[string]string{"path": target, "object_uuid": current.PageUUID})
|
|
return s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{DomainUUID: domain, ActorUUID: actor, EventType: "page.created", TargetKind: "page", TargetUUID: current.PageUUID, Payload: string(payload), IdempotencyKey: "page.created:" + current.PageUUID})
|
|
})
|
|
if err == nil {
|
|
s.InvalidatePathCaches()
|
|
}
|
|
return saved, err
|
|
}
|