this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Agent login abstraction in scripts, add getLabelerLabelDefinitions function

futurGH 2f9df2f0 6761aaeb

+82 -31
+1
.eslintrc.json
··· 8 8 "rules": { 9 9 "@typescript-eslint/no-explicit-any": "off", 10 10 "@typescript-eslint/no-misused-promises": "off", 11 + "@typescript-eslint/no-non-null-assertion": "off", 11 12 "@typescript-eslint/no-unnecessary-condition": "off", 12 13 "@typescript-eslint/no-unsafe-assignment": "off", 13 14 "@typescript-eslint/no-unsafe-call": "off",
+47 -15
src/scripts/declareLabeler.ts
··· 1 1 import { AtpAgent, ComAtprotoLabelDefs } from "@atproto/api"; 2 + import { loginAgentOrCredentials } from "./util.js"; 2 3 3 4 /** 4 5 * Declare the labels this labeler will apply. Necessary for users to be able to configure what they see. 5 - * @param agent The agent logged into the labeler account. 6 + * @param credentials The credentials of the labeler account. 6 7 * @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). 8 + * @param overwriteExisting Whether to overwrite the existing label definitions if they already exist. 7 9 */ 8 10 export async function declareLabeler( 9 - agent: AtpAgent, 11 + credentials: { pds?: string; identifier: string; password: string }, 10 12 labelDefinitions: Array<ComAtprotoLabelDefs.LabelValueDefinition>, 13 + overwriteExisting?: boolean, 11 14 ): Promise<void>; 12 15 /** 13 16 * Declare the labels this labeler will apply. Necessary for users to be able to configure what they see. 14 - * @param credentials The credentials of the labeler account. 17 + * @param agent The agent logged into the labeler account. 15 18 * @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). 19 + * @param overwriteExisting Whether to overwrite the existing label definitions if they already exist. 16 20 */ 17 21 export async function declareLabeler( 18 - credentials: { pds?: string; identifier: string; password: string }, 22 + agent: AtpAgent, 19 23 labelDefinitions: Array<ComAtprotoLabelDefs.LabelValueDefinition>, 24 + overwriteExisting?: boolean, 20 25 ): Promise<void>; 21 26 export async function declareLabeler( 22 27 agentOrCredentials: AtpAgent | { pds?: string; identifier: string; password: string }, 23 28 labelDefinitions: Array<ComAtprotoLabelDefs.LabelValueDefinition>, 29 + overwriteExisting?: boolean, 24 30 ) { 25 - const agent = agentOrCredentials instanceof AtpAgent 26 - ? agentOrCredentials 27 - : new AtpAgent({ service: agentOrCredentials.pds || "https://bsky.social" }); 28 - if (!agent.hasSession) { 29 - if (!(agentOrCredentials instanceof AtpAgent)) { 30 - await agent.login(agentOrCredentials); 31 - } else { 32 - throw new Error("A password must be provided to log in to the labeler account."); 33 - } 31 + const agent = await loginAgentOrCredentials(agentOrCredentials); 32 + const labelValues = labelDefinitions.map(({ identifier }) => identifier); 33 + 34 + const existing = await getLabelerLabelDefinitions(agent); 35 + if (existing?.length && !overwriteExisting) { 36 + if (overwriteExisting === false) return; 37 + throw new Error( 38 + "Label definitions already exist. Use `overwriteExisting: true` to update them, or `overwriteExisting: false` to silence this error.", 39 + ); 34 40 } 35 41 36 - const labelValues = labelDefinitions.map(({ identifier }) => identifier); 37 - await agent.app.bsky.labeler.service.create({ repo: agent.did }, { 42 + await agent.app.bsky.labeler.service.create({ repo: agent.accountDid }, { 38 43 policies: { labelValues, labelValueDefinitions: labelDefinitions }, 39 44 createdAt: new Date().toUTCString(), 40 45 }); 41 46 } 47 + 48 + /** 49 + * Get the label definitions currently declared by the labeler. 50 + * @param credentials The credentials of the labeler account. 51 + * @returns The label definitions. 52 + */ 53 + export async function getLabelerLabelDefinitions( 54 + credentials: { pds?: string; identifier: string; password: string }, 55 + ): Promise<Array<ComAtprotoLabelDefs.LabelValueDefinition>>; 56 + /** 57 + * Get the label definitions currently declared by the labeler. 58 + * @param agent The agent logged into the labeler account. 59 + * @returns The label definitions. 60 + */ 61 + export async function getLabelerLabelDefinitions( 62 + agent: AtpAgent, 63 + ): Promise<Array<ComAtprotoLabelDefs.LabelValueDefinition>>; 64 + export async function getLabelerLabelDefinitions( 65 + agentOrCredentials: AtpAgent | { pds?: string; identifier: string; password: string }, 66 + ) { 67 + const agent = await loginAgentOrCredentials(agentOrCredentials); 68 + const { value: { policies } } = await agent.app.bsky.labeler.service.get({ 69 + rkey: "self", 70 + repo: agent.accountDid, 71 + }); 72 + return policies.labelValueDefinitions; 73 + }
+8 -16
src/scripts/plc.ts
··· 1 1 import { AtpAgent, ComAtprotoIdentitySignPlcOperation } from "@atproto/api"; 2 2 import { P256Keypair, Secp256k1Keypair } from "@atproto/crypto"; 3 3 import * as ui8 from "uint8arrays"; 4 + import { loginAgentOrCredentials } from "./util.js"; 4 5 5 6 /** Options for the {@link plcSetupLabeler} function. */ 6 7 export interface PlcSetupLabelerOptions { ··· 126 127 /** 127 128 * Request a PLC token, needed for {@link plcSetupLabeler}. The token will be sent to the email 128 129 * associated with the labeler account. 129 - * @param agent An agent logged into the labeler account. 130 - */ 131 - export async function plcRequestToken(agent: AtpAgent): Promise<void>; 132 - /** 133 - * Request a PLC token, needed for {@link plcSetupLabeler}. The token will be sent to the email 134 - * associated with the labeler account. 135 130 * @param credentials The credentials of the labeler account. 136 131 */ 137 132 export async function plcRequestToken( 138 133 credentials: { pds?: string; identifier: string; password: string }, 139 134 ): Promise<void>; 135 + /** 136 + * Request a PLC token, needed for {@link plcSetupLabeler}. The token will be sent to the email 137 + * associated with the labeler account. 138 + * @param agent An agent logged into the labeler account. 139 + */ 140 + export async function plcRequestToken(agent: AtpAgent): Promise<void>; 140 141 export async function plcRequestToken( 141 142 agentOrCredentials: AtpAgent | { pds?: string; identifier: string; password: string }, 142 143 ) { 143 - const agent = agentOrCredentials instanceof AtpAgent 144 - ? agentOrCredentials 145 - : new AtpAgent({ service: agentOrCredentials.pds || "https://bsky.social" }); 146 - if (!agent.hasSession) { 147 - if (!(agentOrCredentials instanceof AtpAgent)) { 148 - await agent.login(agentOrCredentials); 149 - } else { 150 - throw new Error("A password must be provided to log in to the labeler account."); 151 - } 152 - } 144 + const agent = await loginAgentOrCredentials(agentOrCredentials); 153 145 await agent.com.atproto.identity.requestPlcOperationSignature(); 154 146 }
+26
src/scripts/util.ts
··· 1 + import { AtpAgent } from "@atproto/api"; 2 + 3 + export interface LoginCredentials { 4 + /** The URL of the PDS where the account is located. Defaults to "https://bsky.social". */ 5 + pds?: string; 6 + /** The account identifier; a DID or handle. */ 7 + identifier: string; 8 + /** The account password. */ 9 + password: string; 10 + } 11 + 12 + export async function loginAgentOrCredentials( 13 + agentOrCredentials: AtpAgent | { pds?: string; identifier: string; password: string }, 14 + ) { 15 + const agent = agentOrCredentials instanceof AtpAgent 16 + ? agentOrCredentials 17 + : new AtpAgent({ service: agentOrCredentials.pds || "https://bsky.social" }); 18 + if (!agent.hasSession) { 19 + if (!(agentOrCredentials instanceof AtpAgent)) { 20 + await agent.login(agentOrCredentials); 21 + } else { 22 + throw new Error("A password must be provided to log in to the labeler account."); 23 + } 24 + } 25 + return agent; 26 + }