maarcadetweet: initial commit

AT Protocol PDS + AppView + Tauri Desktop Client, 160-char post limit.

- PDS (Rust + axum + sqlx)
  - Auth: createAccount, createSession, refreshSession
  - Records: createRecord, deleteRecord (race-safe via SELECT FOR UPDATE)
  - Feed: feed.like.create, feed.repost.create
  - Sync: getRepo, getBlocks, getLatestCommit, getRecord (with MST proof), listRepos
  - Identity: resolveHandle
  - MST: spec-conformant (at-mst crate, 27 tests)
  - Repo: signed commits, TID counter (monotonic, 4096 wrap safe)

- AppView (Rust + axum + sqlx)
  - Jetstream consumer (WebSocket, exponential backoff, 38k+ events indexed)
  - REST API: timeline/home (graph-aware), profile, search, post (with thread hydration)
  - Handle-sync worker (did:plc + did:web)
  - JSONB embed storage + thread columns (migration 0003)
  - Like/repost counter cache (migration 0004)

- Tauri 2 + Svelte 5 Desktop Client
  - System tray (Show/Compose/Quit menu)
  - OS notifications (tauri-plugin-notification)
  - Auto-update (tauri-plugin-updater, placeholder endpoint)
  - Window-state (tauri-plugin-window-state)
  - 160-char compose with live counter
  - Image/Link embed rendering
  - LocalStorage-persisted like state
  - Timeline with poll (prepend new posts)
  - Custom TitleBar (transparent, no decorations)
  - Orange/IBM Plex Mono maarcade design

