Files
pages/internal/calendar/preferences.go
T
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

159 lines
3.4 KiB
Go

package calendar
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"unicode"
ical "github.com/emersion/go-ical"
"golang.org/x/text/unicode/norm"
)
var ErrNameTaken = errors.New("pretty_name is already in use")
func ValidPrettyName(s string) bool {
if s == "" || len(s) > 120 || norm.NFC.String(s) != s || validID(s) {
return false
}
for _, c := range s {
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '-' && c != '_' {
return false
}
}
return true
}
func (r *Repository) uniqueName(domain, id, name string) error {
entries, e := os.ReadDir(filepath.Join(r.Root, domain))
if e != nil {
return e
}
for _, entry := range entries {
if !entry.IsDir() || !validID(entry.Name()) || entry.Name() == id {
continue
}
c, e := readMeta(filepath.Join(r.Root, domain, entry.Name()))
if errors.Is(e, ErrMissing) {
continue
}
if e != nil {
return e
}
if strings.EqualFold(c.PrettyName, name) {
return ErrNameTaken
}
}
return nil
}
func renameSnapshots(dir string, c *Calendar) error {
for month, hash := range c.Months {
b, e := os.ReadFile(filepath.Join(dir, month+"."+hash+".ics"))
if e != nil {
return e
}
if digest(b) != hash {
return ErrInvalid
}
cal, e := ical.NewDecoder(strings.NewReader(string(b))).Decode()
if e != nil {
return e
}
cal.Props.SetText("NAME", c.PrettyName)
data, e := encode(cal)
if e != nil {
return e
}
h := digest([]byte(data))
if e = atomicFile(filepath.Join(dir, month+"."+h+".ics"), []byte(data)); e != nil {
return e
}
c.Months[month] = h
}
return nil
}
func pruneSnapshots(dir string, c Calendar) {
files, _ := filepath.Glob(filepath.Join(dir, "*.ics"))
keep := map[string]bool{}
for month, hash := range c.Months {
keep[month+"."+hash+".ics"] = true
}
for _, file := range files {
if !keep[filepath.Base(file)] {
_ = os.Remove(file)
}
}
}
// Defaults belong to a principal within a Domain. Persist identities, not names.
// Inaccessible calendars are filtered on every read, never used as grants.
func (r *Repository) Defaults(domain, actor string) ([]string, error) {
if actor == "" {
return []string{}, nil
}
if !validID(domain) || !validID(actor) {
return nil, ErrForbidden
}
b, e := os.ReadFile(filepath.Join(r.Root, domain, "preferences", actor+".json"))
if os.IsNotExist(e) {
return []string{}, nil
}
if e != nil {
return nil, e
}
var ids []string
e = json.Unmarshal(b, &ids)
if e != nil {
return nil, e
}
visible, e := r.List(domain, actor)
if e != nil {
return nil, e
}
allowed := map[string]bool{}
for _, c := range visible {
allowed[c.ID] = true
}
out := []string{}
for _, id := range ids {
if allowed[id] {
out = append(out, id)
}
}
return out, nil
}
func (r *Repository) SaveDefaults(ctx context.Context, domain, actor string, ids []string) error {
if !validID(domain) || !validID(actor) {
return ErrForbidden
}
if len(ids) > 32 {
return ErrInvalid
}
seen := map[string]bool{}
for _, id := range ids {
if seen[id] {
return ErrInvalid
}
seen[id] = true
if _, e := r.Get(domain, id, actor); e != nil {
return e
}
}
dir := filepath.Join(r.Root, domain, "preferences")
if e := os.MkdirAll(dir, 0700); e != nil {
return e
}
unlock, e := lock(ctx, dir)
if e != nil {
return e
}
defer unlock()
b, e := json.Marshal(ids)
if e != nil {
return e
}
return atomicFile(filepath.Join(dir, actor+".json"), b)
}