mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Refactor getCustomEmojis function, support emoji categories
This commit is contained in:
parent
f15b6f79c0
commit
b1a1ace0ac
2 changed files with 37 additions and 16 deletions
|
|
@ -57,11 +57,13 @@ Deno.test('customEmojisRoute', async (t) => {
|
||||||
url: 'https://soapbox.pub/favicon.ico',
|
url: 'https://soapbox.pub/favicon.ico',
|
||||||
static_url: 'https://soapbox.pub/favicon.ico',
|
static_url: 'https://soapbox.pub/favicon.ico',
|
||||||
visible_in_picker: true,
|
visible_in_picker: true,
|
||||||
|
category: 'soapbox',
|
||||||
}, {
|
}, {
|
||||||
shortcode: 'ditto',
|
shortcode: 'ditto',
|
||||||
url: 'https://ditto.pub/favicon.ico',
|
url: 'https://ditto.pub/favicon.ico',
|
||||||
static_url: 'https://ditto.pub/favicon.ico',
|
static_url: 'https://ditto.pub/favicon.ico',
|
||||||
visible_in_picker: true,
|
visible_in_picker: true,
|
||||||
|
category: 'soapbox',
|
||||||
}]);
|
}]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { userMiddleware } from '@ditto/mastoapi/middleware';
|
import { userMiddleware } from '@ditto/mastoapi/middleware';
|
||||||
import { DittoRoute } from '@ditto/mastoapi/router';
|
import { DittoRoute } from '@ditto/mastoapi/router';
|
||||||
import { NostrFilter } from '@nostrify/nostrify';
|
import { NostrFilter, NRelay } from '@nostrify/nostrify';
|
||||||
|
|
||||||
const route = new DittoRoute();
|
const route = new DittoRoute();
|
||||||
|
|
||||||
|
|
@ -13,18 +13,42 @@ interface MastodonCustomEmoji {
|
||||||
}
|
}
|
||||||
|
|
||||||
route.get('/', userMiddleware(), async (c) => {
|
route.get('/', userMiddleware(), async (c) => {
|
||||||
const { relay, user, signal } = c.var;
|
const { user } = c.var;
|
||||||
|
|
||||||
const pubkey = await user.signer.getPublicKey();
|
const pubkey = await user.signer.getPublicKey();
|
||||||
|
const emojis = await getCustomEmojis(pubkey, c.var);
|
||||||
|
|
||||||
|
return c.json([...emojis.entries()].map(([shortcode, data]): MastodonCustomEmoji => {
|
||||||
|
return {
|
||||||
|
shortcode,
|
||||||
|
url: data.url.toString(),
|
||||||
|
static_url: data.url.toString(),
|
||||||
|
visible_in_picker: true,
|
||||||
|
category: data.category,
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
interface GetCustomEmojisOpts {
|
||||||
|
relay: NRelay;
|
||||||
|
signal?: AbortSignal;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getCustomEmojis(
|
||||||
|
pubkey: string,
|
||||||
|
opts: GetCustomEmojisOpts,
|
||||||
|
): Promise<Map<string, { url: URL; category?: string }>> {
|
||||||
|
const { relay, signal } = opts;
|
||||||
|
|
||||||
|
const emojis = new Map<string, { url: URL; category?: string }>();
|
||||||
|
|
||||||
const [emojiList] = await relay.query([{ kinds: [10030], authors: [pubkey] }], { signal });
|
const [emojiList] = await relay.query([{ kinds: [10030], authors: [pubkey] }], { signal });
|
||||||
|
|
||||||
if (!emojiList) {
|
if (!emojiList) {
|
||||||
return c.json([]);
|
return emojis;
|
||||||
}
|
}
|
||||||
|
|
||||||
const a = new Set<string>();
|
const a = new Set<string>();
|
||||||
const emojis = new Map<string, URL>();
|
|
||||||
|
|
||||||
for (const tag of emojiList.tags) {
|
for (const tag of emojiList.tags) {
|
||||||
if (tag[0] === 'emoji') {
|
if (tag[0] === 'emoji') {
|
||||||
|
|
@ -32,7 +56,7 @@ route.get('/', userMiddleware(), async (c) => {
|
||||||
|
|
||||||
if (!emojis.has(shortcode)) {
|
if (!emojis.has(shortcode)) {
|
||||||
try {
|
try {
|
||||||
emojis.set(shortcode, new URL(url));
|
emojis.set(shortcode, { url: new URL(url) });
|
||||||
} catch {
|
} catch {
|
||||||
// continue
|
// continue
|
||||||
}
|
}
|
||||||
|
|
@ -56,15 +80,17 @@ route.get('/', userMiddleware(), async (c) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filters.length) {
|
if (!filters.length) {
|
||||||
return c.json([]);
|
return new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const event of await relay.query(filters, { signal })) {
|
for (const event of await relay.query(filters, { signal })) {
|
||||||
|
const d = event.tags.find(([name]) => name === 'd')?.[1];
|
||||||
|
|
||||||
for (const [t, shortcode, url] of event.tags) {
|
for (const [t, shortcode, url] of event.tags) {
|
||||||
if (t === 'emoji') {
|
if (t === 'emoji') {
|
||||||
if (!emojis.has(shortcode)) {
|
if (!emojis.has(shortcode)) {
|
||||||
try {
|
try {
|
||||||
emojis.set(shortcode, new URL(url));
|
emojis.set(shortcode, { url: new URL(url), category: d });
|
||||||
} catch {
|
} catch {
|
||||||
// continue
|
// continue
|
||||||
}
|
}
|
||||||
|
|
@ -73,14 +99,7 @@ route.get('/', userMiddleware(), async (c) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json([...emojis.entries()].map(([shortcode, url]): MastodonCustomEmoji => {
|
return emojis;
|
||||||
return {
|
}
|
||||||
shortcode,
|
|
||||||
url: url.toString(),
|
|
||||||
static_url: url.toString(),
|
|
||||||
visible_in_picker: true,
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
export default route;
|
export default route;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue