wall getters

This commit is contained in:
Daniel Lugo 2020-06-23 19:26:53 -04:00
parent dd97106aae
commit 06f969b64e
2 changed files with 61 additions and 0 deletions

View file

@ -89,3 +89,4 @@ const getMyUser = async () => {
module.exports.getMyUser = getMyUser
module.exports.Follows = require('./follows')
module.exports.Wall = require('./wall')

View file

@ -0,0 +1,60 @@
/**
* @format
*/
const Common = require('shock-common')
const Utils = require('../utils')
const Key = require('../key')
/**
* @returns {Promise<number>}
*/
const getTotalPages = () =>
/** @type {Promise<number>} */ (Utils.tryAndWait(
(_, user) =>
user
.get(Key.WALL)
.get(Key.NUM_OF_PAGES)
.then(),
v => typeof v !== 'number'
))
/**
* Won't fail if given an invalid page, will return an empty set.
* @param {number} page
* @returns {Promise<Common.SchemaTypes.WallPage>}
*/
const getWallPage = async page => {
const totalPages = await getTotalPages()
const empty = {
count: 0,
posts: {}
}
if (page === 0) {
return empty
}
const actualPageIdx = page < 0 ? totalPages + (page + 1) : page - 1
const thePage = await Utils.tryAndWait(
(_, user) =>
new Promise(res => {
user
.get(Key.WALL)
.get(Key.PAGES)
.get(actualPageIdx.toString())
.load(res)
})
)
if (Common.Schema.isWallPage(thePage)) {
return thePage
}
return empty
}
module.exports = {
getTotalPages,
getWallPage
}