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

136 lines
3.7 KiB
Go

package calendar
import (
"context"
"database/sql"
"encoding/base64"
"strings"
)
func DAVFilename(c Calendar, uid string) string {
if name := c.DAVNames[uid]; name != "" {
return name
}
return base64.RawURLEncoding.EncodeToString([]byte(uid)) + ".ics"
}
func ValidDAVFilename(name string) bool {
if len(name) < 5 || len(name) > 700 || !strings.HasSuffix(name, ".ics") {
return false
}
for _, ch := range name {
if !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '-' || ch == '_' || ch == '.') {
return false
}
}
return true
}
func DAVUID(c Calendar, name string) (string, error) {
if !ValidDAVFilename(name) {
return "", ErrMissing
}
for uid, n := range c.DAVNames {
if n == name {
return uid, nil
}
}
b, err := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(name, ".ics"))
if err != nil || len(b) == 0 || len(b) > 512 {
return "", ErrMissing
}
return string(b), nil
}
// DAVSnapshot is complete or fails; a truncated listing can cause client deletes.
// The metadata lock keeps href mappings and event generations in one snapshot.
func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string, withData bool) (Calendar, []Event, error) {
c, _, unlock, err := r.locked(ctx, domain, id, actor)
if err != nil {
return c, nil, err
}
defer unlock()
cols := "uid,etag,''"
if withData {
cols = "uid,etag,data"
}
rows, err := r.DB.QueryContext(ctx, `SELECT `+cols+` FROM calendar_event_cache WHERE domain=? AND calendar=? ORDER BY uid LIMIT 10001`, domain, id)
if err != nil {
return c, nil, err
}
defer rows.Close()
items := []Event{}
size := 0
for rows.Next() {
var item Event
if err = rows.Scan(&item.UID, &item.ETag, &item.Data); err != nil {
return c, nil, err
}
size += len(item.Data) + len(item.UID) + len(item.ETag)
if len(items) >= 10000 || size > 32<<20 {
return c, nil, ErrLimit
}
items = append(items, item)
}
return c, items, rows.Err()
}
// ChangeDAV reserves resource identities in the same manifest commit as the
// monthly snapshots. Existing resources still require an exact strong ETag.
func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, match string, item *Event, authorize func() error) (Event, bool, error) {
if !ValidDAVFilename(name) {
return Event{}, false, ErrInvalid
}
c, dir, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.CanWrite(actor) })
if err != nil {
return Event{}, false, err
}
defer unlock()
if authorize != nil {
if err = authorize(); err != nil {
return Event{}, false, ErrForbidden
}
}
uid, _ := DAVUID(c, name)
var exists bool
if uid != "" {
if err = r.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?)`, domain, id, uid).Scan(&exists); err != nil {
return Event{}, false, err
}
}
if exists {
if match == "" {
return Event{}, false, ErrPreconditionRequired
}
if item != nil && item.UID != uid {
return Event{}, false, ErrConflict
}
} else {
if item == nil {
return Event{}, false, ErrMissing
}
if match != "" && match != "*" {
return Event{}, false, ErrConflict
}
uid = item.UID
var tag string
err = r.DB.QueryRowContext(ctx, `SELECT etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&tag)
if err == nil {
return Event{}, false, ErrConflict
}
if err != sql.ErrNoRows {
return Event{}, false, err
}
if len(c.DAVNames) >= 10000 {
return Event{}, false, ErrLimit
}
if c.DAVNames == nil {
c.DAVNames = map[string]string{}
}
c.DAVNames[uid] = name
match = "*"
}
result, err := r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
return result, !exists, err
}