Merge pull request #216 from shocknet/feature/fetch-user-data

Feature/fetch user data
This commit is contained in:
Daniel Lugo 2020-10-17 14:17:00 -04:00 committed by GitHub
commit 2b7d77dca5
2 changed files with 54 additions and 0 deletions

View file

@ -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

View file

@ -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