Files
pages/internal/store/task_dav_settings.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

75 lines
2.5 KiB
Go

package store
import (
"context"
"database/sql"
"encoding/json"
"errors"
"strings"
"time"
"unicode"
"unicode/utf8"
)
const DefaultTaskDAVTitle = "PagesWiki Tasks"
func ValidTaskDAVTitle(title string) bool {
return strings.TrimSpace(title) != "" && len(title) <= 300 && utf8.ValidString(title) && strings.IndexFunc(title, unicode.IsControl) < 0
}
// The default list is a personal setting, not a synthetic project. Renaming it
// must not change task membership, resource identities, or another user's view.
func (s *Store) TaskDAVDefaultTitle(ctx context.Context, domain, actor string) (string, error) {
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
return "", err
}
var title string
err := s.DB.QueryRowContext(ctx, `SELECT title FROM organizer_dav_default_lists WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&title)
if errors.Is(err, sql.ErrNoRows) {
return DefaultTaskDAVTitle, nil
}
return title, err
}
func (s *Store) SetTaskDAVDefaultTitle(ctx context.Context, domain, actor, title, credential string) error {
if !ValidTaskDAVTitle(title) {
return ErrTaskInvalid
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
// Acquire the writer before checking authority, as with task writes.
if _, err = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
return err
}
if err = taskActor(ctx, tx, domain, actor, true); err != nil {
return err
}
if credential != "" {
if err = taskDAVCredentialAllowed(ctx, tx, domain, actor, credential, true); err != nil {
return err
}
}
var raw string
err = tx.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=? AND key='system_extension_states'`, domain).Scan(&raw)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
states := map[string]bool{}
if raw != "" && json.Unmarshal([]byte(raw), &states) != nil {
return ErrAccessDenied
}
if enabled, ok := states["organizer.tasks"]; ok && !enabled {
return ErrAccessDenied
}
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_dav_default_lists(domain_uuid,owner_uuid,title,updated_at_ms) VALUES(?,?,?,?) ON CONFLICT(domain_uuid,owner_uuid) DO UPDATE SET title=excluded.title,updated_at_ms=excluded.updated_at_ms`, domain, actor, title, time.Now().UnixMilli())
if err != nil {
return err
}
return tx.Commit()
}