Merge pull request #216 from shocknet/feature/fetch-user-data
Feature/fetch user data
This commit is contained in:
commit
2b7d77dca5
2 changed files with 54 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue