From e2c99d8e968fc41719a49d59b1b46ebf95e0c4b1 Mon Sep 17 00:00:00 2001 From: split Date: Fri, 22 Nov 2024 21:46:04 -0800 Subject: [PATCH] feat(gravatar): :sparkles: specify email for avatar --- .vscode/settings.json | 5 +++++ README.md | 1 + package.json | 2 +- src/translators/gravatar.ts | 20 +++++++++++++++----- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a11a75b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "conventionalCommits.scopes": [ + "gravatar" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index c3ccdcf..9ade627 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ All settings for endpoints are configured in query parameters. ### `/gravatar` - `cookie`: Your `gravatar` cookie. +- `email` (optional): Email to set the profile picture for. ### `/misskey` diff --git a/package.json b/package.json index 4ab2d71..8629f1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "avahooks", - "version": "1.1.0", + "version": "1.2.0", "main": "index.js", "scripts": { "start": "node ./dist/index.js", diff --git a/src/translators/gravatar.ts b/src/translators/gravatar.ts index 2b17bac..190026f 100644 --- a/src/translators/gravatar.ts +++ b/src/translators/gravatar.ts @@ -1,11 +1,16 @@ import { z } from "zod" import { translator } from "../lib/types.js" import downloadAvatarForPayload from "../lib/downloadAvatarForPayload.js" +import crypto from "node:crypto" export default translator({ query: z.object({ cookie: z.string().describe("Your `gravatar` cookie."), + email: z + .string() + .optional() + .describe("Email to set the profile picture for."), }), - async execute(payload, { cookie }) { + async execute(payload, { cookie, email }) { // unsure regarding supported image formats; // just to play it safe, prefer png then jpeg @@ -41,10 +46,15 @@ export default translator({ `gravatar: upload failed with ${uploadResult.status}` ) - const { - email_hash, - image_id, - }: { email_hash: string; image_id: string } = await uploadResult.json() + const uprRes: { email_hash: string; image_id: string } = + await uploadResult.json(), + { image_id } = uprRes, + email_hash = email + ? crypto + .createHash("md5") + .update(email.toLowerCase()) + .digest("hex") + : uprRes.email_hash // set its alt text