follows as submodule
This commit is contained in:
parent
f375ada0c5
commit
47b2a4f010
2 changed files with 51 additions and 47 deletions
50
services/gunDB/contact-api/getters/follows.js
Normal file
50
services/gunDB/contact-api/getters/follows.js
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
const Common = require('shock-common')
|
||||||
|
const Logger = require('winston')
|
||||||
|
|
||||||
|
const Key = require('../key')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Common.Schema.Follow} Follow
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Promise<Record<string, Common.Schema.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))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
const Common = require('shock-common')
|
|
||||||
const Logger = require('winston')
|
|
||||||
/**
|
|
||||||
* @typedef {import('shock-common').Schema.Follow} Follow
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Key = require('../key')
|
const Key = require('../key')
|
||||||
const Utils = require('../utils')
|
const Utils = require('../utils')
|
||||||
|
|
||||||
|
|
@ -45,44 +39,4 @@ exports.userToIncomingID = async pub => {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
module.exports.Follows = require('./follows')
|
||||||
* @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))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue