From 6b17ac70b1bd081125a951a5eb96380e1b679b60 Mon Sep 17 00:00:00 2001 From: Niko Date: Wed, 16 Feb 2022 15:45:45 +0100 Subject: [PATCH] Added "getAccount" to get an specific actor relevant data for APIs --- api/src/api-utils.js | 9 +++++++++ api/src/api/accounts.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/api/src/api-utils.js b/api/src/api-utils.js index a7afc4d..803b9a2 100644 --- a/api/src/api-utils.js +++ b/api/src/api-utils.js @@ -10,6 +10,15 @@ module.exports = { return false } }, + firstIfArray: (o) => { + if (o === undefined) + return null + if (o instanceof Array) + if (o.length > 0) + return o[0] + else return null + return o + }, apRandomURL: () => { return 'https://'+apex.domain+'/'+crypto.randomUUID() }, diff --git a/api/src/api/accounts.js b/api/src/api/accounts.js index eeb25d7..33309ea 100644 --- a/api/src/api/accounts.js +++ b/api/src/api/accounts.js @@ -25,7 +25,27 @@ async function getRejectedTimes(req, res) { return times } +async function getAccount(id) { + const person = await db.getAPObject(id, 'Person') + const username = utils.firstIfArray(person.preferredUsername) + const icon = utils.firstIfArray(person.icon) + const newicon = { + type: utils.firstIfArray(icon.mediaType), + url: utils.firstIfArray(icon.url), + } + return { + _id: person._id, + url: person.id, + acct: username+'@'+(new URL(person.id)).hostname, + name: utils.firstIfArray(person.name), + summary: utils.firstIfArray(person.summary), + avatar: newicon, + username + } +} + module.exports = { + getAccount, follow: { post: [auth.enforceSession, async (req, res) => { if (!utils.isURLValid(req.query.url))