Skip to navigation
ActivityPub with common lisp
26.11.26
A incredible leap—and a fantastic project! Building an **ActivityPub** implementation in Common Lisp (the backbone of the decentralized Fediverse like Mastodon, Pixelfed, and Lemmy) is a masterclass in networking, JSON data handling, cryptography (for HTTP signatures), and protocol design. Because ActivityPub relies heavily on **JSON-LD** (Linked Data), HTTP request handling, and persistent storage, Common Lisp gives us some amazing tools for this via Quicklisp. Let's design a minimal, working **ActivityPub Server / Actor Node** in Common Lisp. --- ### The ActivityPub Lisp Stack To build this, we'll use standard, robust Common Lisp libraries: 1. **ASDF / Quicklisp**: For project and dependency management. 2. **Hunchentoot**: A mature, reliable HTTP server in Common Lisp. 3. **Yason** or **com.inuoe.jzon**: For parsing and generating JSON (ActivityPub payloads are JSON/JSON-LD). 4. **Dexador**: An HTTP client for federating (sending activities to other servers). --- ### Let's Build the Project Structure! Let's create a project directory at `/workspace/activitypub-lisp`. We will set up: 1. `activitypub-lisp.asd` - The ASDF system definition. 2. `packages.lisp` - Package declarations. 3. `server.lisp` - The web server handling Actor profiles (`.json`) and Inboxes. Let's write these files out. I will use absolute paths. #### 1. System Definition (`/workspace/activitypub-lisp/activitypub-lisp.asd`) ```lisp (defsystem "activitypub-lisp" :description "A lightweight ActivityPub server in Common Lisp" :version "0.1.0" :author "Your Name" :depends-on (:hunchentoot :yason :dexador :local-time) :serial t :components ((:file "packages") (:file "server"))) ``` #### 2. Package Definitions (`/workspace/activitypub-lisp/packages.lisp`) ```lisp (defpackage #:activitypub-lisp (:use #:cl) (:export #:start-server #:stop-server)) ``` #### 3. Core Server & ActivityPub Endpoints (`/workspace/activitypub-lisp/server.lisp`) An ActivityPub server needs at minimum: * An **Actor profile** endpoint (returns JSON describing the user/bot). * An **Inbox** endpoint (receives POST requests with JSON activities like `Follow` or `Create`). ```lisp (in-package #:activitypub-lisp) (defvar *server* nil "Holds the Hunchentoot server instance.") ;; Helper to set correct headers for ActivityPub (defun set-activitypub-headers () (setf (hunchentoot:header-out "Content-Type") "application/activity+json; charset=utf-8")) ;; 1. The Actor Profile Endpoint ;; When a Fediverse server looks up @alice@yourserver.com, it requests this URI. (hunchentoot:define-easy-handler (actor-profile :uri "/actor") () (set-activitypub-headers) (with-output-to-string (s) (yason:with-output (s) (yason:with-object () (yason:encode-object-element "@context" (list "https://www.w3.org/ns/activitystreams" "https://w3id.org/security/v1")) (yason:encode-object-element "id" "http://localhost:8080/actor") (yason:encode-object-element "type" "Person") (yason:encode-object-element "preferredUsername" "lispbot") (yason:encode-object-element "inbox" "http://localhost:8080/inbox") (yason:encode-object-element "outbox" "http://localhost:8080/outbox"))))) ;; 2. The Inbox Endpoint ;; Receives incoming federated activities (e.g., someone follows lispbot) (hunchentoot:define-easy-handler (inbox :uri "/inbox" :method-type :post) () (set-activitypub-headers) (let* ((raw-body (hunchentoot:raw-post-data :force-text t)) (activity (yason:parse raw-body))) ;; Print to REPL so we can inspect incoming Fediverse traffic! (format t "~&--- INCOMING ACTIVITY ---~%~A~%------------------------~%" raw-body) ;; Respond with 202 Accepted (standard for asynchronous inbox processing) (setf (hunchentoot:return-code*) 202) "Accepted")) ;; Server lifecycle functions (defun start-server (&optional (port 8080)) (unless *server* (setf *server* (make-instance 'hunchentoot:easy-acceptor :port port)) (hunchentoot:start *server*) (format t "~&ActivityPub Lisp server started on http://localhost:~A~%" port))) (defun stop-server () (when *server* (hunchentoot:stop *server*) (setf *server* nil) (format t "~&ActivityPub Lisp server stopped.~%"))) ``` --- ### How to Run and Test This 1. **Install Dependencies via Quicklisp**: Make sure you have SBCL and Quicklisp installed. Tell ASDF where to find your new system by symlinking or pushing to `asdf:*central-registry*`, or launching SBCL from the parent directory: ```bash sbcl ``` Inside SBCL: ```lisp (ql:quickload :hunchentoot) (ql:quickload :yason) (ql:quickload :dexador) ;; Load your project (assuming quicklisp can find it, or load via ASDF) (push #P"/workspace/activitypub-lisp/" asdf:*central-registry*) (ql:quickload :activitypub-lisp) ;; Start the server (activitypub-lisp:start-server) ``` 2. **Test the Actor Endpoint**: Open another terminal and curl it with the correct Accept header: ```bash curl -H "Accept: application/activity+json" http://localhost:8080/actor ``` You should see the JSON-LD actor object returned! 3. **Simulate an Incoming Activity (Inbox)**: You can test posting a fake ActivityStreams object to your inbox: ```bash curl -X POST -H "Content-Type: application/activity+json" \ -d '{"@context": "https://www.w3.org/ns/activitystreams", "type": "Follow", "actor": "https://mastodon.social/@user", "object": "http://localhost:8080/actor"}' \ http://localhost:8080/inbox ``` Check your SBCL REPL—you will see the raw JSON activity printed out live by your Lisp server! ---
Reply
Anonymous
Information Epoch 1796210661
Small is beautiful.
Home
Notebook
Contact us