Tests: 231 Rust + 9 vitest = 240 passed.
This commit is contained in:
tomdebone
2026-07-05 20:01:31 +02:00
commit c586fd39c9
134 changed files with 35279 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
-- AppView database schema (initial)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- =====================================================
-- posts
-- =====================================================
CREATE TABLE posts (
uri TEXT PRIMARY KEY, -- at://did/rkey/app.twi.post
did TEXT NOT NULL,
handle TEXT NOT NULL,
rkey TEXT NOT NULL,
collection TEXT NOT NULL, -- app.twi.post | app.bsky.feed.post
text TEXT NOT NULL,
cid TEXT NOT NULL,
parent_uri TEXT, -- reply parent
root_uri TEXT, -- thread root
langs TEXT[],
indexed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX posts_did_idx ON posts (did);
CREATE INDEX posts_created_at_idx ON posts (created_at DESC);
CREATE INDEX posts_collection_idx ON posts (collection);
CREATE INDEX posts_parent_idx ON posts (parent_uri) WHERE parent_uri IS NOT NULL;
CREATE INDEX posts_root_idx ON posts (root_uri) WHERE root_uri IS NOT NULL;
-- =====================================================
-- likes
-- =====================================================
CREATE TABLE likes (
uri TEXT PRIMARY KEY,
did TEXT NOT NULL,
post_uri TEXT NOT NULL,
post_cid TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
indexed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX likes_post_idx ON likes (post_uri);
CREATE INDEX likes_did_idx ON likes (did);
-- =====================================================
-- reposts
-- =====================================================
CREATE TABLE reposts (
uri TEXT PRIMARY KEY,
did TEXT NOT NULL,
post_uri TEXT NOT NULL,
post_cid TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
indexed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX reposts_post_idx ON reposts (post_uri);
CREATE INDEX reposts_did_idx ON reposts (did);
-- =====================================================
-- follows
-- =====================================================
CREATE TABLE follows (
follower_did TEXT NOT NULL,
subject_did TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
indexed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (follower_did, subject_did)
);
CREATE INDEX follows_subject_idx ON follows (subject_did);
-- =====================================================
-- timeline cache (materialized per-user timelines)
-- =====================================================
CREATE TABLE timeline_cache (
did TEXT NOT NULL,
post_uri TEXT NOT NULL,
score DOUBLE PRECISION NOT NULL,
ranked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (did, post_uri)
);
CREATE INDEX timeline_did_score_idx ON timeline_cache (did, score DESC, ranked_at DESC);
-- =====================================================
-- search (simple trigram)
-- =====================================================
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX posts_text_trgm_idx ON posts USING GIN (text gin_trgm_ops);
-- =====================================================
-- jetstream cursor
-- =====================================================
CREATE TABLE jetstream_cursor (
id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1),
cursor BIGINT NOT NULL DEFAULT 0,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
INSERT INTO jetstream_cursor (id, cursor) VALUES (1, 0);
@@ -0,0 +1,15 @@
-- AppView database migration 0002
-- Adds pagination indexes that support the keyset `(indexed_at DESC, uri DESC)`
-- ordered queries used by /api/timeline/home and /api/profile. Without these,
-- every page request triggers a full table scan + sort.
--
-- Also fixes an UPSERT bug: re-indexing the same post rewrote `indexed_at`,
-- which shifted the row in the pagination order and caused mid-pagination
-- users to silently skip or duplicate posts.
CREATE INDEX IF NOT EXISTS posts_collection_indexed_at_uri_idx
ON posts (collection, indexed_at DESC, uri DESC);
CREATE INDEX IF NOT EXISTS posts_did_indexed_at_uri_idx
ON posts (did, indexed_at DESC, uri DESC)
WHERE collection IN ('app.twi.post','app.bsky.feed.post');
@@ -0,0 +1,38 @@
-- AppView database migration 0003
-- Adds embed + thread-context columns to `posts`.
--
-- Why
--
-- Phase 4 (indexer) only stored `text` + `parent_uri` + `root_uri`, which
-- was enough to render plain-text timelines. Phase 5 wants embeds (images,
-- link cards, quoted posts) and thread context visible in the UI.
--
-- Columns
-- embed JSONB, nullable — the full AT-Protocol embed
-- object as it appears in the record value. We
-- store the whole thing verbatim rather than
-- normalising into a separate `embeds` table so
-- the UI can decode it without a second round
-- trip, and so a future lexicon change doesn't
-- require a schema migration.
-- reply_parent_handle TEXT, nullable — display handle for
-- `parent_uri`'s author. Backfilled by the
-- handle-sync worker. Nullable because the
-- sync worker hasn't seen the row yet, OR
-- because the parent isn't in our index.
-- reply_root_handle TEXT, nullable — display handle for
-- `root_uri`'s author. Same semantics.
-- reply_root_uri TEXT, nullable — duplicate of `root_uri`
-- for the "show full thread" link target.
-- Kept as its own column so the index covers
-- it without having to special-case NULLs on
-- the existing `root_uri`.
--
-- All columns are nullable. Pre-existing rows will continue to render
-- correctly — old posts simply have `embed = NULL` and the UI omits the
-- embed block.
ALTER TABLE posts ADD COLUMN IF NOT EXISTS embed JSONB;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_parent_handle TEXT;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_root_handle TEXT;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_root_uri TEXT;
@@ -0,0 +1,45 @@
-- AppView migration 0004: like/repost counter cache + uniqueness
--
-- Phase 5b review identified two issues:
-- H3 — Users could spam `feed.like.create` against the same post because
-- the `likes` PK is just `uri` (the like's own rkey). Adding a
-- partial unique index lets us short-circuit "already liked" at the
-- DB layer instead of relying on caller discipline.
-- H6 — `SELECT COUNT(*) FROM likes WHERE post_uri = $1` on every post fetch
-- doesn't scale. We add a denormalized counter on `posts` that's
-- kept consistent by the AppView's own ingest path. Jetstream-driven
-- upserts touch this too. The PDS path goes through ingest-commit
-- which routes through the same code.
--
-- Idempotent: dedup likes/reposts first (real-world Jetstream replays can
-- leave duplicates). Keep only the oldest row per (did, post_uri).
-- dedup likes: keep only the lowest uri per (did, post_uri)
DELETE FROM likes a USING likes b
WHERE a.did = b.did
AND a.post_uri = b.post_uri
AND a.uri > b.uri;
-- dedup reposts: same approach
DELETE FROM reposts a USING reposts b
WHERE a.did = b.did
AND a.post_uri = b.post_uri
AND a.uri > b.uri;
CREATE UNIQUE INDEX IF NOT EXISTS likes_did_post_uri_idx
ON likes (did, post_uri);
CREATE UNIQUE INDEX IF NOT EXISTS reposts_did_post_uri_idx
ON reposts (did, post_uri);
ALTER TABLE posts ADD COLUMN IF NOT EXISTS like_count BIGINT NOT NULL DEFAULT 0;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS repost_count BIGINT NOT NULL DEFAULT 0;
-- Backfill: compute current counts from the now-deduped rows so the
-- column matches reality on upgrade. Wrapped in a single statement so
-- it's fast even on 100k+ rows.
UPDATE posts p
SET like_count = COALESCE((SELECT COUNT(*) FROM likes WHERE post_uri = p.uri), 0),
repost_count = COALESCE((SELECT COUNT(*) FROM reposts WHERE post_uri = p.uri), 0);
CREATE INDEX IF NOT EXISTS posts_count_idx ON posts (like_count DESC, repost_count DESC);
+99
View File
@@ -0,0 +1,99 @@
-- PDS database schema (initial)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- =====================================================
-- users
-- =====================================================
CREATE TABLE users (
did TEXT PRIMARY KEY, -- did:plc:...
handle TEXT NOT NULL UNIQUE, -- alice.maarcadetweet.local
email TEXT, -- nullable for did:web
password_hash TEXT, -- argon2id; nullable
signing_key BYTEA NOT NULL, -- compressed secp256k1 pubkey
rotation_key BYTEA NOT NULL, -- for PLC rotation ops
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX users_handle_idx ON users (LOWER(handle));
-- =====================================================
-- repos
-- =====================================================
CREATE TABLE repos (
did TEXT PRIMARY KEY REFERENCES users(did) ON DELETE CASCADE,
rev TEXT NOT NULL, -- TID-encoded revision counter
head_cid BYTEA NOT NULL, -- CID of latest commit
head_commit BYTEA NOT NULL, -- CBOR block of latest commit
prev_commit BYTEA, -- for fast linear back-link
indexed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- =====================================================
-- repo blocks (MST nodes + records)
-- =====================================================
CREATE TABLE repo_blocks (
did TEXT NOT NULL,
cid BYTEA NOT NULL,
block BYTEA NOT NULL,
size INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (did, cid)
);
CREATE INDEX repo_blocks_did_idx ON repo_blocks (did);
-- =====================================================
-- blobs
-- =====================================================
CREATE TABLE blobs (
cid TEXT PRIMARY KEY, -- bafy...
did TEXT NOT NULL,
mime_type TEXT NOT NULL,
size BIGINT NOT NULL,
storage_key TEXT NOT NULL, -- S3 object key
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX blobs_did_idx ON blobs (did);
-- =====================================================
-- sessions
-- =====================================================
CREATE TABLE sessions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
did TEXT NOT NULL REFERENCES users(did) ON DELETE CASCADE,
access_jwt TEXT NOT NULL, -- ES256K signed
refresh_jwt TEXT NOT NULL, -- ES256 signed (different key)
access_expires_at TIMESTAMPTZ NOT NULL,
refresh_expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
revoked_at TIMESTAMPTZ
);
CREATE INDEX sessions_did_idx ON sessions (did);
-- =====================================================
-- plc operations (audit log of submitted ops)
-- =====================================================
CREATE TABLE plc_ops (
id BIGSERIAL PRIMARY KEY,
did TEXT NOT NULL,
prev TEXT, -- CID of previous op, or null for create
op_cid TEXT NOT NULL,
signed_op BYTEA NOT NULL, -- DAG-CBOR
submitted BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX plc_ops_did_idx ON plc_ops (did);
-- =====================================================
-- update trigger for users.updated_at
-- =====================================================
CREATE OR REPLACE FUNCTION touch_updated_at() RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER users_touch BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();
+21
View File
@@ -0,0 +1,21 @@
-- Add a per-block MIME type column.
--
-- Phase 7 (uploadBlob + MIME detection). `uploadBlob` records the
-- client's `Content-Type` (or the sniffed fallback) here so that
-- `com.atproto.sync.getBlob` can serve the right `Content-Type`
-- header without sniffing on every read.
--
-- `IF NOT EXISTS` keeps the migration idempotent — re-running it on
-- a database that already has the column is a no-op rather than an
-- error. Existing rows (from before this migration) leave the
-- column NULL; the read path falls back to magic-byte sniffing and
-- then `application/octet-stream`.
ALTER TABLE repo_blocks ADD COLUMN IF NOT EXISTS mime_type TEXT;
-- An index helps the per-CID mime lookup used by the Tauri shortcut
-- `GET /blob/{cid}`, which scans by CID alone (no DID). We index
-- *all* rows on (cid) — the lookup wants the row regardless of
-- whether mime_type is set, and a partial index would silently
-- regress to a seq-scan when a row's mime_type happens to be NULL.
CREATE INDEX IF NOT EXISTS repo_blocks_cid_idx ON repo_blocks (cid);