Users can now subscribe to a remote ICS/webcal feed from the dashboard (name + color), the same way they set up a real calendar or the virtual Birthdays calendar. Subscriptions are read-only, per-user, and share the "/cal/home/<name>/" namespace with real calendars and "birthdays" (name collisions are rejected in both directions). - internal/db: new ics_subscriptions table + CRUD (internal/db/ics.go); CreateCalendarWithColor checks for a colliding subscription name. - internal/icssub: shared HTTP-fetch + TTL cache (15 min) for remote ICS calendars, with webcal:// -> https:// rewriting and stale-on-error fallback, used by both the web UI and the CalDAV backend. - internal/web: dashboard "Subscribe to an ICS/webcal calendar" form, color picker, delete button (internal/web/ics.go, templates/dashboard.templ); month view renders subscription events in their chosen color, read-only (internal/web/calendar.go). - internal/caldav: subscriptions are exposed as read-only calendars (internal/caldav/ics.go) - listed in PROPFIND, events served via GET/REPORT, PUT/DELETE on individual events rejected with 403, but DELETE on the calendar itself unsubscribes; calendar-color is injected the same way as for real calendars and Birthdays. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
172 lines
5.4 KiB
Go
172 lines
5.4 KiB
Go
// Package db provides a lightweight SQLite-backed store for data that
|
|
// doesn't fit the plain-file model used by internal/store — currently
|
|
// calendar/address-book sharing grants. It's intentionally small: no ORM,
|
|
// just database/sql with hand-written queries, so it stays easy to
|
|
// extend when user management and the web UI are added later.
|
|
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
// DB wraps a SQLite connection and exposes typed helpers for the
|
|
// application's tables.
|
|
type DB struct {
|
|
conn *sql.DB
|
|
}
|
|
|
|
// Open opens (creating if necessary) the SQLite database at path and runs
|
|
// schema migrations. The parent directory of path is created if it
|
|
// doesn't already exist.
|
|
func Open(path string) (*DB, error) {
|
|
if dir := filepath.Dir(path); dir != "" && dir != "." {
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return nil, fmt.Errorf("creating database directory %q: %w", dir, err)
|
|
}
|
|
}
|
|
|
|
conn, err := sql.Open("sqlite", path+"?_pragma=foreign_keys(1)")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening database %q: %w", path, err)
|
|
}
|
|
// SQLite only supports one writer at a time; a single connection avoids
|
|
// "database is locked" errors under concurrent access.
|
|
conn.SetMaxOpenConns(1)
|
|
|
|
d := &DB{conn: conn}
|
|
if err := d.migrate(); err != nil {
|
|
conn.Close()
|
|
return nil, fmt.Errorf("migrating database: %w", err)
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// Close closes the underlying connection.
|
|
func (d *DB) Close() error {
|
|
return d.conn.Close()
|
|
}
|
|
|
|
func (d *DB) migrate() error {
|
|
const schema = `
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
username TEXT PRIMARY KEY,
|
|
password_hash TEXT NOT NULL,
|
|
display_name TEXT NOT NULL DEFAULT '',
|
|
email TEXT NOT NULL DEFAULT '',
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS calendars (
|
|
owner TEXT NOT NULL REFERENCES users (username) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
color TEXT NOT NULL DEFAULT '',
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (owner, name)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS addressbooks (
|
|
owner TEXT NOT NULL REFERENCES users (username) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (owner, name)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS calendar_shares (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
owner TEXT NOT NULL,
|
|
calendar_name TEXT NOT NULL,
|
|
shared_with TEXT NOT NULL,
|
|
permission TEXT NOT NULL CHECK (permission IN ('read', 'write')),
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE (owner, calendar_name, shared_with)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS addressbook_shares (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
owner TEXT NOT NULL,
|
|
addressbook_name TEXT NOT NULL,
|
|
shared_with TEXT NOT NULL,
|
|
permission TEXT NOT NULL CHECK (permission IN ('read', 'write')),
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE (owner, addressbook_name, shared_with)
|
|
);
|
|
|
|
-- Per-user display color for the synthetic "Birthdays" calendar (computed
|
|
-- from contacts' BDAY fields, not a real user-created calendar — see
|
|
-- internal/web/calendar.go and internal/caldav/backend.go). One row per
|
|
-- user; absent means "use the built-in default color".
|
|
CREATE TABLE IF NOT EXISTS birthday_calendars (
|
|
owner TEXT PRIMARY KEY REFERENCES users (username) ON DELETE CASCADE,
|
|
color TEXT NOT NULL DEFAULT ''
|
|
);
|
|
|
|
-- User-added read-only calendars backed by a remote ICS/webcal URL (see
|
|
-- internal/db/ics.go, internal/web/ics.go, internal/caldav/ics.go). The
|
|
-- local calendar name (like real calendars) is unique per owner and lives
|
|
-- in the same "/cal/home/<name>/" namespace, so name collisions with the
|
|
-- calendars table and reservedCalendarNames are checked at creation time.
|
|
CREATE TABLE IF NOT EXISTS ics_subscriptions (
|
|
owner TEXT NOT NULL REFERENCES users (username) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
url TEXT NOT NULL,
|
|
color TEXT NOT NULL DEFAULT '',
|
|
PRIMARY KEY (owner, name)
|
|
);
|
|
|
|
-- Web UI login sessions. Sessions are opaque random tokens stored server
|
|
-- side (not JWTs) so they can be revoked instantly by deleting the row.
|
|
CREATE TABLE IF NOT EXISTS web_sessions (
|
|
token TEXT PRIMARY KEY,
|
|
username TEXT NOT NULL,
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
expires_at DATETIME NOT NULL
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_web_sessions_expires_at ON web_sessions (expires_at);
|
|
`
|
|
_, err := d.conn.Exec(schema)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return d.migrateAddColumns()
|
|
}
|
|
|
|
// migrateAddColumns adds columns to already-existing tables that predate
|
|
// their introduction. CREATE TABLE IF NOT EXISTS above only creates a
|
|
// table's initial shape, so columns added later (like calendars.color)
|
|
// need an explicit ALTER TABLE for databases created before this change.
|
|
func (d *DB) migrateAddColumns() error {
|
|
hasColumn := func(table, column string) (bool, error) {
|
|
rows, err := d.conn.Query(`SELECT name FROM pragma_table_info(?)`, table)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var name string
|
|
if err := rows.Scan(&name); err != nil {
|
|
return false, err
|
|
}
|
|
if name == column {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, rows.Err()
|
|
}
|
|
|
|
ok, err := hasColumn("calendars", "color")
|
|
if err != nil {
|
|
return fmt.Errorf("checking calendars.color column: %w", err)
|
|
}
|
|
if !ok {
|
|
if _, err := d.conn.Exec(`ALTER TABLE calendars ADD COLUMN color TEXT NOT NULL DEFAULT ''`); err != nil {
|
|
return fmt.Errorf("adding calendars.color column: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|