Implement Knowledge City platform, organizer and DAV integration
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.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,271 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
ical "github.com/emersion/go-ical"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// Deliberately bounded, non-recurring VTODO profile. Unsupported scheduling
|
||||
// semantics fail explicitly; accepted ancillary properties survive UI edits.
|
||||
func parseTaskICAL(data string) (*ical.Calendar, error) {
|
||||
bad := func(reason string) (*ical.Calendar, error) { return nil, fmt.Errorf("invalid VTODO: %s", reason) }
|
||||
if len(data) > 32768 || !utf8.ValidString(data) {
|
||||
return bad("size or UTF-8")
|
||||
}
|
||||
for _, c := range data {
|
||||
if c < 32 && c != '\r' && c != '\n' && c != '\t' || c == 127 {
|
||||
return bad("control character")
|
||||
}
|
||||
}
|
||||
// Bound nesting before invoking the recursive decoder.
|
||||
depth := 0
|
||||
unfolded := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(data, "\r\n", "\n"), "\n ", ""), "\n\t", "")
|
||||
for _, line := range strings.Split(unfolded, "\n") {
|
||||
upper := strings.ToUpper(line)
|
||||
if strings.HasPrefix(upper, "BEGIN:") {
|
||||
depth++
|
||||
if depth > 2 {
|
||||
return bad("nested components (including VALARM/VTIMEZONE) unsupported")
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(upper, "END:") {
|
||||
depth--
|
||||
}
|
||||
}
|
||||
dec := ical.NewDecoder(strings.NewReader(data))
|
||||
c, err := dec.Decode()
|
||||
if err != nil {
|
||||
return bad("syntax")
|
||||
}
|
||||
if _, err = dec.Decode(); err != io.EOF {
|
||||
return bad("exactly one VCALENDAR required")
|
||||
}
|
||||
if len(c.Children) != 1 || c.Children[0].Name != "VTODO" || len(c.Children[0].Children) != 0 {
|
||||
return bad("exactly one VTODO required")
|
||||
}
|
||||
for key, props := range c.Props {
|
||||
if len(props) != 1 || (key != "VERSION" && key != "PRODID" && key != "CALSCALE") {
|
||||
return bad("unsupported calendar property " + key)
|
||||
}
|
||||
}
|
||||
if v, _ := c.Props.Text("VERSION"); v != "2.0" {
|
||||
return bad("VERSION")
|
||||
}
|
||||
if v, _ := c.Props.Text("PRODID"); v == "" {
|
||||
return bad("PRODID")
|
||||
}
|
||||
if v, _ := c.Props.Text("CALSCALE"); v != "" && v != "GREGORIAN" {
|
||||
return bad("CALSCALE")
|
||||
}
|
||||
v := c.Children[0]
|
||||
allowed := map[string]bool{"UID": true, "DTSTAMP": true, "SUMMARY": true, "DESCRIPTION": true, "STATUS": true, "PRIORITY": true, "PERCENT-COMPLETE": true, "DTSTART": true, "DUE": true, "CREATED": true, "LAST-MODIFIED": true, "COMPLETED": true, "SEQUENCE": true, "CATEGORIES": true, "URL": true, "LOCATION": true, "CLASS": true, "RELATED-TO": true}
|
||||
for key, props := range v.Props {
|
||||
if !allowed[key] && !strings.HasPrefix(key, "X-") {
|
||||
return bad("unsupported property " + key)
|
||||
}
|
||||
if len(props) != 1 && key != "CATEGORIES" && key != "RELATED-TO" && !strings.HasPrefix(key, "X-") {
|
||||
return bad("duplicate " + key)
|
||||
}
|
||||
for _, p := range props {
|
||||
for param, values := range p.Params {
|
||||
if len(values) != 1 {
|
||||
return bad("multiple parameter values")
|
||||
}
|
||||
if param != "VALUE" && param != "LANGUAGE" && param != "RELTYPE" {
|
||||
return bad("unsupported parameter " + param)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"UID", "DTSTAMP", "SUMMARY"} {
|
||||
if len(v.Props[key]) != 1 {
|
||||
return bad(key + " required")
|
||||
}
|
||||
}
|
||||
uid, e := v.Props.Text("UID")
|
||||
if e != nil || uid == "" || len(uid) > 512 || strings.ContainsAny(uid, "\r\n\x00") {
|
||||
return bad("UID")
|
||||
}
|
||||
for _, key := range []string{"SUMMARY", "DESCRIPTION", "LOCATION", "CLASS"} {
|
||||
if _, err = v.Props.Text(key); err != nil {
|
||||
return bad(key)
|
||||
}
|
||||
}
|
||||
title, _ := v.Props.Text("SUMMARY")
|
||||
notes, _ := v.Props.Text("DESCRIPTION")
|
||||
if strings.TrimSpace(title) == "" || len(title) > 300 || strings.ContainsAny(title, "\r\n") || len(notes) > 16384 {
|
||||
return bad("title or description size")
|
||||
}
|
||||
status, _ := v.Props.Text("STATUS")
|
||||
if status != "" && taskStatusFromICAL(status) == "" {
|
||||
return bad("STATUS")
|
||||
}
|
||||
for key, max := range map[string]int{"PRIORITY": 9, "PERCENT-COMPLETE": 100, "SEQUENCE": 2147483647} {
|
||||
if p := v.Props.Get(key); p != nil {
|
||||
n, e := strconv.Atoi(p.Value)
|
||||
if e != nil || n < 0 || n > max {
|
||||
return bad(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"DTSTART", "DUE", "DTSTAMP", "CREATED", "LAST-MODIFIED", "COMPLETED"} {
|
||||
p := v.Props.Get(key)
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
date := p.ValueType() == ical.ValueDate
|
||||
if (key == "DTSTART" || key == "DUE") && !date {
|
||||
return bad(key + " currently requires VALUE=DATE (task dates have no time of day)")
|
||||
}
|
||||
if key != "DTSTART" && key != "DUE" && (date || !strings.HasSuffix(p.Value, "Z")) {
|
||||
return bad(key + " requires UTC DATE-TIME")
|
||||
}
|
||||
t, e := p.DateTime(time.UTC)
|
||||
if e != nil || t.Year() < 1 || t.Year() > 9999 {
|
||||
return bad(key)
|
||||
}
|
||||
}
|
||||
if start, due := v.Props.Get("DTSTART"), v.Props.Get("DUE"); start != nil && due != nil && due.Value <= start.Value {
|
||||
return bad("DUE must follow DTSTART")
|
||||
}
|
||||
if _, err = encodeTaskICAL(c); err != nil {
|
||||
return bad("property cardinality")
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
func taskStatusFromICAL(s string) string {
|
||||
return map[string]string{"NEEDS-ACTION": "todo", "IN-PROCESS": "doing", "COMPLETED": "done", "CANCELLED": "cancelled"}[s]
|
||||
}
|
||||
func taskPriorityFromICAL(n int) int {
|
||||
if n == 1 {
|
||||
return 3
|
||||
}
|
||||
if n >= 2 && n <= 4 {
|
||||
return 2
|
||||
}
|
||||
if n == 0 || n == 5 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
func taskFromICAL(c *ical.Calendar, old store.TaskRecord) store.TaskRecord {
|
||||
previousStart, previousDue := old.StartOn, old.DueOn
|
||||
p := c.Children[0].Props
|
||||
old.Title, _ = p.Text("SUMMARY")
|
||||
old.Notes, _ = p.Text("DESCRIPTION")
|
||||
status, _ := p.Text("STATUS")
|
||||
old.Status = taskStatusFromICAL(status)
|
||||
if old.Status == "" {
|
||||
old.Status = "todo"
|
||||
}
|
||||
old.Priority = 1
|
||||
if v := p.Get("PRIORITY"); v != nil {
|
||||
n, _ := strconv.Atoi(v.Value)
|
||||
old.Priority = taskPriorityFromICAL(n)
|
||||
}
|
||||
old.StartOn, old.DueOn = "", ""
|
||||
for key, dst := range map[string]*string{"DTSTART": &old.StartOn, "DUE": &old.DueOn} {
|
||||
if v := p.Get(key); v != nil {
|
||||
t, _ := v.DateTime(time.UTC)
|
||||
*dst = t.Format("2006-01-02")
|
||||
}
|
||||
}
|
||||
if old.StartOn == "" && previousStart != "" && previousDue != "" && previousDue <= previousStart && old.DueOn == previousDue {
|
||||
old.StartOn = previousStart // DTSTART was intentionally absent in this projection.
|
||||
}
|
||||
// Clearing a date must also stop the date-specific reminder.
|
||||
if old.DueOn == "" {
|
||||
old.NotifyDue = false
|
||||
}
|
||||
return old
|
||||
}
|
||||
func encodeTaskICAL(c *ical.Calendar) (string, error) {
|
||||
var b bytes.Buffer
|
||||
if err := ical.NewEncoder(&b).Encode(c); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var out strings.Builder
|
||||
for _, line := range strings.Split(strings.TrimSuffix(b.String(), "\r\n"), "\r\n") {
|
||||
limit := 75
|
||||
for len(line) > limit {
|
||||
n := limit
|
||||
for n > 0 && !utf8.RuneStart(line[n]) {
|
||||
n--
|
||||
}
|
||||
out.WriteString(line[:n] + "\r\n ")
|
||||
line = line[n:]
|
||||
limit = 74
|
||||
}
|
||||
out.WriteString(line + "\r\n")
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
func taskICAL(item store.TaskRecord) (string, string, error) {
|
||||
c := ical.NewCalendar()
|
||||
c.Props.SetText("VERSION", "2.0")
|
||||
c.Props.SetText("PRODID", "-//PagesWiki//Tasks//EN")
|
||||
v := ical.NewComponent("VTODO")
|
||||
c.Children = []*ical.Component{v}
|
||||
uid := item.UUID
|
||||
if item.CalDAV != nil {
|
||||
var err error
|
||||
c, err = parseTaskICAL(item.CalDAV.Data)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
v = c.Children[0]
|
||||
uid = item.CalDAV.UID
|
||||
}
|
||||
p := v.Props
|
||||
p.SetText("UID", uid)
|
||||
p.SetText("SUMMARY", item.Title)
|
||||
p.SetText("DESCRIPTION", item.Notes)
|
||||
status := map[string]string{"todo": "NEEDS-ACTION", "doing": "IN-PROCESS", "done": "COMPLETED", "cancelled": "CANCELLED"}[item.Status]
|
||||
previous, _ := p.Text("STATUS")
|
||||
if previous != status {
|
||||
delete(p, "PERCENT-COMPLETE")
|
||||
}
|
||||
p.SetText("STATUS", status)
|
||||
if item.Status == "done" {
|
||||
p.SetText("PERCENT-COMPLETE", "100")
|
||||
p.SetDateTime("COMPLETED", time.UnixMilli(item.CompletedMS).UTC())
|
||||
} else {
|
||||
delete(p, "COMPLETED")
|
||||
}
|
||||
priority := map[int]string{0: "9", 1: "5", 2: "3", 3: "1"}[item.Priority]
|
||||
if old := p.Get("PRIORITY"); old != nil {
|
||||
n, _ := strconv.Atoi(old.Value)
|
||||
if taskPriorityFromICAL(n) == item.Priority {
|
||||
priority = old.Value
|
||||
}
|
||||
}
|
||||
p.SetText("PRIORITY", priority)
|
||||
for key, date := range map[string]string{"DTSTART": item.StartOn, "DUE": item.DueOn} {
|
||||
delete(p, key)
|
||||
if date != "" {
|
||||
t, _ := time.Parse("2006-01-02", date)
|
||||
p.SetDate(key, t)
|
||||
}
|
||||
}
|
||||
// The UI permits same-day or overdue task dates; VTODO requires DUE > DTSTART.
|
||||
// Omit DTSTART from the projection in that case, keeping UI dates untouched.
|
||||
if item.StartOn != "" && item.DueOn != "" && item.DueOn <= item.StartOn {
|
||||
delete(p, "DTSTART")
|
||||
}
|
||||
p.SetDateTime("DTSTAMP", time.UnixMilli(item.UpdatedMS).UTC())
|
||||
p.SetDateTime("LAST-MODIFIED", time.UnixMilli(item.UpdatedMS).UTC())
|
||||
p.SetDateTime("CREATED", time.UnixMilli(item.CreatedMS).UTC())
|
||||
p.SetText("SEQUENCE", strconv.FormatInt(item.Version-1, 10))
|
||||
data, err := encodeTaskICAL(c)
|
||||
h := sha256.Sum256([]byte(data))
|
||||
return data, fmt.Sprintf(`"%x"`, h), err
|
||||
}
|
||||
Reference in new issue
Block a user