chadtmiller.com/pds.js
An atproto PDS in JavaScript, for Cloudflare Workers and Node
0
0
// @pdsjs/core/handlers/xrpc-preferences - app.bsky.actor.*Preferences.
//
// The server keeps the preferences array as the client sends it. Its contents
// are the AppView's concern, not this one's.
/**
* @typedef {Object} PreferencesContext
* @property {import('../ports.js').ActorStoragePort} actorStorage
* @property {boolean} readOnly
* @property {() => Response} readOnlyError
*/
/**
* @param {PreferencesContext} ctx
* @returns {{ routes: import('../pds.js').Routes }}
*/
export function createPreferencesHandlers(ctx) {
const { actorStorage, readOnly, readOnlyError } = ctx;
/**
* app.bsky.actor.getPreferences
* @param {Request} _request
* @param {URL} _url
* @param {import('../pds.js').Auth|null} auth
* @returns {Promise<Response>}
*/
async function handleGetPreferences(_request, _url, auth) {
if (!auth) {
return Response.json(
{ error: 'AuthenticationRequired', message: 'Authentication required' },
{ status: 401 },
);
}
const preferences = await actorStorage.getPreferences();
return Response.json({ preferences });
}
/**
* app.bsky.actor.putPreferences
* @param {Request} request
* @param {URL} _url
* @param {import('../pds.js').Auth|null} auth
* @returns {Promise<Response>}
*/
async function handlePutPreferences(request, _url, auth) {
if (readOnly) return readOnlyError();
if (!auth) {
return Response.json(
{ error: 'AuthenticationRequired', message: 'Authentication required' },
{ status: 401 },
);
}
const { preferences } = await request.json();
if (!Array.isArray(preferences)) {
return Response.json(
{ error: 'InvalidRequest', message: 'preferences must be an array' },
{ status: 400 },
);
}
await actorStorage.setPreferences(preferences);
return Response.json({});
}
return {
routes: {
'/xrpc/app.bsky.actor.getPreferences': {
auth: 'required',
handler: handleGetPreferences,
},
'/xrpc/app.bsky.actor.putPreferences': {
method: 'POST',
auth: 'required',
handler: handlePutPreferences,
},
},
};
}