Scratch space for learning atproto app development

Merge pull request #9 from bluesky-social/paul/tutorial-v1

Tutorial first draft

+644
TUTORIAL.md
···
+
# Tutorial
+
+
In this guide, we're going to build a simple multi-user app that publishes your current "status" as an emoji.
+
+
![A screenshot of our example application](./docs/app-screenshot.png)
+
+
At various points we will cover how to:
+
+
- Signin via OAuth
+
- Fetch information about users (profiles)
+
- Listen to the network firehose for new data
+
- Publish data on the user's account using a custom schema
+
+
We're going to keep this light so you can quickly wrap your head around ATProto. There will be links with more information about each step.
+
+
## Where are we going?
+
+
Data in the Atmosphere is stored on users' personal repos. It's almost like each user has their own website. Our goal is to aggregate data from the users into our SQLite DB.
+
+
Think of our app like a Google. If Google's job was to say which emoji each website had under `/status.json`, then it would show something like:
+
+
- `nytimes.com` is feeling 📰 according to `https://nytimes.com/status.json`
+
- `bsky.app` is feeling 🦋 according to `https://bsky.app/status.json`
+
- `reddit.com` is feeling 🤓 according to `https://reddit.com/status.json`
+
+
The Atmosphere works the same way, except we're going to check `at://` instead of `https://`. Each user has a data repo under an `at://` URL. We'll crawl all the `at://`s in the Atmosphere for all the "status.json" records and aggregate them into our SQLite database.
+
+
> `at://` is the URL scheme of the AT Protocol. Under the hood it uses common tech like HTTP and DNS, but it adds all of the features we'll be using in this tutorial.
+
+
## Step 1. Starting with our ExpressJS app
+
+
Start by cloning the repo and installing packages.
+
+
```bash
+
git clone TODO
+
cd TODO
+
npm i
+
npm run dev # you can leave this running and it will auto-reload
+
```
+
+
Our repo is a regular Web app. We're rendering our HTML server-side like it's 1999. We also have a SQLite database that we're managing with [Kysley](https://kysely.dev/).
+
+
Our starting stack:
+
+
- Typescript
+
- NodeJS web server ([express](https://expressjs.com/))
+
- SQLite database ([Kysley](https://kysely.dev/))
+
- Server-side rendering ([uhtml](https://www.npmjs.com/package/uhtml))
+
+
With each step we'll explain how our Web app taps into the Atmosphere. Refer to the codebase for more detailed code — again, this tutorial is going to keep it light and quick to digest.
+
+
## Step 2. Signing in with OAuth
+
+
When somebody logs into our app, they'll give us read & write access to their personal `at://` repo. We'll use that to write the `status.json` record.
+
+
We're going to accomplish this using OAuth ([spec](https://github.com/bluesky-social/proposals/tree/main/0004-oauth)). Most of the OAuth flows are going to be handled for us using the [@atproto/oauth-client-node](https://github.com/bluesky-social/atproto/tree/main/packages/oauth/oauth-client-node) library. This is the arrangement we're aiming toward:
+
+
![A diagram of the OAuth elements](./docs/diagram-oauth.png)
+
+
When the user logs in, the OAuth client will create a new session with their repo server and give us read/write access along with basic user info.
+
+
![A screenshot of the login UI](./docs/app-login.png)
+
+
Our login page just asks the user for their "handle," which is the domain name associated with their account. For [Bluesky](https://bsky.app) users, these tend to look like `alice.bsky.social`, but they can be any kind of domain (eg `alice.com`).
+
+
```html
+
<!-- src/pages/login.ts -->
+
<form action="/login" method="post" class="login-form">
+
<input
+
type="text"
+
name="handle"
+
placeholder="Enter your handle (eg alice.bsky.social)"
+
required
+
/>
+
<button type="submit">Log in</button>
+
</form>
+
```
+
+
When they submit the form, we tell our OAuth client to initiate the authorization flow and then redirect the user to their server to complete the process.
+
+
```typescript
+
/** src/routes.ts **/
+
// Login handler
+
router.post(
+
'/login',
+
handler(async (req, res) => {
+
// Initiate the OAuth flow
+
const url = await oauthClient.authorize(handle)
+
return res.redirect(url.toString())
+
})
+
)
+
```
+
+
This is the same kind of SSO flow that Google or GitHub uses. The user will be asked for their password, then asked to confirm the session with your application.
+
+
When that finishes, they'll be sent back to `/oauth/callback` on our Web app. The OAuth client stores the access tokens for the server, and then we attach their account's [DID](https://atproto.com/specs/did) to their cookie-session.
+
+
```typescript
+
/** src/routes.ts **/
+
// OAuth callback to complete session creation
+
router.get(
+
'/oauth/callback',
+
handler(async (req, res) => {
+
// Store the credentials
+
const { agent } = await oauthClient.callback(params)
+
+
// Attach the account DID to our user via a cookie
+
const session = await getIronSession(req, res)
+
session.did = agent.accountDid
+
await session.save()
+
+
// Send them back to the app
+
return res.redirect('/')
+
})
+
)
+
```
+
+
With that, we're in business! We now have a session with the user's `at://` repo server and can use that to access their data.
+
+
## Step 3. Fetching the user's profile
+
+
Why don't we learn something about our user? In [Bluesky](https://bsky.app), users publish a "profile" record which looks like this:
+
+
```typescript
+
interface ProfileRecord {
+
displayName?: string // a human friendly name
+
description?: string // a short bio
+
avatar?: BlobRef // small profile picture
+
banner?: BlobRef // banner image to put on profiles
+
createdAt?: string // declared time this profile data was added
+
// ...
+
}
+
```
+
+
You can examine this record directly using [atproto-browser.vercel.app](https://atproto-browser.vercel.app). For instance, [this is the profile record for @bsky.app](https://atproto-browser.vercel.app/at?u=at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.actor.profile/self).
+
+
We're going to use the [Agent](https://github.com/bluesky-social/atproto/tree/main/packages/api) associated with the user's OAuth session to fetch this record.
+
+
```typescript
+
await agent.getRecord({
+
repo: agent.accountDid, // The user
+
collection: 'app.bsky.actor.profile', // The collection
+
rkey: 'self', // The record key
+
})
+
```
+
+
When asking for a record, we provide three pieces of information.
+
+
- **repo** The [DID](https://atproto.com/specs/did) which identifies the user,
+
- **collection** The collection name, and
+
- **rkey** The record key
+
+
We'll explain the collection name shortly. Record keys are strings with [some restrictions](https://atproto.com/specs/record-key#record-key-syntax) and a couple of common patterns. The `"self"` pattern is used when a collection is expected to only contain one record which describes the user.
+
+
Let's update our homepage to fetch this profile record:
+
+
```typescript
+
/** src/routes.ts **/
+
// Homepage
+
router.get(
+
'/',
+
handler(async (req, res) => {
+
// If the user is signed in, get an agent which communicates with their server
+
const agent = await getSessionAgent(req, res, ctx)
+
+
if (!agent) {
+
// Serve the logged-out view
+
return res.type('html').send(page(home()))
+
}
+
+
// Fetch additional information about the logged-in user
+
const { data: profileRecord } = await agent.getRecord({
+
repo: agent.accountDid, // our user's repo
+
collection: 'app.bsky.actor.profile', // the bluesky profile record type
+
rkey: 'self', // the record's key
+
})
+
+
// Serve the logged-in view
+
return res
+
.type('html')
+
.send(page(home({ profile: profileRecord.value || {} })))
+
})
+
)
+
```
+
+
With that data, we can give a nice personalized welcome banner for our user:
+
+
![A screenshot of the banner image](./docs/app-banner.png)
+
+
```html
+
<!-- pages/home.ts -->
+
<div class="card">
+
${profile
+
? html`<form action="/logout" method="post" class="session-form">
+
<div>
+
Hi, <strong>${profile.displayName || 'friend'}</strong>.
+
What's your status today?
+
</div>
+
<div>
+
<button type="submit">Log out</button>
+
</div>
+
</form>`
+
: html`<div class="session-form">
+
<div><a href="/login">Log in</a> to set your status!</div>
+
<div>
+
<a href="/login" class="button">Log in</a>
+
</div>
+
</div>`}
+
</div>
+
```
+
+
## Step 4. Reading & writing records
+
+
You can think of the user repositories as collections of JSON records:
+
+
![A diagram of a repository](./docs/diagram-repo.png)
+
+
Let's look again at how we read the "profile" record:
+
+
```typescript
+
await agent.getRecord({
+
repo: agent.accountDid, // The user
+
collection: 'app.bsky.actor.profile', // The collection
+
rkey: 'self', // The record key
+
})
+
```
+
+
We write records using a similar API. Since our goal is to write "status" records, let's look at how that will happen:
+
+
```typescript
+
// Generate a time-based key for our record
+
const rkey = TID.nextStr()
+
+
// Write the
+
await agent.putRecord({
+
repo: agent.accountDid, // The user
+
collection: 'com.example.status', // The collection
+
rkey, // The record key
+
record: { // The record value
+
status: "👍",
+
createdAt: new Date().toISOString()
+
}
+
})
+
```
+
+
Our `POST /status` route is going to use this API to publish the user's status to their repo.
+
+
```typescript
+
/** src/routes.ts **/
+
// "Set status" handler
+
router.post(
+
'/status',
+
handler(async (req, res) => {
+
// If the user is signed in, get an agent which communicates with their server
+
const agent = await getSessionAgent(req, res, ctx)
+
if (!agent) {
+
return res.status(401).type('html').send('<h1>Error: Session required</h1>')
+
}
+
+
// Construct their status record
+
const record = {
+
$type: 'com.example.status',
+
status: req.body?.status,
+
createdAt: new Date().toISOString(),
+
}
+
+
try {
+
// Write the status record to the user's repository
+
await agent.putRecord({
+
repo: agent.accountDid,
+
collection: 'com.example.status',
+
rkey: TID.nextStr(),
+
record,
+
})
+
} catch (err) {
+
logger.warn({ err }, 'failed to write record')
+
return res.status(500).type('html').send('<h1>Error: Failed to write record</h1>')
+
}
+
+
res.status(200).json({})
+
})
+
)
+
```
+
+
Now in our homepage we can list out the status buttons:
+
+
```html
+
<!-- src/pages/home.ts -->
+
<form action="/status" method="post" class="status-options">
+
${STATUS_OPTIONS.map(status => html`
+
<button class="status-option" name="status" value="${status}">
+
${status}
+
</button>
+
`)}
+
</form>
+
```
+
+
And here we are!
+
+
![A screenshot of the app's status options](./docs/app-status-options.png)
+
+
## Step 5. Creating a custom "status" schema
+
+
Repo collections are typed, meaning that they have a defined schema. The `app.bsky.actor.profile` type definition [can be found here](https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/profile.json).
+
+
Anybody can create a new schema using the [Lexicon](https://atproto.com/specs/lexicon) language, which is very similar to [JSON-Schema](http://json-schema.org/). The schemas use [reverse-DNS IDs](https://atproto.com/specs/nsid) which indicate ownership, but for this demo app we're going to use `com.example` which is safe for non-production software.
+
+
> ### Why create a schema?
+
>
+
> Schemas help other applications understand the data your app is creating. By publishing your schemas, you make it easier for other application authors to publish data in a format your app will recognize and handle.
+
+
Let's create our schema in the `/lexicons` folder of our codebase. You can [read more about how to define schemas here](https://atproto.com/guides/lexicon).
+
+
```json
+
/** lexicons/status.json **/
+
{
+
"lexicon": 1,
+
"id": "com.example.status",
+
"defs": {
+
"main": {
+
"type": "record",
+
"key": "tid",
+
"record": {
+
"type": "object",
+
"required": ["status", "createdAt"],
+
"properties": {
+
"status": {
+
"type": "string",
+
"minLength": 1,
+
"maxGraphemes": 1,
+
"maxLength": 32
+
},
+
"createdAt": {
+
"type": "string",
+
"format": "datetime"
+
}
+
}
+
}
+
}
+
}
+
}
+
```
+
+
Now let's run some code-generation using our schema:
+
+
```bash
+
./node_modules/.bin/lex gen-server ./src/lexicon ./lexicons/*
+
```
+
+
This will produce Typescript interfaces as well as runtime validation functions that we can use in our app. Here's what that generated code looks like:
+
+
```typescript
+
/** src/lexicon/types/com/example/status.ts **/
+
export interface Record {
+
status: string
+
createdAt: string
+
[k: string]: unknown
+
}
+
+
export function isRecord(v: unknown): v is Record {
+
return (
+
isObj(v) &&
+
hasProp(v, '$type') &&
+
(v.$type === 'com.example.status#main' || v.$type === 'com.example.status')
+
)
+
}
+
+
export function validateRecord(v: unknown): ValidationResult {
+
return lexicons.validate('com.example.status#main', v)
+
}
+
```
+
+
Let's use that code to improve the `POST /status` route:
+
+
```typescript
+
/** src/routes.ts **/
+
import * as Status from '#/lexicon/types/com/example/status'
+
// ...
+
// "Set status" handler
+
router.post(
+
'/status',
+
handler(async (req, res) => {
+
// ...
+
+
// Construct & validate their status record
+
const record = {
+
$type: 'com.example.status',
+
status: req.body?.status,
+
createdAt: new Date().toISOString(),
+
}
+
if (!Status.validateRecord(record).success) {
+
return res.status(400).json({ error: 'Invalid status' })
+
}
+
+
// ...
+
})
+
)
+
```
+
+
## Step 6. Listening to the firehose
+
+
So far, we have:
+
+
- Logged in via OAuth
+
- Created a custom schema
+
- Read & written records for the logged in user
+
+
Now we want to fetch the status records from other users.
+
+
Remember how we referred to our app as being like a Google, crawling around the repos to get their records? One advantage we have in the AT Protocol is that each repo publishes an event log of their updates.
+
+
![A diagram of the event stream](./docs/diagram-event-stream.png)
+
+
Using a [Relay service](https://docs.bsky.app/docs/advanced-guides/federation-architecture#relay) we can listen to an aggregated firehose of these events across all users in the network. In our case what we're looking for are valid `com.example.status` records.
+
+
+
```typescript
+
/** src/firehose.ts **/
+
import * as Status from '#/lexicon/types/com/example/status'
+
// ...
+
const firehose = new Firehose({})
+
+
for await (const evt of firehose.run()) {
+
// Watch for write events
+
if (evt.event === 'create' || evt.event === 'update') {
+
const record = evt.record
+
+
// If the write is a valid status update
+
if (
+
evt.collection === 'com.example.status' &&
+
Status.isRecord(record) &&
+
Status.validateRecord(record).success
+
) {
+
// Store the status
+
// TODO
+
}
+
}
+
}
+
```
+
+
Let's create a SQLite table to store these statuses:
+
+
```typescript
+
/** src/db.ts **/
+
// Create our statuses table
+
await db.schema
+
.createTable('status')
+
.addColumn('uri', 'varchar', (col) => col.primaryKey())
+
.addColumn('authorDid', 'varchar', (col) => col.notNull())
+
.addColumn('status', 'varchar', (col) => col.notNull())
+
.addColumn('createdAt', 'varchar', (col) => col.notNull())
+
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
+
.execute()
+
```
+
+
Now we can write these statuses into our database as they arrive from the firehose:
+
+
```typescript
+
/** src/firehose.ts **/
+
// If the write is a valid status update
+
if (
+
evt.collection === 'com.example.status' &&
+
Status.isRecord(record) &&
+
Status.validateRecord(record).success
+
) {
+
// Store the status in our SQLite
+
await db
+
.insertInto('status')
+
.values({
+
uri: evt.uri.toString(),
+
authorDid: evt.author,
+
status: record.status,
+
createdAt: record.createdAt,
+
indexedAt: new Date().toISOString(),
+
})
+
.onConflict((oc) =>
+
oc.column('uri').doUpdateSet({
+
status: record.status,
+
indexedAt: new Date().toISOString(),
+
})
+
)
+
.execute()
+
}
+
```
+
+
You can almost think of information flowing in a loop:
+
+
![A diagram of the flow of information](./docs/diagram-info-flow.png)
+
+
Applications write to the repo. The write events are then emitted on the firehose where they're caught by the apps and ingested into their databases.
+
+
Why sync from the event log like this? Because there are other apps in the network that will write the records we're interested in. By subscribing to the event log, we ensure that we catch all the data we're interested in &mdash; including data published by other apps!
+
+
## Step 7. Listing the latest statuses
+
+
Now that we have statuses populating our SQLite, we can produce a timeline of status updates by users. We also use a [DID](https://atproto.com/specs/did)-to-handle resolver so we can show a nice username with the statuses:
+
+
```typescript
+
/** src/routes.ts **/
+
// Homepage
+
router.get(
+
'/',
+
handler(async (req, res) => {
+
// ...
+
+
// Fetch data stored in our SQLite
+
const statuses = await db
+
.selectFrom('status')
+
.selectAll()
+
.orderBy('indexedAt', 'desc')
+
.limit(10)
+
.execute()
+
+
// Map user DIDs to their domain-name handles
+
const didHandleMap = await resolver.resolveDidsToHandles(
+
statuses.map((s) => s.authorDid)
+
)
+
+
// ...
+
})
+
)
+
```
+
+
Our HTML can now list these status records:
+
+
```html
+
<!-- src/pages/home.ts -->
+
${statuses.map((status, i) => {
+
const handle = didHandleMap[status.authorDid] || status.authorDid
+
return html`
+
<div class="status-line">
+
<div>
+
<div class="status">${status.status}</div>
+
</div>
+
<div class="desc">
+
<a class="author" href="https://bsky.app/profile/${handle}">@${handle}</a>
+
was feeling ${status.status} on ${status.indexedAt}.
+
</div>
+
</div>
+
`
+
})}
+
```
+
+
![A screenshot of the app status timeline](./docs/app-status-history.png)
+
+
## Step 8. Optimistic updates
+
+
As a final optimization, let's introduce "optimistic updates."
+
+
Remember the information flow loop with the repo write and the event log?
+
+
![A diagram of the flow of information](./docs/diagram-info-flow.png)
+
+
Since we're updating our users' repos locally, we can short-circuit that flow to our own database:
+
+
![A diagram illustrating optimistic updates](./docs/diagram-optimistic-update.png)
+
+
This is an important optimization to make, because it ensures that the user sees their own changes while using your app. When the event eventually arrives from the firehose, we just discard it since we already have it saved locally.
+
+
To do this, we just update `POST /status` to include an additional write to our SQLite DB:
+
+
```typescript
+
/** src/routes.ts **/
+
// "Set status" handler
+
router.post(
+
'/status',
+
handler(async (req, res) => {
+
// ...
+
+
let uri
+
try {
+
// Write the status record to the user's repository
+
const res = await agent.putRecord({
+
repo: agent.accountDid,
+
collection: 'com.example.status',
+
rkey: TID.nextStr(),
+
record,
+
})
+
uri = res.uri
+
} catch (err) {
+
logger.warn({ err }, 'failed to write record')
+
return res.status(500).json({ error: 'Failed to write record' })
+
}
+
+
try {
+
// Optimistically update our SQLite <-- HERE!
+
await db
+
.insertInto('status')
+
.values({
+
uri,
+
authorDid: agent.accountDid,
+
status: record.status,
+
createdAt: record.createdAt,
+
indexedAt: new Date().toISOString(),
+
})
+
.execute()
+
} catch (err) {
+
logger.warn(
+
{ err },
+
'failed to update computed view; ignoring as it should be caught by the firehose'
+
)
+
}
+
+
res.status(200).json({})
+
})
+
)
+
```
+
+
You'll notice this code looks almost exactly like what we're doing in `firehose.ts`.
+
+
## Thinking in AT Proto
+
+
In this tutorial we've covered the key steps to building an atproto app. Data is published in its canonical form on users' `at://` repos and then aggregated into apps' databases to produce views of the network.
+
+
When building your app, think in these four key steps:
+
+
- Design the [Lexicon](#) schemas for the records you'll publish into the Atmosphere.
+
- Create a database for aggregating the records into useful views.
+
- Build your application to write the records on your users' repos.
+
- Listen to the firehose to aggregate data across the network.
+
+
Remember this flow of information throughout:
+
+
![A diagram of the flow of information](./docs/diagram-info-flow.png)
+
+
This is how every app in the Atmosphere works, including the [Bluesky social app](https://bsky.app).
+
+
## Next steps
+
+
If you want to practice what you've learned, here are some additional challenges you could try:
+
+
- Sync the profile records of all users so that you can show their display names instead of their handles.
+
- Count the number of each status used and display the total counts.
+
- Fetch the authed user's `app.bsky.graph.follow` follows and show statuses from them.
+
- Create a different kind of schema, like a way to post links to websites and rate them 1 through 4 stars.
+
+
You can find more information here:
+
+
|Resources|-|
+
|-|-|
+
|[@ ATProto docs](https://atproto.com)|Learn more about the AT Protocol.|
+
|[🦋 Bluesky API docs](https://docs.bsky.app/)|See how Bluesky works as an ATProto app.|
+
|[📦 ATProto monorepo](https://github.com/bluesky-social/atproto)|See the source code first-hand.|
+
|[💬 ATProto discussions board](https://github.com/bluesky-social/atproto/discussions)|Ask any questions you have!|
docs/app-banner.png

This is a binary file and will not be displayed.

docs/app-login.png

This is a binary file and will not be displayed.

docs/app-screenshot.png

This is a binary file and will not be displayed.

docs/app-status-history.png

This is a binary file and will not be displayed.

docs/app-status-options.png

This is a binary file and will not be displayed.

docs/diagram-event-stream.png

This is a binary file and will not be displayed.

docs/diagram-info-flow.png

This is a binary file and will not be displayed.

docs/diagram-oauth.png

This is a binary file and will not be displayed.

docs/diagram-optimistic-update.png

This is a binary file and will not be displayed.

docs/diagram-repo.png

This is a binary file and will not be displayed.

+49
lexicons/profile.json
···
+
{
+
"lexicon": 1,
+
"id": "app.bsky.actor.profile",
+
"defs": {
+
"main": {
+
"type": "record",
+
"description": "A declaration of a Bluesky account profile.",
+
"key": "literal:self",
+
"record": {
+
"type": "object",
+
"properties": {
+
"displayName": {
+
"type": "string",
+
"maxGraphemes": 64,
+
"maxLength": 640
+
},
+
"description": {
+
"type": "string",
+
"description": "Free-form profile description text.",
+
"maxGraphemes": 256,
+
"maxLength": 2560
+
},
+
"avatar": {
+
"type": "blob",
+
"description": "Small image to be displayed next to posts from account. AKA, 'profile picture'",
+
"accept": ["image/png", "image/jpeg"],
+
"maxSize": 1000000
+
},
+
"banner": {
+
"type": "blob",
+
"description": "Larger horizontal image to display behind profile view.",
+
"accept": ["image/png", "image/jpeg"],
+
"maxSize": 1000000
+
},
+
"labels": {
+
"type": "union",
+
"description": "Self-label values, specific to the Bluesky application, on the overall account.",
+
"refs": ["com.atproto.label.defs#selfLabels"]
+
},
+
"joinedViaStarterPack": {
+
"type": "ref",
+
"ref": "com.atproto.repo.strongRef"
+
},
+
"createdAt": { "type": "string", "format": "datetime" }
+
}
+
}
+
}
+
}
+
}
+3 -3
lexicons/status.json
···
"defs": {
"main": {
"type": "record",
-
"key": "literal:self",
+
"key": "tid",
"record": {
"type": "object",
-
"required": ["status", "updatedAt"],
+
"required": ["status", "createdAt"],
"properties": {
"status": {
"type": "string",
···
"maxGraphemes": 1,
"maxLength": 32
},
-
"updatedAt": { "type": "string", "format": "datetime" }
+
"createdAt": { "type": "string", "format": "datetime" }
}
}
}
+2 -25
package-lock.json
···
"version": "0.0.1",
"license": "MIT",
"dependencies": {
+
"@atproto/common": "^0.4.1",
"@atproto/identity": "^0.4.0",
"@atproto/lexicon": "0.4.1-rc.0",
"@atproto/oauth-client-node": "0.0.2-rc.2",
···
"kysely": "^0.27.4",
"multiformats": "^9.9.0",
"pino": "^9.3.2",
-
"pino-http": "^10.0.0",
"uhtml": "^4.5.9"
},
"devDependencies": {
···
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/@atproto/common/-/common-0.4.1.tgz",
"integrity": "sha512-uL7kQIcBTbvkBDNfxMXL6lBH4fO2DQpHd2BryJxMtbw/4iEPKe9xBYApwECHhEIk9+zhhpTRZ15FJ3gxTXN82Q==",
+
"license": "MIT",
"dependencies": {
"@atproto/common-web": "^0.3.0",
"@ipld/dag-cbor": "^7.0.3",
···
"resolved": "https://registry.npmjs.org/gc-hook/-/gc-hook-0.3.1.tgz",
"integrity": "sha512-E5M+O/h2o7eZzGhzRZGex6hbB3k4NWqO0eA+OzLRLXxhdbYPajZnynPwAtphnh+cRHPwsj5Z80dqZlfI4eK55A=="
},
-
"node_modules/get-caller-file": {
-
"version": "2.0.5",
-
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
-
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
-
"engines": {
-
"node": "6.* || 8.* || >= 10.*"
-
}
-
},
"node_modules/get-intrinsic": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz",
···
"readable-stream": "^4.0.0",
"split2": "^4.0.0"
-
},
-
"node_modules/pino-http": {
-
"version": "10.2.0",
-
"resolved": "https://registry.npmjs.org/pino-http/-/pino-http-10.2.0.tgz",
-
"integrity": "sha512-am03BxnV3Ckx68OkbH0iZs3indsrH78wncQ6w1w51KroIbvJZNImBKX2X1wjdY8lSyaJ0UrX/dnO2DY3cTeCRw==",
-
"dependencies": {
-
"get-caller-file": "^2.0.5",
-
"pino": "^9.0.0",
-
"pino-std-serializers": "^7.0.0",
-
"process-warning": "^3.0.0"
-
}
-
},
-
"node_modules/pino-http/node_modules/process-warning": {
-
"version": "3.0.0",
-
"resolved": "https://registry.npmjs.org/process-warning/-/process-warning-3.0.0.tgz",
-
"integrity": "sha512-mqn0kFRl0EoqhnL0GQ0veqFHyIN1yig9RHh/InzORTUiZHFRAur+aMtRkELNwGs9aNwKS6tg/An4NYBPGwvtzQ=="
},
"node_modules/pino-pretty": {
"version": "11.2.2",
+1
package.json
···
"clean": "rimraf dist coverage"
},
"dependencies": {
+
"@atproto/common": "^0.4.1",
"@atproto/identity": "^0.4.0",
"@atproto/lexicon": "0.4.1-rc.0",
"@atproto/oauth-client-node": "0.0.2-rc.2",
-61
src/auth/session.ts
···
-
import assert from 'node:assert'
-
import type { IncomingMessage, ServerResponse } from 'node:http'
-
import { getIronSession } from 'iron-session'
-
import { env } from '#/lib/env'
-
import { AppContext } from '#/index'
-
-
export type Session = { did: string }
-
-
export async function createSession(
-
req: IncomingMessage,
-
res: ServerResponse<IncomingMessage>,
-
did: string
-
) {
-
const session = await getSessionRaw(req, res)
-
assert(!session.did, 'session already exists')
-
session.did = did
-
await session.save()
-
return { did: session.did }
-
}
-
-
export async function destroySession(
-
req: IncomingMessage,
-
res: ServerResponse<IncomingMessage>
-
) {
-
const session = await getSessionRaw(req, res)
-
await session.destroy()
-
return null
-
}
-
-
export async function getSession(
-
req: IncomingMessage,
-
res: ServerResponse<IncomingMessage>
-
) {
-
const session = await getSessionRaw(req, res)
-
if (!session.did) return null
-
return { did: session.did }
-
}
-
-
export async function getSessionAgent(
-
req: IncomingMessage,
-
res: ServerResponse<IncomingMessage>,
-
ctx: AppContext
-
) {
-
const session = await getSessionRaw(req, res)
-
if (!session.did) return null
-
return await ctx.oauthClient.restore(session.did).catch(async (err) => {
-
ctx.logger.warn({ err }, 'oauth restore failed')
-
await destroySession(req, res)
-
return null
-
})
-
}
-
-
async function getSessionRaw(
-
req: IncomingMessage,
-
res: ServerResponse<IncomingMessage>
-
) {
-
return await getIronSession<Session>(req, res, {
-
cookieName: 'sid',
-
password: env.COOKIE_SECRET,
-
})
-
}
+94
src/db.ts
···
+
import SqliteDb from 'better-sqlite3'
+
import {
+
Kysely,
+
Migrator,
+
SqliteDialect,
+
Migration,
+
MigrationProvider,
+
} from 'kysely'
+
+
// Types
+
+
export type DatabaseSchema = {
+
status: Status
+
auth_session: AuthSession
+
auth_state: AuthState
+
}
+
+
export type Status = {
+
uri: string
+
authorDid: string
+
status: string
+
createdAt: string
+
indexedAt: string
+
}
+
+
export type AuthSession = {
+
key: string
+
session: AuthSessionJson
+
}
+
+
export type AuthState = {
+
key: string
+
state: AuthStateJson
+
}
+
+
type AuthStateJson = string
+
+
type AuthSessionJson = string
+
+
// Migrations
+
+
const migrations: Record<string, Migration> = {}
+
+
const migrationProvider: MigrationProvider = {
+
async getMigrations() {
+
return migrations
+
},
+
}
+
+
migrations['001'] = {
+
async up(db: Kysely<unknown>) {
+
await db.schema
+
.createTable('status')
+
.addColumn('uri', 'varchar', (col) => col.primaryKey())
+
.addColumn('authorDid', 'varchar', (col) => col.notNull())
+
.addColumn('status', 'varchar', (col) => col.notNull())
+
.addColumn('createdAt', 'varchar', (col) => col.notNull())
+
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
+
.execute()
+
await db.schema
+
.createTable('auth_session')
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
+
.addColumn('session', 'varchar', (col) => col.notNull())
+
.execute()
+
await db.schema
+
.createTable('auth_state')
+
.addColumn('key', 'varchar', (col) => col.primaryKey())
+
.addColumn('state', 'varchar', (col) => col.notNull())
+
.execute()
+
},
+
async down(db: Kysely<unknown>) {
+
await db.schema.dropTable('auth_state').execute()
+
await db.schema.dropTable('auth_session').execute()
+
await db.schema.dropTable('status').execute()
+
},
+
}
+
+
// APIs
+
+
export const createDb = (location: string): Database => {
+
return new Kysely<DatabaseSchema>({
+
dialect: new SqliteDialect({
+
database: new SqliteDb(location),
+
}),
+
})
+
}
+
+
export const migrateToLatest = async (db: Database) => {
+
const migrator = new Migrator({ db, provider: migrationProvider })
+
const { error } = await migrator.migrateToLatest()
+
if (error) throw error
+
}
+
+
export type Database = Kysely<DatabaseSchema>
-20
src/db/index.ts
···
-
import SqliteDb from 'better-sqlite3'
-
import { Kysely, Migrator, SqliteDialect } from 'kysely'
-
import { migrationProvider } from './migrations'
-
import type { DatabaseSchema } from './schema'
-
-
export const createDb = (location: string): Database => {
-
return new Kysely<DatabaseSchema>({
-
dialect: new SqliteDialect({
-
database: new SqliteDb(location),
-
}),
-
})
-
}
-
-
export const migrateToLatest = async (db: Database) => {
-
const migrator = new Migrator({ db, provider: migrationProvider })
-
const { error } = await migrator.migrateToLatest()
-
if (error) throw error
-
}
-
-
export type Database = Kysely<DatabaseSchema>
-36
src/db/migrations.ts
···
-
import type { Kysely, Migration, MigrationProvider } from 'kysely'
-
-
const migrations: Record<string, Migration> = {}
-
-
export const migrationProvider: MigrationProvider = {
-
async getMigrations() {
-
return migrations
-
},
-
}
-
-
migrations['001'] = {
-
async up(db: Kysely<unknown>) {
-
await db.schema
-
.createTable('status')
-
.addColumn('authorDid', 'varchar', (col) => col.primaryKey())
-
.addColumn('status', 'varchar', (col) => col.notNull())
-
.addColumn('updatedAt', 'varchar', (col) => col.notNull())
-
.addColumn('indexedAt', 'varchar', (col) => col.notNull())
-
.execute()
-
await db.schema
-
.createTable('auth_session')
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
-
.addColumn('session', 'varchar', (col) => col.notNull())
-
.execute()
-
await db.schema
-
.createTable('auth_state')
-
.addColumn('key', 'varchar', (col) => col.primaryKey())
-
.addColumn('state', 'varchar', (col) => col.notNull())
-
.execute()
-
},
-
async down(db: Kysely<unknown>) {
-
await db.schema.dropTable('auth_state').execute()
-
await db.schema.dropTable('auth_session').execute()
-
await db.schema.dropTable('status').execute()
-
},
-
}
-26
src/db/schema.ts
···
-
export type DatabaseSchema = {
-
status: Status
-
auth_session: AuthSession
-
auth_state: AuthState
-
}
-
-
export type Status = {
-
authorDid: string
-
status: string
-
updatedAt: string
-
indexedAt: string
-
}
-
-
export type AuthSession = {
-
key: string
-
session: AuthSessionJson
-
}
-
-
export type AuthState = {
-
key: string
-
state: AuthStateJson
-
}
-
-
type AuthStateJson = string
-
-
type AuthSessionJson = string
+9 -3
src/firehose/ingester.ts
···
await this.db
.insertInto('status')
.values({
+
uri: evt.uri.toString(),
authorDid: evt.author,
status: record.status,
-
updatedAt: record.updatedAt,
+
createdAt: record.createdAt,
indexedAt: new Date().toISOString(),
})
.onConflict((oc) =>
-
oc.column('authorDid').doUpdateSet({
+
oc.column('uri').doUpdateSet({
status: record.status,
-
updatedAt: record.updatedAt,
indexedAt: new Date().toISOString(),
})
)
.execute()
}
+
} else if (
+
evt.event === 'delete' &&
+
evt.collection === 'com.example.status'
+
) {
+
// Remove the status from our SQLite
+
await this.db.deleteFrom('status').where({ uri: evt.uri.toString() })
}
}
}
+30
src/lexicon/index.ts
···
export class Server {
xrpc: XrpcServer
+
app: AppNS
com: ComNS
constructor(options?: XrpcOptions) {
this.xrpc = createXrpcServer(schemas, options)
+
this.app = new AppNS(this)
this.com = new ComNS(this)
+
}
+
}
+
+
export class AppNS {
+
_server: Server
+
bsky: AppBskyNS
+
+
constructor(server: Server) {
+
this._server = server
+
this.bsky = new AppBskyNS(server)
+
}
+
}
+
+
export class AppBskyNS {
+
_server: Server
+
actor: AppBskyActorNS
+
+
constructor(server: Server) {
+
this._server = server
+
this.actor = new AppBskyActorNS(server)
+
}
+
}
+
+
export class AppBskyActorNS {
+
_server: Server
+
+
constructor(server: Server) {
+
this._server = server
}
}
+61 -3
src/lexicon/lexicons.ts
···
import { LexiconDoc, Lexicons } from '@atproto/lexicon'
export const schemaDict = {
+
AppBskyActorProfile: {
+
lexicon: 1,
+
id: 'app.bsky.actor.profile',
+
defs: {
+
main: {
+
type: 'record',
+
description: 'A declaration of a Bluesky account profile.',
+
key: 'literal:self',
+
record: {
+
type: 'object',
+
properties: {
+
displayName: {
+
type: 'string',
+
maxGraphemes: 64,
+
maxLength: 640,
+
},
+
description: {
+
type: 'string',
+
description: 'Free-form profile description text.',
+
maxGraphemes: 256,
+
maxLength: 2560,
+
},
+
avatar: {
+
type: 'blob',
+
description:
+
"Small image to be displayed next to posts from account. AKA, 'profile picture'",
+
accept: ['image/png', 'image/jpeg'],
+
maxSize: 1000000,
+
},
+
banner: {
+
type: 'blob',
+
description:
+
'Larger horizontal image to display behind profile view.',
+
accept: ['image/png', 'image/jpeg'],
+
maxSize: 1000000,
+
},
+
labels: {
+
type: 'union',
+
description:
+
'Self-label values, specific to the Bluesky application, on the overall account.',
+
refs: ['lex:com.atproto.label.defs#selfLabels'],
+
},
+
joinedViaStarterPack: {
+
type: 'ref',
+
ref: 'lex:com.atproto.repo.strongRef',
+
},
+
createdAt: {
+
type: 'string',
+
format: 'datetime',
+
},
+
},
+
},
+
},
+
},
+
},
ComExampleStatus: {
lexicon: 1,
id: 'com.example.status',
···
key: 'literal:self',
record: {
type: 'object',
-
required: ['status', 'updatedAt'],
+
required: ['status', 'createdAt'],
properties: {
status: {
type: 'string',
···
maxGraphemes: 1,
maxLength: 32,
},
-
updatedAt: {
+
createdAt: {
type: 'string',
format: 'datetime',
},
···
}
export const schemas: LexiconDoc[] = Object.values(schemaDict) as LexiconDoc[]
export const lexicons: Lexicons = new Lexicons(schemas)
-
export const ids = { ComExampleStatus: 'com.example.status' }
+
export const ids = {
+
AppBskyActorProfile: 'app.bsky.actor.profile',
+
ComExampleStatus: 'com.example.status',
+
}
+38
src/lexicon/types/app/bsky/actor/profile.ts
···
+
/**
+
* GENERATED CODE - DO NOT MODIFY
+
*/
+
import { ValidationResult, BlobRef } from '@atproto/lexicon'
+
import { lexicons } from '../../../../lexicons'
+
import { isObj, hasProp } from '../../../../util'
+
import { CID } from 'multiformats/cid'
+
import * as ComAtprotoLabelDefs from '../../../com/atproto/label/defs'
+
import * as ComAtprotoRepoStrongRef from '../../../com/atproto/repo/strongRef'
+
+
export interface Record {
+
displayName?: string
+
/** Free-form profile description text. */
+
description?: string
+
/** Small image to be displayed next to posts from account. AKA, 'profile picture' */
+
avatar?: BlobRef
+
/** Larger horizontal image to display behind profile view. */
+
banner?: BlobRef
+
labels?:
+
| ComAtprotoLabelDefs.SelfLabels
+
| { $type: string; [k: string]: unknown }
+
joinedViaStarterPack?: ComAtprotoRepoStrongRef.Main
+
createdAt?: string
+
[k: string]: unknown
+
}
+
+
export function isRecord(v: unknown): v is Record {
+
return (
+
isObj(v) &&
+
hasProp(v, '$type') &&
+
(v.$type === 'app.bsky.actor.profile#main' ||
+
v.$type === 'app.bsky.actor.profile')
+
)
+
}
+
+
export function validateRecord(v: unknown): ValidationResult {
+
return lexicons.validate('app.bsky.actor.profile#main', v)
+
}
+1 -1
src/lexicon/types/com/example/status.ts
···
export interface Record {
status: string
-
updatedAt: string
+
createdAt: string
[k: string]: unknown
}
+10 -11
src/pages/home.ts
···
-
import type { Status } from '#/db/schema'
+
import type { Status } from '#/db'
import { html } from '../lib/view'
import { shell } from './shell'
···
type Props = {
statuses: Status[]
didHandleMap: Record<string, string>
-
profile?: { displayName?: string; handle: string }
+
profile?: { displayName?: string }
myStatus?: Status
}
···
${profile
? html`<form action="/logout" method="post" class="session-form">
<div>
-
Hi, <strong>${profile.displayName || profile.handle}</strong>.
-
what's your status today?
+
Hi, <strong>${profile.displayName || 'friend'}</strong>. What's
+
your status today?
</div>
<div>
<button type="submit">Log out</button>
···
</div>
</div>`}
</div>
-
<div class="status-options">
+
<form action="/status" method="post" class="status-options">
${STATUS_OPTIONS.map(
(status) =>
-
html`<div
+
html`<button
class=${myStatus?.status === status
? 'status-option selected'
: 'status-option'}
-
data-value="${status}"
-
data-authed=${profile ? '1' : '0'}
+
name="status"
+
value="${status}"
>
${status}
-
</div>`
+
</button>`
)}
-
</div>
+
</form>
${statuses.map((status, i) => {
const handle = didHandleMap[status.authorDid] || status.authorDid
const date = ts(status)
···
`
})}
</div>
-
<script src="/public/home.js"></script>
</div>`
}
-32
src/pages/public/home.js
···
-
Array.from(document.querySelectorAll('.status-option'), (el) => {
-
el.addEventListener('click', async (ev) => {
-
setError('')
-
-
if (el.dataset.authed !== '1') {
-
window.location = '/login'
-
return
-
}
-
-
const res = await fetch('/status', {
-
method: 'POST',
-
headers: { 'content-type': 'application/json' },
-
body: JSON.stringify({ status: el.dataset.value }),
-
})
-
const body = await res.json()
-
if (body?.error) {
-
setError(body.error)
-
} else {
-
location.reload()
-
}
-
})
-
})
-
-
function setError(str) {
-
const errMsg = document.querySelector('.error')
-
if (str) {
-
errMsg.classList.add('visible')
-
errMsg.textContent = str
-
} else {
-
errMsg.classList.remove('visible')
-
}
-
}
+1
src/pages/public/styles.css
···
font-size: 2rem;
width: 3rem;
height: 3rem;
+
padding: 0;
background-color: #fff;
border: 1px solid var(--border-color);
border-radius: 3rem;
+81 -24
src/routes.ts
···
+
import assert from 'node:assert'
import path from 'node:path'
+
import type { IncomingMessage, ServerResponse } from 'node:http'
import { OAuthResolverError } from '@atproto/oauth-client-node'
import { isValidHandle } from '@atproto/syntax'
+
import { TID } from '@atproto/common'
import express from 'express'
-
import { createSession, destroySession, getSessionAgent } from '#/auth/session'
+
import { getIronSession } from 'iron-session'
import type { AppContext } from '#/index'
import { home } from '#/pages/home'
import { login } from '#/pages/login'
+
import { env } from '#/lib/env'
import { page } from '#/lib/view'
import * as Status from '#/lexicon/types/com/example/status'
+
import * as Profile from '#/lexicon/types/app/bsky/actor/profile'
+
+
type Session = { did: string }
// Helper function for defining routes
const handler =
···
}
}
+
// Helper function to get the Atproto Agent for the active session
+
async function getSessionAgent(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>,
+
ctx: AppContext
+
) {
+
const session = await getIronSession<Session>(req, res, {
+
cookieName: 'sid',
+
password: env.COOKIE_SECRET,
+
})
+
if (!session.did) return null
+
try {
+
return await ctx.oauthClient.restore(session.did)
+
} catch (err) {
+
ctx.logger.warn({ err }, 'oauth restore failed')
+
await session.destroy()
+
return null
+
}
+
}
+
export const createRouter = (ctx: AppContext) => {
const router = express.Router()
···
const params = new URLSearchParams(req.originalUrl.split('?')[1])
try {
const { agent } = await ctx.oauthClient.callback(params)
-
await createSession(req, res, agent.accountDid)
+
const session = await getIronSession<Session>(req, res, {
+
cookieName: 'sid',
+
password: env.COOKIE_SECRET,
+
})
+
assert(!session.did, 'session already exists')
+
session.did = agent.accountDid
+
await session.save()
} catch (err) {
ctx.logger.error({ err }, 'oauth callback failed')
return res.redirect('/?error')
···
router.post(
'/logout',
handler(async (req, res) => {
-
await destroySession(req, res)
+
const session = await getIronSession<Session>(req, res, {
+
cookieName: 'sid',
+
password: env.COOKIE_SECRET,
+
})
+
await session.destroy()
return res.redirect('/')
})
)
···
.selectFrom('status')
.selectAll()
.where('authorDid', '=', agent.accountDid)
+
.orderBy('indexedAt', 'desc')
.executeTakeFirst()
: undefined
···
}
// Fetch additional information about the logged-in user
-
const { data: profile } = await agent.getProfile({
-
actor: agent.accountDid,
+
const { data: profileRecord } = await agent.com.atproto.repo.getRecord({
+
repo: agent.accountDid,
+
collection: 'app.bsky.actor.profile',
+
rkey: 'self',
})
-
didHandleMap[profile.handle] = agent.accountDid
+
const profile =
+
Profile.isRecord(profileRecord.value) &&
+
Profile.validateRecord(profileRecord.value).success
+
? profileRecord.value
+
: {}
// Serve the logged-in view
-
return res
-
.type('html')
-
.send(page(home({ statuses, didHandleMap, profile, myStatus })))
+
return res.type('html').send(
+
page(
+
home({
+
statuses,
+
didHandleMap,
+
profile,
+
myStatus,
+
})
+
)
+
)
})
)
···
// If the user is signed in, get an agent which communicates with their server
const agent = await getSessionAgent(req, res, ctx)
if (!agent) {
-
return res.status(401).json({ error: 'Session required' })
+
return res
+
.status(401)
+
.type('html')
+
.send('<h1>Error: Session required</h1>')
}
// Construct & validate their status record
+
const rkey = TID.nextStr()
const record = {
$type: 'com.example.status',
status: req.body?.status,
-
updatedAt: new Date().toISOString(),
+
createdAt: new Date().toISOString(),
}
if (!Status.validateRecord(record).success) {
-
return res.status(400).json({ error: 'Invalid status' })
+
return res
+
.status(400)
+
.type('html')
+
.send('<h1>Error: Invalid status</h1>')
}
+
let uri
try {
// Write the status record to the user's repository
-
await agent.com.atproto.repo.putRecord({
+
const res = await agent.com.atproto.repo.putRecord({
repo: agent.accountDid,
collection: 'com.example.status',
-
rkey: 'self',
+
rkey,
record,
validate: false,
})
+
uri = res.data.uri
} catch (err) {
ctx.logger.warn({ err }, 'failed to write record')
-
return res.status(500).json({ error: 'Failed to write record' })
+
return res
+
.status(500)
+
.type('html')
+
.send('<h1>Error: Failed to write record</h1>')
}
try {
···
await ctx.db
.insertInto('status')
.values({
+
uri,
authorDid: agent.accountDid,
status: record.status,
-
updatedAt: record.updatedAt,
+
createdAt: record.createdAt,
indexedAt: new Date().toISOString(),
})
-
.onConflict((oc) =>
-
oc.column('authorDid').doUpdateSet({
-
status: record.status,
-
updatedAt: record.updatedAt,
-
indexedAt: new Date().toISOString(),
-
})
-
)
.execute()
} catch (err) {
ctx.logger.warn(
···
)
}
-
res.status(200).json({})
+
return res.redirect('/')
})
)