9.9 KiB · text History 7ce364f

recourse mobile API

The JSON API the Android app (android/, plan in docs/android.md) talks to. It follows the family’s conventions (pin_flip’s docs/api.md, party-slut’s /api/v1), and the server half is rosin_hunter_3000’s shape: a /api/v1 nest with its own bearer-only middleware.

Signing in

Each device gets its own token. Only its SHA-256 is stored; the token itself is shown once.

  1. On the web, open Devices (the account menu) → Pair a device. The page shows a QR of recourse://pair?base=<server>&token=<token>, and the two fields as text. base is PUBLIC_URL when set, otherwise the origin the browser is using (X-Forwarded-Proto/-Host, then Host).
  2. The app scans it (or the phone opens the link, or the fields are typed), calls GET /api/v1/me, and keeps the token only if that answers 200.

Send it as Authorization: Bearer <token>. The session cookie is not accepted here: the API has no CSRF protection of its own.

A token stays valid for 90 days after it was last used; use is recorded at most once a day, and each recording slides the expiry. Tokens minted before expiries existed keep none. A deactivated account’s tokens stop working. DELETE /api/v1/sessions/current unpairs the calling device; the Devices page and DELETE /api/v1/me/devices/{id} revoke any. A revoked token’s next request is a 401.

The older GET /api/profile (cookie or bearer) is unchanged.

Conventions

  • Format: snake_case keys, UUIDv7 ids, ISO-8601 UTC timestamps.
  • Permissions: may_write, may_create_tags, can_reply, can_edit_tags, can_post, can_edit are computed by the server; the app shows or hides controls from them and holds no rules of its own. The server checks every write regardless.
  • Visibility: exactly the web’s. A category is visible when it and every ancestor admit the user’s trust level (or they’re staff). Anything you can’t see is a 404, whether or not it exists — topics, posts, tags used only in hidden categories, and writes aimed at any of them. Lists (search, tags, bookmarks, notifications, unread count) leave hidden things out.
  • Email verification: when the site requires it, an unverified account reads everything it may see, and every write answers 403 unverified. POST /me/verification sends a new link.
  • Reading a topic counts a view (at most once per ten minutes per user) and marks it read up to its last post, as on the web.
  • Toggles take the desired state ({"bookmarked": true}), so a retried request lands on the same value.
  • Images: avatar_url is server-relative (/u/{id}/avatar), public, and is either the upload or a generated SVG.
  • Post bodies: cooked is the server’s sanitized HTML of the markdown — the web’s own rendering. Off-site links in it are the server’s absolute /out?u=<destination>&s=<signature> interstitial (the app unwraps u and asks before leaving); links to the forum are direct.
  • Errors always use this shape: {"error": {"code": "...", "message": "...", "details": {...}|null}}
Status Codes
400 bad_request (unreadable JSON or a malformed id; details.reason)
401 unauthorized
403 reactions_off, reaction_not_offered (the category’s reactions), unverified, silenced (a cooldown: read-only), locked (topic locked by staff), staff_only / trust_level (the category’s posting rule, or the trust needed to flag), own_post (flagging your own), moderated / hidden (a post staff moderated or that is hidden pending review), not_author (tags)
409 already_flagged
413 too_long (a post over MAX_POST_CHARS, 32,000 by default; the message says both lengths)
404 not_found
410 removed (a deleted topic, for non-staff)
422 invalid (the message says what)

Endpoints

All paths start with /api/v1, and all need a token.

Me and devices

Method Path Notes
GET /me {user, verification_required, min_trust_to_create_tag}. user has preferred_view (classic/focus), preferred_sort, may_write, email_verified, trust_level, trust_name, …
PATCH /me {layout?, sort?, reaction_notifications?} → as GET /me. The web’s profile reads the same fields.
PUT /me/avatar multipart, part avatar (PNG/JPEG/GIF/WebP, ≤ 5 MB) → as GET /me
DELETE /me/avatar back to the letter avatar → as GET /me
POST /me/verification {result: sent | recent | verified | failed}; rate-limited as on the web
GET /me/devices {devices: [{id, label, created_at, last_used_at, expires_at, current}]}
DELETE /me/devices/{id} 204
DELETE /sessions/current 204; this device’s token is revoked

Browsing

