Skip to content

Instantly share code, notes, and snippets.

@steinbring
Last active November 25, 2023 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinbring/1aa170bb1189ce9131938c2e44a3538c to your computer and use it in GitHub Desktop.
Save steinbring/1aa170bb1189ce9131938c2e44a3538c to your computer and use it in GitHub Desktop.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
let path = url.pathname;
// WebFinger endpoint for discovery
if (path === '/.well-known/webfinger') {
return handleWebFingerRequest(url);
}
// User profile endpoint
if (path === '/joe') {
return new Response(JSON.stringify(userProfile), {
headers: { 'Content-Type': 'application/activity+json' }
});
}
// User outbox endpoint
if (path === '/joe/outbox') {
return new Response(JSON.stringify(outbox), {
headers: { 'Content-Type': 'application/activity+json' }
});
}
// User followers endpoint (mocked)
if (path === '/joe/followers') {
return new Response(JSON.stringify(followers), {
headers: { 'Content-Type': 'application/activity+json' }
});
}
// Inbox endpoint (for accepting follow requests)
if (path === '/joe/inbox') {
return new Response(null, { status: 202 }); // Accepting but not processing
}
return new Response('Not Found', { status: 404 });
}
// WebFinger request handler
function handleWebFingerRequest(url) {
const resource = url.searchParams.get('resource');
if (resource === 'acct:joe@social.joe.workers.dev') {
const webFingerResponse = {
subject: resource,
links: [
{
rel: 'self',
type: 'application/activity+json',
href: 'https://social.joe.workers.dev/joe'
}
]
};
return new Response(JSON.stringify(webFingerResponse), {
headers: { 'Content-Type': 'application/jrd+json' }
});
}
return new Response('Resource Not Found', { status: 404 });
}
// Example user profile
const userProfile = {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://social.joe.workers.dev/joe",
"type": "Person",
"name": "Joe Steinbring",
"preferredUsername": "joe",
"inbox": "https://social.joe.workers.dev/joe/inbox",
"outbox": "https://social.joe.workers.dev/joe/outbox",
"followers": "https://social.joe.workers.dev/joe/followers"
};
// Example outbox with a single post
const outbox = {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://social.joe.workers.dev/joe/outbox",
"type": "OrderedCollection",
"totalItems": 1,
"orderedItems": [
{
"id": "https://social.joe.workers.dev/joe/post/1",
"type": "Note",
"published": "2023-11-15T12:00:00Z",
"attributedTo": "https://social.joe.workers.dev/joe",
"content": "Hello world!"
}
]
};
// Mocked followers collection
const followers = {
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://social.joe.workers.dev/joe/followers",
"type": "OrderedCollection",
"totalItems": 0, // No followers initially
"orderedItems": []
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment