feat(gravatar): specify email for avatar

This commit is contained in:
May 2024-11-22 21:46:04 -08:00
parent e81de2aecc
commit e2c99d8e96
Signed by: split
GPG key ID: C325C61F0BF517C0
4 changed files with 22 additions and 6 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"conventionalCommits.scopes": [
"gravatar"
]
}

View file

@ -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`

View file

@ -1,6 +1,6 @@
{
"name": "avahooks",
"version": "1.1.0",
"version": "1.2.0",
"main": "index.js",
"scripts": {
"start": "node ./dist/index.js",

View file

@ -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