diff --git a/.vscode/settings.json b/.vscode/settings.json index a11a75b..157da48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "conventionalCommits.scopes": [ - "gravatar" + "gravatar", + "forgejo" ] } \ No newline at end of file diff --git a/README.md b/README.md index 9ade627..8c27555 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ All settings for endpoints are configured in query parameters. - `identifier`: Bluesky handle - `password`: Bluesky password - create an app password in settings +### `/forgejo` + +- `instance`: Link to your Forgejo instance. Include protocol. +- `apiKey`: Forgejo API key. + ### `/gravatar` - `cookie`: Your `gravatar` cookie. diff --git a/package.json b/package.json index 9d9af27..879604e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "avahooks", - "version": "1.2.1", + "version": "1.3.0", "main": "index.js", "scripts": { "start": "node ./dist/index.js", diff --git a/src/translators/forgejo.ts b/src/translators/forgejo.ts new file mode 100644 index 0000000..2c707b5 --- /dev/null +++ b/src/translators/forgejo.ts @@ -0,0 +1,45 @@ +import { z } from "zod" +import { translator } from "../lib/types.js" +import downloadAvatarForPayload from "../lib/downloadAvatarForPayload.js" +export default translator({ + query: z.object({ + instance: z + .string() + .describe("Link to your Forgejo instance. Include protocol."), + apiKey: z.string().describe("Forgejo API key."), + }), + async execute(payload, { instance, apiKey }) { + const avatar = await downloadAvatarForPayload(payload, [ + "png", + "jpeg", + "webp", + "gif", + undefined, + ]) + + if (!avatar) throw new Error("forgejo: failed to get avatar") + + // update the user avatar + + let updateRequest = await fetch( + new URL("/api/v1/user/avatar", instance), + { + headers: { + Authorization: `Bearer ${apiKey}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + avatar: `data:${avatar.type};base64,${Buffer.from( + await avatar.arrayBuffer() + ).toString("base64")}`, + }), + method: "POST", + } + ) + + if (!updateRequest.ok) + console.error( + `forgejo: updating avatar, got ${updateRequest.status}` + ) + }, +})