Method Path Notes
GET /categories {categories: [{id, slug, name, colors, description, parent_id, depth, topic_count, watching, posting, can_create_topic, can_reply, reactions}]} in tree order
GET /categories/{slug}?sort= {category, ancestors, subcategories, sort, topics, can_create_topic, create_blocked, can_post} (can_post = can_create_topic, kept for older apps). topics covers the category and its visible subcategories, pinned first. A given sort is saved as the account’s, as on the web.
PUT /categories/{slug}/watch {watching} → {watching}
POST /categories/{slug}/topics {title, body, tags?: [..]} → 201, as GET /topics/{id}
GET /topics/{id} {topic, posts, can_reply, reply_blocked, can_react, can_flag, can_edit_tags, watching}
POST /topics/{id}/posts {body} (markdown) → 201, a post
PUT /topics/{id}/tags {tags: [..]} → {tags}. Replaces; up to five; new names need may_create_tags and are otherwise dropped.
PUT /posts/{id}/reaction {key} or {key: null} → {reactions}
PUT /posts/{id}/bookmark {bookmarked} → {bookmarked}
POST /posts/{id}/flags {reason, note?} → 201 {flagged: true}. reason is spam, abusive, off_topic or other. Once per post (409), not your own, trust level MIN_TRUST_TO_FLAG and up.

A topic summary (in every list) is {id, title, category, author, is_pinned, is_locked, is_closed, posts_count, replies, views, created_at, bumped_at, tags, posters, reactions, is_new, unread, notes} — what a Focus tile shows. category is {id, slug, name, colors}; colors is the lineage, root first, the web’s category circle.

A post is {id, post_number, author, cooked, created_at, edited_at, deleted, bookmarked, reactions: [{key, emoji, count, reacted}], can_edit, moderated, hidden, held?, can_react, can_flag, flagged}.

Moderation, as the app sees it

  • posting on a category is {create_topic, reply}, each {kind, min_trust} with kind one of everyone (whoever can read the category), min_trust or staff. can_create_topic / can_reply already apply it, the account (verified, not silenced) and, for can_reply on a topic, the lock.
  • reactions on a category is {mode, keys}: all, none (off) or some with the offered keys. Posts in it list only those reactions (none when off), can_react is false when off, and PUT /posts/{id}/reaction answers 403 reactions_off or 403 reaction_not_offered. Existing reactions are kept while off and return when turned back on.
  • *_blocked is {code, message} or null: the same codes as the errors above, and the web’s sentence, to show instead of the hidden control.
  • A topic’s is_locked (= is_closed) and lock_reason: only staff reply, react or edit.
  • A post’s moderated is {by, at} or null. cooked is then the staff note, to show as “staff: …”; the original is never sent.
  • hidden: true means flagged past the threshold, or held by the lameness filter, and waiting for staff: cooked is empty unless you wrote it.
  • held is {reasons: [..]} on your own post when the lameness filter held it (“it repeats itself: it compresses to 0.54% of its length”), to show as “Held for review: …”; absent otherwise, and never sent to anyone else. Creating or editing a post answers 201/200 as usual, with hidden: true and held set when it was held. Edits are scored again.
  • flagged means you flagged it (can_flag is then false).
  • /me‘s user.silenced is a cooldown in force, {until, reason, message} (message: “Read-only until …: reason”), or null; user.read_only_until is the same end, flat. may_write is false while it lasts. A suspended account’s token is revoked and any request is 401.
  • Staff moderation (moderate, hide, lock, the flag queue, sanctions) is web-only.

Finding

Method Path Notes
GET /tags {tags: [{name, slug, count}]}, most used first
GET /tags/{slug} {tag, topics}
GET /search?q= {q, topics, too_short, min_chars}; titles, post bodies, tag names; 50 results. Under min_chars (3) characters nothing is searched: 200 with no topics and too_short: true
GET /bookmarks {bookmarks: [{post_id, topic_id, topic_title, post_number, cooked, deleted, created_at}]}
GET /notifications {unread, notifications: [{id, kind, topic_id, post_id, topic_title, actor, is_read, created_at}]}, the 50 newest
GET /notifications/unread {unread}
POST /notifications/{id}/read 204
POST /notifications/read 204; all of them

Not in the API

Everything under /admin (categories, reaction types, flags, users, the moderation log), staff moderation of posts and topics, invites, editing and deleting posts and topics, sign-up and password sign-in stay on the web or the manage CLI. There is no push (docs/android.md §2e).

Testing

tests/api_db.rs drives these routes through the real router against Postgres (just test-db; skipped without TEST_DATABASE_URL). just fixtures also writes the responses to android/app/src/test/resources/api/, which the app’s ContractTest decodes.