Skip to content

About

A Mastodon-compatible ActivityPub server built for the edge — powered by Cloudflare Workers, D1, and the open web

Topics

Resources

Stars

8 stars

Watchers

0 watching

Forks

Latest commit

 

History

743 Commits

Folders and files

Repository files navigation

CF ActivityPub

License: MIT Version PRs Welcome

A Mastodon-compatible ActivityPub server built for the edge — powered by Cloudflare Workers, D1, and the open web.

Overview

CF ActivityPub is a fully functional social server implementing the ActivityPub protocol with Mastodon REST API compatibility. It runs entirely on Cloudflare Workers — no traditional servers, no Docker.

  • Zero cold starts — Cloudflare's V8 isolate model starts instantly in 300+ edge locations
  • Mastodon client compatible — works with Ivory, Elk, Tusky, Megalodon, and any Mastodon app
  • Federated — follows, boosts, likes and mentions across the fediverse
  • Cryptographically secure — HTTP Signatures via Web Crypto API
  • Web Push notifications — native push to mobile/desktop via VAPID + AES-128-GCM
  • AI-powered — automatic image alt-text via Workers AI (LLaVA)
  • AI moderation (Guardian) — a fully autonomous moderator keeps the instance safe from spam and toxicity, with a complete audit trail
  • Account expiration — inactive accounts are warned by email and removed automatically (configurable window, on by default at 12 months)
  • Fully open source — MIT licensed

Architecture

Layer Technology
Runtime Cloudflare Workers
Framework Next.js 16 App Router via vinext + @vinext/cloudflare (Vite)
Database Cloudflare D1 (SQLite)
Cache / Sessions Cloudflare KV
Media storage Cloudflare R2
Async delivery Cloudflare Queues
Realtime streaming Cloudflare Durable Objects (TimelineStreamDO)
WebRTC signaling Cloudflare Durable Objects (CallSignalingDO)
WebRTC ICE Cloudflare STUN + optional Cloudflare Calls TURN
AI inference Cloudflare Workers AI (LLaVA for media descriptions; clef-flash + clef decisions + BGE-M3 embeddings for moderation)
Semantic memory Cloudflare Vectorize (moderation abuse memory + RAG precedent)
Email Cloudflare Email Workers (via send_email binding)
Crypto Web Crypto API (RSASSA-PKCS1-v1_5 + PBKDF2 + ECDH + AES-128-GCM)
Styling Tailwind CSS v4

Environment variables

Secrets (wrangler secret put)

# Cloudflare Turnstile (bot protection for registration)
wrangler secret put TURNSTILE_SECRET

# Cloudflare Calls TURN (optional — for WebRTC relay behind symmetric NAT)
wrangler secret put CALLS_TURN_KEY_ID
wrangler secret put CALLS_API_TOKEN

# Web Push VAPID private key (generate with the script below)
wrangler secret put VAPID_PRIVATE_KEY

# Admin API token (optional — if set, admin endpoints require a Bearer token)
wrangler secret put ADMIN_TOKEN

VAPID key generation

Web Push notifications require a VAPID key pair. Generate one with:

node scripts/generate-vapid-keys.mjs

This outputs VAPID_PUBLIC_KEY (safe to put in wrangler.toml under [vars]) and VAPID_PRIVATE_KEY (must be set as a secret). Set VAPID_EMAIL to a contact address like mailto:admin@yourdomain.com.

Admin token generation

The Guardian admin API is protected by a shared secret when ADMIN_TOKEN is set. Generate a strong random token with:

openssl rand -hex 64

Then set it as a Cloudflare secret:

wrangler secret put ADMIN_TOKEN

For local development, put it in .env instead (or .dev.vars, which wrangler also reads):

ADMIN_TOKEN=your-generated-token

