From 06f969b64eb05496474c1fefe4060e84ef3edcbb Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 23 Jun 2020 19:26:53 -0400 Subject: [PATCH] wall getters --- services/gunDB/contact-api/getters/index.js | 1 + services/gunDB/contact-api/getters/wall.js | 60 +++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 services/gunDB/contact-api/getters/wall.js diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index 54d80fd5..5074b5dd 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -89,3 +89,4 @@ const getMyUser = async () => { module.exports.getMyUser = getMyUser module.exports.Follows = require('./follows') +module.exports.Wall = require('./wall') diff --git a/services/gunDB/contact-api/getters/wall.js b/services/gunDB/contact-api/getters/wall.js new file mode 100644 index 00000000..50824099 --- /dev/null +++ b/services/gunDB/contact-api/getters/wall.js @@ -0,0 +1,60 @@ +/** + * @format + */ +const Common = require('shock-common') +const Utils = require('../utils') +const Key = require('../key') + +/** + * @returns {Promise} + */ +const getTotalPages = () => + /** @type {Promise} */ (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} + */ +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 +}