@skyware/labeler
Version:
A lightweight alternative to Ozone for operating an atproto labeler.
70 lines (69 loc) • 3.24 kB
JavaScript
import "@atcute/bluesky/lexicons";
import { loginAgent } from "./util.js";
/**
* Declare the labels this labeler will apply. Necessary for users to be able to configure what they see.
* @param credentials The credentials of the labeler account.
* @param labelDefinitions The label definitions to declare. You can learn about the definition format [here](https://docs.bsky.app/docs/advanced-guides/moderation#custom-label-values).
* @param overwriteExisting Whether to overwrite the existing label definitions if they already exist.
*/
export async function declareLabeler(credentials, labelDefinitions, overwriteExisting) {
const { agent, session } = await loginAgent(credentials);
const labelValues = labelDefinitions.map(({ identifier }) => identifier);
const existing = await getLabelerLabelDefinitions(credentials);
if (existing?.length && !overwriteExisting) {
if (overwriteExisting === false)
return;
else if (overwriteExisting === undefined) {
throw new Error("Label definitions already exist. Use `overwriteExisting: true` to update them, or `overwriteExisting: false` to silence this error.");
}
}
const data = {
collection: "app.bsky.labeler.service",
rkey: "self",
repo: session.did,
record: {
$type: "app.bsky.labeler.service",
policies: { labelValues, labelValueDefinitions: labelDefinitions },
createdAt: new Date().toISOString(),
},
validate: true,
};
// We check if existing is truthy because an empty array means the record exists, but contains no definitions.
if (existing) {
await agent.call("com.atproto.repo.putRecord", { data });
}
else {
await agent.call("com.atproto.repo.createRecord", { data });
}
}
/**
* Get the label definitions currently declared by the labeler.
* @param credentials The credentials of the labeler account.
* @returns The label definitions.
*/
export async function getLabelerLabelDefinitions(credentials) {
const { agent, session } = await loginAgent(credentials);
const { data: { value: declaration } } = await agent.get("com.atproto.repo.getRecord", {
params: { collection: "app.bsky.labeler.service", rkey: "self", repo: session.did },
}).catch(() => ({ data: { value: {} } }));
// @ts-expect-error — declaration is unknown
return declaration?.policies?.labelValueDefinitions ?? null;
}
/**
* Set the label definitions for this labeler account.
* @param credentials The credentials of the labeler account.
* @param labelDefinitions The label definitions to set.
*/
export async function setLabelerLabelDefinitions(credentials, labelDefinitions) {
return declareLabeler(credentials, labelDefinitions, true);
}
/**
* Delete the labeler declaration for this account, removing all label definitions.
* @param credentials The credentials of the labeler account.
*/
export async function deleteLabelerDeclaration(credentials) {
const { agent, session } = await loginAgent(credentials);
await agent.call("com.atproto.repo.deleteRecord", {
data: { collection: "app.bsky.labeler.service", rkey: "self", repo: session.did },
});
}