When configured, every request to /api/v1/admin/* must send Authorization: Bearer <token>. Without it, admin routes stay open (default behaviour).

Vectorize index (optional)

The AI moderation ("Guardian") gets a semantic memory of confirmed abuse via Cloudflare Vectorize: near-duplicates of known spam are blocked without a fresh AI call, and similar past cases are shown to the reasoning model as precedent.

Create the index (dimensions must match the @cf/baai/bge-m3 embedding model):

npx wrangler vectorize create moderation-vectors --dimensions=1024 --metric=cosine

Then uncomment the [[vectorize]] block at the top of wrangler.toml. Without a binding, moderation keeps working unchanged — the vector memory simply stays off.

Plain-text vars (documented in .env.example)

Every variable is documented with its default in .env.example: copy it to .env (gitignored) to change anything — .env values are merged into the deployed worker config at build time (wrangler.toml holds bindings and resource IDs only), and npm run check:env keeps the list complete. The table below is just the handful needed during the deploy steps:

Variable Description
INSTANCE_URL Your public domain (e.g. https://social.example.com)
INSTANCE_TITLE Instance display name
INSTANCE_DESCRIPTION Short instance description
INSTANCE_VERSION Version string
VAPID_PUBLIC_KEY Web Push VAPID public key (from generate-vapid-keys.mjs)
VAPID_EMAIL Contact email for VAPID (mailto:...)
TURNSTILE_SITE_KEY Cloudflare Turnstile public site key
FROM_EMAIL Sender address for transactional emails (must belong to a domain with Cloudflare Email Routing)
LIBRETRANSLATE_URL LibreTranslate instance URL (set empty to disable translation)
NODE_ENV production

Deploy

Prerequisites

1. Clone and install

git clone https://github.com/manalejandro/cf-activitypub-next.git
cd cf-activitypub-next
npm install

2. Create Cloudflare resources

wrangler login
wrangler d1 create cf-activitypub
wrangler kv namespace create CF_ACTIVITYPUB_KV
wrangler r2 bucket create cf-activitypub-media
wrangler queues create cf-activitypub-delivery
wrangler queues create cf-activitypub-delivery-dlq

The delivery queue (cf-activitypub-delivery) fans out ActivityPub activities to remote inboxes. Its consumer retries transient failures (max_retries = 16, exponential backoff up to 24h, Retry-After honoured); messages that still fail after all retries are moved to the dead-letter queue cf-activitypub-delivery-dlq instead of being silently dropped. Inspect or replay them with:

wrangler queues info cf-activitypub-delivery-dlq

Copy the generated IDs into wrangler.toml:

  • database_id under [[d1_databases]]
  • id under [[kv_namespaces]]

3. Configure your domain

Edit wrangler.toml and set:

  • INSTANCE_URL — your public domain (e.g. https://social.example.com)
  • pattern under [[routes]] — your custom domain

4. Generate VAPID keys for Web Push

node scripts/generate-vapid-keys.mjs

Add VAPID_PUBLIC_KEY and VAPID_EMAIL to wrangler.toml [vars], then:

wrangler secret put VAPID_PRIVATE_KEY

5. Set remaining secrets

wrangler secret put TURNSTILE_SECRET

Optional — only needed if you want TURN relay for WebRTC calling:

wrangler secret put CALLS_TURN_KEY_ID
wrangler secret put CALLS_API_TOKEN

Optional — only needed if you want to protect the admin API:

# Generate the token first: openssl rand -hex 64
wrangler secret put ADMIN_TOKEN

6. Run database migrations

npm run db:migrate

On an instance that already has data, node scripts/upgrade-schema.mjs re-applies the schema and runs the idempotent migrations (new columns, indexes and backfills) instead:

node scripts/upgrade-schema.mjs

To reset the database:

wrangler d1 execute cf-activitypub --remote --file=lib/db/drop.sql
npm run db:migrate

7. Deploy

npm run deploy

npm run deploy builds and deploys. Copy .env.example to .env to override any variable from wrangler.toml's [vars] for the deployment (the build merges .env into the uploaded worker configuration); secrets still go through wrangler secret put.

Preview locally

npm run preview

Runs the Cloudflare Workers runtime locally via vite preview (Miniflare bindings from wrangler.toml, variables from .env).

Features

ActivityPub Federation

  • WebFinger actor discovery
  • Actor profiles, Inbox/Outbox, Followers/Following collections
  • Shared inbox for efficient fan-out
  • HTTP Signatures on all federated requests
  • Handles: Create, Follow, Accept, Reject, Undo, Like, Announce, Delete, Update, Move, Flag, Add/Remove
  • Quotes (FEP-044f authorizations), polls, events and geolocated statuses
  • Pinned statuses and featured hashtags (federated collections), relays (ActivityPub relay subscriptions)
  • FEP-7aa9 collections of accounts
  • RSS/Atom feeds per profile (/@user.rss, /@user.atom) and Markdown for agents (Accept: text/markdown)
  • NodeInfo support

Mastodon API

  • OAuth 2.0 (password + authorization code + PKCE + client_credentials + refresh tokens)
  • Account registration, profile management, follow/unfollow
  • Status create/delete, favourite, reblog, polls, quotes, bookmarks
  • Home and public timelines, hashtag timelines, lists, filtered notifications
  • Notifications (follow, mention, favourite, reblog, poll, update, moderation)
  • Media uploads (R2-backed) with automatic alt-text, plus PDF and plain-text documents (Mastodon's unknown attachment type, federated as an AP Document)
  • Blocks, domain blocks, follow requests, mutes, endorsements, private notes
  • Scheduled statuses, E2EE (MLS) messaging, WebRTC calls

Realtime

  • Streaming timelines via Durable Objects

Web Push Notifications

  • VAPID-authenticated push to all major push services (Apple, Google, Mozilla)
  • AES-128-GCM encrypted payloads
  • Subscription lifecycle management (auto-cleanup on 410/404)
  • Fires for: follow, favourite, reblog, mention, poll results, status edits

AI Image Descriptions

  • Automatic alt-text generation via Cloudflare Workers AI (LLaVA model)
  • Triggered on media upload when no description is provided

AI Moderation (Guardian)

Fully autonomous moderation — there is no human admin, the AI runs the instance's safety. The instance is bilingual (English + Spanish); prompts and notification emails are language-aware.

  • Report auto-resolution — incoming Mastodon reports are evaluated and resolved (dismiss / warn / delete / suspend), with the reporter notified of the outcome
  • Pre-publish screening — every new status is filtered by clef-flash; flagged content is escalated to the clef reasoning model (allow / mark_sensitive / delete / escalate)
  • Registration screening — new sign-ups are reviewed for abuse before approval
  • Account patrol — a scheduled cycle scans recent statuses, suspicious accounts, duplicate spam and spam domains
  • Deterministic heuristics — rule-based signals (link-only posts, caps/emoji abuse, scam keywords in English and Spanish, posting floods, mass-following) feed every AI decision, so spam is caught even if the LLM is unavailable
  • Vector memory (Cloudflare Vectorize) — confirmed abuse is embedded and stored; near-duplicates of known spam are blocked immediately without a fresh AI call, and similar past cases are injected into the decision prompt as RAG precedent
  • Complete audit trail — every decision is written to a moderation_log table with the action, reason and confidence
  • Action engine — warned / deleted / suspended / rejected accounts are notified by email, and suspensions purge the account's content
  • Admin API — read the moderation log via GET /api/v1/admin/moderation_log; optionally protected by ADMIN_TOKEN (Bearer)
  • Guardian agent — an admin chat (/admin/guardian, Cloudflare Agents SDK) that answers with the instance's real state through tools and proposes moderation actions (suspend, silence, warn, delete, re-run a report) that only run once an administrator approves them

WebRTC Calling

  • Voice and video calls between users on the same instance or across federated instances
  • Per-call CallSignalingDO Durable Object relays SDP offer/answer and ICE candidates
  • Cross-instance signaling via ActivityPub (CallOffer, CallAnswer, CallIceCandidate, CallHangup)
  • Incoming call overlay with accept/decline, active call panel with mute/camera/hangup controls
  • ICE server configuration: Cloudflare STUN (stun:stun.cloudflare.com:3478) by default; optional TURN via Cloudflare Calls API

Optional TURN (Cloudflare Calls)

To enable TURN relay for users behind symmetric NAT:

wrangler secret put CALLS_TURN_KEY_ID
wrangler secret put CALLS_API_TOKEN

Credentials at dash.cloudflare.com → Realtime → Calls → TURN.

Frontend caching

  • HTML shells are cached in the Workers Cache API: 60 s fresh and up to 10 min served stale while revalidating in the background (PAGE_CACHE_TTL / PAGE_CACHE_SWR, 0 disables). Nothing personalised is cached — RSC payloads, /api/*, authenticated requests and Set-Cookie responses always render fresh — and content-hashed static assets keep Cloudflare's immutable one-year caching

Instance status

  • Public status page at /status (linked from the landing footer): component states (web, database, federation, delivery, media cache, streaming, maintenance), public counters and an automatic refresh every minute, served by GET /api/v1/status
  • Admin statistics at /admin/stats: the same status plus database size and row counts, storage retention, the federation registry, delivery failures, the media cache with its effective limits, moderation counters and which optional features are configured
  • Nothing is invented: every figure is something the instance measures itself, and a component without data says "unknown" instead of a made-up green

Scheduled tasks

  • Cron trigger runs every minute so scheduled posts go out on time; background maintenance is spread over 2–30 minute intervals (per-stage cadence, one KV read per tick)
  • Includes the Guardian patrol cycle (spam/abuse sweeps with AI review)
  • Media cache maintenance (R2 retention, eviction, orphan cleanup), link-preview crawling and instance-registry refreshes
  • Opt-in federated-history retention: delete remote public/unlisted statuses older than N days and/or keep the database under a byte budget (DB_RETENTION_*)

Disclaimer

The author is not responsible for the use of this instance, nor for any charges Cloudflare may apply for its use.

License

MIT

About

A Mastodon-compatible ActivityPub server built for the edge — powered by Cloudflare Workers, D1, and the open web

Topics

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages