Files
Anya Tokugawa c1175d389f 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.
2026-09-27 14:44:43 +09:00

176 lines
5.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"time"
"unicode"
cal "pageswiki/internal/calendar"
"pageswiki/internal/store"
)
func (s *Server) handleTaskPlanning(w http.ResponseWriter, r *http.Request, actor string, active bool, parts []string) {
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "organizer.calendar") {
http.NotFound(w, r)
return
}
if r.Method != "GET" && !active {
taskError(w, store.ErrAccessDenied)
return
}
// A live membership check is needed even for an empty calendar picker.
if _, _, err := s.store.ListTasks(r.Context(), domain, actor, store.TaskQuery{Kind: "task", View: "all", Today: time.Now().In(s.getLocation(domain)).Format("2006-01-02"), Limit: 1}); err != nil {
taskError(w, err)
return
}
repo, err := s.calendars()
if err != nil {
taskError(w, err)
return
}
if parts[0] == "calendars" {
if len(parts) != 1 {
http.NotFound(w, r)
return
}
if r.Method != "GET" {
w.WriteHeader(405)
return
}
items, err := repo.List(domain, actor)
if err != nil {
calendarError(w, err)
return
}
out := []map[string]string{}
for _, c := range items {
if c.PersonalLocal(actor) {
out = append(out, map[string]string{"id": c.ID, "name": c.Name, "pretty_name": c.PrettyName})
}
}
writeEventJSON(w, 200, map[string]any{"items": out, "timezone": s.getTimezoneName(domain)})
return
}
if len(parts) == 1 && r.Method == "GET" {
items, err := s.store.TaskBlocks(r.Context(), domain, actor, r.URL.Query().Get("task_uuid"))
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 200, map[string]any{"items": items})
return
}
if len(parts) == 2 && r.Method == "DELETE" {
// Tombstone only: unlink is explicitly NOT calendar event deletion.
if err := s.store.SetTaskBlockState(r.Context(), domain, actor, parts[1], "unlinked"); err != nil {
taskError(w, err)
return
}
w.WriteHeader(204)
return
}
if len(parts) == 2 && r.Method == "GET" {
b, err := s.store.GetTaskBlock(r.Context(), domain, actor, parts[1])
if err != nil {
taskError(w, err)
return
}
c, event, err := repo.TaskBlock(r.Context(), domain, b.CalendarUUID, actor, b.EventUID, nil)
out := map[string]any{"item": b, "available": err == nil}
if err == nil {
out["start"] = event.Start
out["end"] = event.End
out["etag"] = event.ETag
out["href"] = "/v/plan/event/" + url.PathEscape(b.EventUID) + "?calendar=" + url.QueryEscape(c.PrettyName)
}
// No summary, description, imported data, or backend paths are exposed.
writeEventJSON(w, 200, out)
return
}
if len(parts) != 1 || r.Method != "POST" {
w.WriteHeader(405)
return
}
var input struct {
UUID string `json:"uuid"`
TaskUUID string `json:"task_uuid"`
CalendarUUID string `json:"calendar_uuid"`
Version int64 `json:"task_version"`
Start string `json:"start"`
End string `json:"end"`
Timezone string `json:"timezone"`
}
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8192))
dec.DisallowUnknownFields()
if err := dec.Decode(&input); err != nil {
taskError(w, store.ErrTaskInvalid)
return
}
// RFC3339 is unambiguous on retries, even after Domain timezone changes.
parse := func(value string) (time.Time, error) {
if t, err := time.Parse(time.RFC3339, value); err == nil {
return t, nil
}
loc, err := time.LoadLocation(input.Timezone)
if err != nil || input.Timezone == "" || input.Timezone == "Local" {
return time.Time{}, store.ErrTaskInvalid
}
t, err := time.ParseInLocation("2006-01-02T15:04", value, loc)
if err != nil || t.Format("2006-01-02T15:04") != value {
return time.Time{}, store.ErrTaskInvalid
}
return t, nil
}
start, e1 := parse(input.Start)
end, e2 := parse(input.End)
if e1 != nil || e2 != nil {
taskError(w, store.ErrTaskInvalid)
return
}
c, err := repo.Get(domain, input.CalendarUUID, actor)
if err != nil {
calendarError(w, err)
return
}
if !c.PersonalLocal(actor) {
taskError(w, store.ErrAccessDenied)
return
}
b, err := s.store.ReserveTaskBlock(r.Context(), domain, actor, store.TaskBlock{UUID: input.UUID, TaskUUID: input.TaskUUID, CalendarUUID: input.CalendarUUID, Start: start, End: end}, input.Version)
if err != nil {
taskError(w, err)
return
}
var draft *cal.Event
if b.State == "pending" {
// Only the explicitly approved title snapshot is copied, never notes.
title := strings.Join(strings.Fields(strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return ' '
}
return r
}, b.Title)), " ")
event, e := cal.Build("", cal.Event{UID: b.EventUID, Summary: "Focus time(" + title + ")", Start: b.Start, End: b.End})
if e != nil {
calendarError(w, e)
return
}
draft = &event
}
_, _, err = repo.TaskBlock(r.Context(), domain, b.CalendarUUID, actor, b.EventUID, draft)
if err != nil {
calendarError(w, err)
return
}
if err = s.store.SetTaskBlockState(r.Context(), domain, actor, b.UUID, "active"); err != nil {
taskError(w, err)
return
}
b.State = "active"
writeEventJSON(w, 201, map[string]any{"item": b})
}