mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Pleroma API supports custom emojis with or without colons
This commit is contained in:
parent
1dd50e2e63
commit
755ed884d4
1 changed files with 13 additions and 11 deletions
|
|
@ -17,11 +17,10 @@ const route = new DittoRoute();
|
||||||
* https://docs.pleroma.social/backend/development/API/pleroma_api/#put-apiv1pleromastatusesidreactionsemoji
|
* https://docs.pleroma.social/backend/development/API/pleroma_api/#put-apiv1pleromastatusesidreactionsemoji
|
||||||
*/
|
*/
|
||||||
route.put('/:id{[0-9a-f]{64}}/reactions/:emoji', userMiddleware(), async (c) => {
|
route.put('/:id{[0-9a-f]{64}}/reactions/:emoji', userMiddleware(), async (c) => {
|
||||||
const params = c.req.param();
|
|
||||||
|
|
||||||
const { relay, user, conf, signal } = c.var;
|
const { relay, user, conf, signal } = c.var;
|
||||||
const { type, value } = parseEmojiParam(params.emoji);
|
|
||||||
|
|
||||||
|
const params = c.req.param();
|
||||||
|
const result = parseEmojiParam(params.emoji);
|
||||||
const pubkey = await user.signer.getPublicKey();
|
const pubkey = await user.signer.getPublicKey();
|
||||||
|
|
||||||
const [event] = await relay.query([{ ids: [params.id] }], { signal });
|
const [event] = await relay.query([{ ids: [params.id] }], { signal });
|
||||||
|
|
@ -34,18 +33,18 @@ route.put('/:id{[0-9a-f]{64}}/reactions/:emoji', userMiddleware(), async (c) =>
|
||||||
['p', event.pubkey, conf.relay],
|
['p', event.pubkey, conf.relay],
|
||||||
];
|
];
|
||||||
|
|
||||||
if (type === 'custom') {
|
if (result.type === 'custom') {
|
||||||
const emojis = await getCustomEmojis(pubkey, c.var);
|
const emojis = await getCustomEmojis(pubkey, c.var);
|
||||||
const emoji = emojis.get(value);
|
const emoji = emojis.get(result.shortcode);
|
||||||
|
|
||||||
if (!emoji) {
|
if (!emoji) {
|
||||||
return c.json({ error: 'Custom emoji not found' }, 404);
|
return c.json({ error: 'Custom emoji not found' }, 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
tags.push(['emoji', value, emoji.url.href]);
|
tags.push(['emoji', result.shortcode, emoji.url.href]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = type === 'custom' ? `:${value}:` : value;
|
const content = result.type === 'custom' ? `:${result.shortcode}:` : result.emoji;
|
||||||
|
|
||||||
await createEvent({ kind: 7, content, tags }, c);
|
await createEvent({ kind: 7, content, tags }, c);
|
||||||
await hydrateEvents({ ...c.var, events: [event] });
|
await hydrateEvents({ ...c.var, events: [event] });
|
||||||
|
|
@ -137,13 +136,16 @@ route.get('/:id{[0-9a-f]{64}}/reactions/:emoji?', userMiddleware({ required: fal
|
||||||
});
|
});
|
||||||
|
|
||||||
/** Determine if the input is a native or custom emoji, returning a structured object or throwing an error. */
|
/** Determine if the input is a native or custom emoji, returning a structured object or throwing an error. */
|
||||||
function parseEmojiParam(input: string): { type: 'native' | 'custom'; value: string } {
|
function parseEmojiParam(input: string): { type: 'native'; emoji: string } | { type: 'custom'; shortcode: string } {
|
||||||
if (/^\p{RGI_Emoji}$/v.test(input)) {
|
if (/^\p{RGI_Emoji}$/v.test(input)) {
|
||||||
return { type: 'native', value: input };
|
return { type: 'native', emoji: input };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/^\w+$/.test(input)) {
|
const match = input.match(/^:?(\w+):?$/); // Pleroma API supports with or without colons.
|
||||||
return { type: 'custom', value: input };
|
|
||||||
|
if (match) {
|
||||||
|
const [, shortcode] = match;
|
||||||
|
return { type: 'custom', shortcode };
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new HTTPException(400, { message: 'Invalid emoji' });
|
throw new HTTPException(400, { message: 'Invalid emoji' });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue