diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index c8811e45..81ea8dc2 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -103,8 +103,40 @@ const getMyUser = async () => { return u } +/** + * @param {string} publicKey + */ +const getUserInfo = async publicKey => { + const userInfo = await Utils.tryAndWait( + gun => + new Promise(res => + gun + .user(publicKey) + .get(Key.PROFILE) + .load(res) + ), + v => { + if (typeof v !== 'object') { + return true + } + + if (v === null) { + return true + } + + // load sometimes returns an empty set on the first try + return size(v) === 0 + } + ) + return { + publicKey, + avatar: userInfo.avatar, + displayName: userInfo.displayName + } +} module.exports.getMyUser = getMyUser +module.exports.getUserInfo = getUserInfo module.exports.Follows = require('./follows') module.exports.getWallPage = Wall.getWallPage diff --git a/src/routes.js b/src/routes.js index 1d56f0eb..17b89a04 100644 --- a/src/routes.js +++ b/src/routes.js @@ -2285,6 +2285,28 @@ module.exports = async ( }) } }) + + app.post(`/api/gun/userInfo`, async (req, res) => { + try { + const { pubs } = req.body + const reqs = pubs.map( + e => + new Promise((res, rej) => { + GunGetters.getUserInfo(e) + .then(r => res(r)) + .catch(e => rej(e)) + }) + ) + const infos = await Promise.all(reqs) + return res.status(200).json({ + pubInfos: infos + }) + } catch (err) { + return res.status(500).json({ + errorMessage: err.message + }) + } + }) ///////////////////////////////// /** * @template P