Change instance emojis code to retrieve all (even if servers didn't send them as Emoji object)

This commit is contained in:
Niko 2022-03-14 21:58:00 +01:00
parent 2ad27897fe
commit a3dfbbdde4
1 changed files with 25 additions and 19 deletions

View File

@ -6,25 +6,31 @@ module.exports = {
get: [auth.enforceSession, async (req, res) => { get: [auth.enforceSession, async (req, res) => {
var result = {} var result = {}
const masto_emojis = await db.table.objects().find( const persons = await db.table.objects().find({
{ type: 'http://joinmastodon.org/ns#Emoji' }).toArray() type: "Person",
for (var i = 0; i < masto_emojis.length; i++) { tag: { $elemMatch: { type: "http://joinmastodon.org/ns#Emoji" }},
const emoji = masto_emojis[i] }).project({ tag: 1 }).toArray()
var name = utils.firstIfArray(emoji.name) for (var i = 0; i < persons.length; i++) {
if (name === null) continue for (var j = 0; j < persons[i].tag.length; j++) {
name = name.replace(/^:/,'').replace(/:$/,'').trim() const emoji = persons[i].tag[j]
if (result[name] !== undefined || if (emoji.type !== 'http://joinmastodon.org/ns#Emoji')
!name.match(/^[a-zA-Z0-9\_]+$/)) continue
continue var name = utils.firstIfArray(emoji.name)
const icon = utils.firstIfArray(emoji.icon) if (name === null) continue
if (icon === null) continue name = name.replace(/^:/,'').replace(/:$/,'').trim()
const url = utils.firstIfArray(icon.url) if (result[name] !== undefined ||
if (url === null) continue !name.match(/^[a-zA-Z0-9\_]+$/))
try { new URL(url).href.length > 0 } continue
catch (e) { continue } const icon = utils.firstIfArray(emoji.icon)
result[name] = { if (icon === null) continue
type: utils.firstIfArray(icon.mediaType), const url = utils.firstIfArray(icon.url)
url, if (url === null) continue
try { new URL(url).href.length > 0 }
catch (e) { continue }
result[name] = {
type: utils.firstIfArray(icon.mediaType),
url,
}
} }
} }