From 18a3143d8f0942ce1216f3021a796ba8be32a261 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 27 Jun 2020 18:47:45 -0400 Subject: [PATCH] circumvent gun.load() bug --- services/gunDB/contact-api/getters/follows.js | 73 +++++++++++-------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/services/gunDB/contact-api/getters/follows.js b/services/gunDB/contact-api/getters/follows.js index b74080fa..22a49333 100644 --- a/services/gunDB/contact-api/getters/follows.js +++ b/services/gunDB/contact-api/getters/follows.js @@ -1,6 +1,11 @@ +/** + * @format + */ const Common = require('shock-common') const Logger = require('winston') +const size = require('lodash/size') +const Utils = require('../utils') const Key = require('../key') /** @@ -8,43 +13,47 @@ const Key = require('../key') */ /** + * @throws {TypeError} * @returns {Promise>} */ -exports.currentFollows = () => { - const user = require('../../Mediator').getUser() - - return new Promise(res => { - user.get(Key.FOLLOWS).load(data => { - if (typeof data !== 'object' || data === null) { - Logger.warn( - `GunDb -> getters -> currentFollows() -> Current follows data as fetched from gun is not an object but ${typeof data}. This can happen if Gun lost its cache and it's still retrieving data from the network.` - ) - res({}) - return +exports.currentFollows = async () => { + /** + * @type {Record} + */ + const raw = await Utils.tryAndWait( + // @ts-ignore + (_, user) => new Promise(res => user.get(Key.FOLLOWS).load(res)), + v => { + if (typeof v !== 'object' || v === null) { + return true } - const rejected = Object.entries(data).filter( - ([_, follow]) => !Common.Schema.isFollow(follow) - ) + if (size(v) === 0) { + return true + } - rejected.forEach(([key, item]) => { - // expected when unfollowed - if (item !== null) { - Logger.warn( - `GunDb -> getters -> currentFollows() -> Item not conforming to schema found inside follows data. Key: ${key}, item: ${JSON.stringify( - item - )}.` - ) - } - }) + return false + } + ) - const passed = Object.entries(data).filter(([_, follow]) => - Common.Schema.isFollow(follow) - ) + if (typeof raw !== 'object' || raw === null) { + Logger.error( + `Expected user.follows to be an object but instead got: ${JSON.stringify( + raw + )}` + ) + throw new TypeError('Could not get follows, not an object') + } - const p = /** @type {unknown} */ (passed) + const clean = { + ...raw + } - res(/** @type {Record} */ (p)) - }) - }) -} \ No newline at end of file + for (const [key, followOrNull] of Object.entries(clean)) { + if (!Common.Schema.isFollow(followOrNull)) { + delete clean[key] + } + } + + return clean +}