diff --git a/services/gunDB/contact-api/getters/follows.js b/services/gunDB/contact-api/getters/follows.js new file mode 100644 index 00000000..b74080fa --- /dev/null +++ b/services/gunDB/contact-api/getters/follows.js @@ -0,0 +1,50 @@ +const Common = require('shock-common') +const Logger = require('winston') + +const Key = require('../key') + +/** + * @typedef {Common.Schema.Follow} Follow + */ + +/** + * @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 + } + + const rejected = Object.entries(data).filter( + ([_, follow]) => !Common.Schema.isFollow(follow) + ) + + 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 + )}.` + ) + } + }) + + const passed = Object.entries(data).filter(([_, follow]) => + Common.Schema.isFollow(follow) + ) + + const p = /** @type {unknown} */ (passed) + + res(/** @type {Record} */ (p)) + }) + }) +} \ No newline at end of file diff --git a/services/gunDB/contact-api/getters/index.js b/services/gunDB/contact-api/getters/index.js index fa724cbc..9b33cb92 100644 --- a/services/gunDB/contact-api/getters/index.js +++ b/services/gunDB/contact-api/getters/index.js @@ -1,12 +1,6 @@ /** * @format */ -const Common = require('shock-common') -const Logger = require('winston') -/** - * @typedef {import('shock-common').Schema.Follow} Follow - */ - const Key = require('../key') const Utils = require('../utils') @@ -45,44 +39,4 @@ exports.userToIncomingID = async pub => { return null } -/** - * @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 - } - - const rejected = Object.entries(data).filter( - ([_, follow]) => !Common.Schema.isFollow(follow) - ) - - 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 - )}.` - ) - } - }) - - const passed = Object.entries(data).filter(([_, follow]) => - Common.Schema.isFollow(follow) - ) - - const p = /** @type {unknown} */ (passed) - - res(/** @type {Record} */ (p)) - }) - }) -} +module.exports.Follows = require('./follows')