wall getters
This commit is contained in:
parent
dd97106aae
commit
06f969b64e
2 changed files with 61 additions and 0 deletions
|
|
@ -89,3 +89,4 @@ const getMyUser = async () => {
|
|||
|
||||
module.exports.getMyUser = getMyUser
|
||||
module.exports.Follows = require('./follows')
|
||||
module.exports.Wall = require('./wall')
|
||||
|
|
|
|||
60
services/gunDB/contact-api/getters/wall.js
Normal file
60
services/gunDB/contact-api/getters/wall.js
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue