follows getter

This commit is contained in:
Daniel Lugo 2020-06-03 10:19:05 -04:00
parent dba537e9b0
commit 154f9e3a10

View file

@ -1,6 +1,12 @@
/**
* @format
*/
const Common = require('shock-common')
/**
* @typedef {import('shock-common').Schema.Follow} Follow
*/
const Logger = require('winston')
const Key = require('./key')
const Utils = require('./utils')
@ -38,3 +44,45 @@ exports.userToIncomingID = async pub => {
return null
}
/**
* @returns {Promise<Record<string, Follow>>}
*/
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<string, Follow>} */ (p))
})
})
}