From a3dfbbdde4f41868bbe84009c40345c5ca5801a4 Mon Sep 17 00:00:00 2001 From: Niko Date: Mon, 14 Mar 2022 21:58:00 +0100 Subject: [PATCH] Change instance emojis code to retrieve all (even if servers didn't send them as Emoji object) --- api/src/api/instance.js | 44 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/api/src/api/instance.js b/api/src/api/instance.js index 74eb975..867334a 100644 --- a/api/src/api/instance.js +++ b/api/src/api/instance.js @@ -6,25 +6,31 @@ module.exports = { get: [auth.enforceSession, async (req, res) => { var result = {} - const masto_emojis = await db.table.objects().find( - { type: 'http://joinmastodon.org/ns#Emoji' }).toArray() - for (var i = 0; i < masto_emojis.length; i++) { - const emoji = masto_emojis[i] - var name = utils.firstIfArray(emoji.name) - if (name === null) continue - name = name.replace(/^:/,'').replace(/:$/,'').trim() - if (result[name] !== undefined || - !name.match(/^[a-zA-Z0-9\_]+$/)) - continue - const icon = utils.firstIfArray(emoji.icon) - if (icon === null) continue - const url = utils.firstIfArray(icon.url) - if (url === null) continue - try { new URL(url).href.length > 0 } - catch (e) { continue } - result[name] = { - type: utils.firstIfArray(icon.mediaType), - url, + const persons = await db.table.objects().find({ + type: "Person", + tag: { $elemMatch: { type: "http://joinmastodon.org/ns#Emoji" }}, + }).project({ tag: 1 }).toArray() + for (var i = 0; i < persons.length; i++) { + for (var j = 0; j < persons[i].tag.length; j++) { + const emoji = persons[i].tag[j] + if (emoji.type !== 'http://joinmastodon.org/ns#Emoji') + continue + var name = utils.firstIfArray(emoji.name) + if (name === null) continue + name = name.replace(/^:/,'').replace(/:$/,'').trim() + if (result[name] !== undefined || + !name.match(/^[a-zA-Z0-9\_]+$/)) + continue + const icon = utils.firstIfArray(emoji.icon) + if (icon === null) continue + const url = utils.firstIfArray(icon.url) + if (url === null) continue + try { new URL(url).href.length > 0 } + catch (e) { continue } + result[name] = { + type: utils.firstIfArray(icon.mediaType), + url, + } } }