fetch user data endpoint

This commit is contained in:
hatim boufnichel 2020-10-17 18:41:03 +02:00
parent 4d126ef180
commit f17f8a1ff6
3 changed files with 55 additions and 1 deletions

View file

@ -46,7 +46,7 @@ module.exports = (mainnet = false) => {
logfile: "shockapi.log", logfile: "shockapi.log",
lndLogFile: parsePath(`${lndDirectory}/logs/bitcoin/${network}/lnd.log`), lndLogFile: parsePath(`${lndDirectory}/logs/bitcoin/${network}/lnd.log`),
lndDirPath: lndDirectory, lndDirPath: lndDirectory,
peers: ['http://gun.shock.network:8765/gun','http://gun2.shock.network:8765/gun'], peers: ["http://superpeer:8765/gun"],
useTLS: false, useTLS: false,
tokenExpirationMS: 259200000 tokenExpirationMS: 259200000
}; };

View file

@ -103,8 +103,40 @@ const getMyUser = async () => {
return u 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.getMyUser = getMyUser
module.exports.getUserInfo = getUserInfo
module.exports.Follows = require('./follows') module.exports.Follows = require('./follows')
module.exports.getWallPage = Wall.getWallPage 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 * @template P