support public key lookup for wall

This commit is contained in:
Daniel Lugo 2020-06-26 21:15:53 -04:00
parent b1594e1707
commit 140b0a43b0
2 changed files with 21 additions and 12 deletions

View file

@ -6,15 +6,19 @@ const Utils = require('../utils')
const Key = require('../key') const Key = require('../key')
/** /**
* @param {string=} publicKey
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
const getWallTotalPages = async () => { const getWallTotalPages = async publicKey => {
const totalPages = await Utils.tryAndWait( const totalPages = await Utils.tryAndWait(
(_, user) => (gun, u) => {
user const user = publicKey ? gun.get(`~${publicKey}`) : u
return user
.get(Key.WALL) .get(Key.WALL)
.get(Key.NUM_OF_PAGES) .get(Key.NUM_OF_PAGES)
.then(), .then()
},
v => typeof v !== 'number' v => typeof v !== 'number'
) )
@ -23,12 +27,13 @@ const getWallTotalPages = async () => {
/** /**
* @param {number} page * @param {number} page
* @param {string=} publicKey
* @throws {TypeError} * @throws {TypeError}
* @throws {RangeError} * @throws {RangeError}
* @returns {Promise<Common.SchemaTypes.WallPage>} * @returns {Promise<Common.SchemaTypes.WallPage>}
*/ */
const getWallPage = async page => { const getWallPage = async (page, publicKey) => {
const totalPages = await getWallTotalPages() const totalPages = await getWallTotalPages(publicKey)
if (page === 0 || totalPages === 0) { if (page === 0 || totalPages === 0) {
return { return {
@ -47,15 +52,18 @@ const getWallPage = async page => {
* @type {Common.SchemaTypes.WallPage} * @type {Common.SchemaTypes.WallPage}
*/ */
const thePage = await Utils.tryAndWait( const thePage = await Utils.tryAndWait(
(_, user) => (g, u) => {
new Promise(res => { const user = publicKey ? g.get(`~${publicKey}`) : u
return new Promise(res => {
user user
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
.get(actualPageIdx.toString()) .get(actualPageIdx.toString())
// @ts-ignore // @ts-ignore
.load(res) .load(res)
}), })
},
maybePage => { maybePage => {
if (typeof maybePage !== 'object' || maybePage === null) { if (typeof maybePage !== 'object' || maybePage === null) {
return true return true

View file

@ -1842,9 +1842,10 @@ module.exports = async (
}) })
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
app.get(`/api/gun/wall`, async (req, res) => { app.get(`/api/gun/wall/:publicKey`, async (req, res) => {
try { try {
const { page } = req.query; const { page } = req.query;
const {publicKey} = req.params
const pageNum = Number(page) const pageNum = Number(page)
@ -1855,8 +1856,8 @@ module.exports = async (
}) })
} }
const totalPages = await GunGetters.getWallTotalPages() const totalPages = await GunGetters.getWallTotalPages(publicKey)
const fetchedPage = await GunGetters.getWallPage(pageNum) const fetchedPage = await GunGetters.getWallPage(pageNum, publicKey)
return res.status(200).json({ return res.status(200).json({
...fetchedPage, ...fetchedPage,