Merge pull request #97 from shocknet/follows-init-hotfix

ensure follows sub graph is always initialized and non-empty
This commit is contained in:
Daniel Lugo 2020-06-27 20:31:09 -04:00 committed by GitHub
commit 5bb5d7855f
2 changed files with 55 additions and 8 deletions

View file

@ -12,6 +12,8 @@ require('gun/lib/load')
const debounce = require('lodash/debounce') const debounce = require('lodash/debounce')
const Encryption = require('../../../utils/encryptionStore') const Encryption = require('../../../utils/encryptionStore')
const Key = require('../contact-api/key')
/** @type {import('../contact-api/SimpleGUN').ISEA} */ /** @type {import('../contact-api/SimpleGUN').ISEA} */
// @ts-ignore // @ts-ignore
const SEAx = require('gun/sea') const SEAx = require('gun/sea')
@ -267,6 +269,21 @@ const authenticate = async (alias, pass, __user) => {
if (typeof ack.err === 'string') { if (typeof ack.err === 'string') {
throw new Error(ack.err) throw new Error(ack.err)
} else if (typeof ack.sea === 'object') { } else if (typeof ack.sea === 'object') {
await new Promise((res, rej) => {
_user.get(Key.FOLLOWS).put(
{
unused: null
},
ack => {
if (ack.err) {
rej(new Error(`Error initializing follows: ${ack.err}`))
} else {
res()
}
}
)
})
return ack.sea.pub return ack.sea.pub
} else { } else {
throw new Error('Unknown error.') throw new Error('Unknown error.')
@ -279,6 +296,22 @@ const authenticate = async (alias, pass, __user) => {
`Tried to re-authenticate with an alias different to that of stored one, tried: ${alias} - stored: ${_currentAlias}, logoff first if need to change aliases.` `Tried to re-authenticate with an alias different to that of stored one, tried: ${alias} - stored: ${_currentAlias}, logoff first if need to change aliases.`
) )
} }
await new Promise((res, rej) => {
_user.get(Key.FOLLOWS).put(
{
unused: null
},
ack => {
if (ack.err) {
rej(new Error(`Error initializing follows: ${ack.err}`))
} else {
res()
}
}
)
})
// move this to a subscription; implement off() ? todo // move this to a subscription; implement off() ? todo
API.Jobs.onAcceptedRequests(_user, mySEA) API.Jobs.onAcceptedRequests(_user, mySEA)
API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.onOrders(_user, gun, mySEA)
@ -312,6 +345,21 @@ const authenticate = async (alias, pass, __user) => {
await new Promise(res => setTimeout(res, 5000)) await new Promise(res => setTimeout(res, 5000))
await new Promise((res, rej) => {
_user.get(Key.FOLLOWS).put(
{
unused: null
},
ack => {
if (ack.err) {
rej(new Error(`Error initializing follows: ${ack.err}`))
} else {
res()
}
}
)
})
API.Jobs.onAcceptedRequests(_user, mySEA) API.Jobs.onAcceptedRequests(_user, mySEA)
API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.onOrders(_user, gun, mySEA)
API.Jobs.lastSeenNode(_user) API.Jobs.lastSeenNode(_user)

View file

@ -1906,12 +1906,11 @@ module.exports = async (
/** /**
* @type {RequestHandler<FollowsRouteParams>} * @type {RequestHandler<FollowsRouteParams>}
*/ */
const apiGunFollowsGet = (_, res) => { const apiGunFollowsGet = async (_, res) => {
try { try {
// const { publicKey } = req.params; const currFollows = await GunGetters.Follows.currentFollows()
// const currFollows = await GunGetters.currentFollows()
return res.status(200).json({}) return res.status(200).json(currFollows)
} catch (err) { } catch (err) {
return res.status(500).json({ return res.status(500).json({
errorMessage: err.message || 'Unknown ERR at GET /api/follows' errorMessage: err.message || 'Unknown ERR at GET /api/follows'
@ -1923,14 +1922,14 @@ module.exports = async (
/** /**
* @type {RequestHandler<FollowsRouteParams>} * @type {RequestHandler<FollowsRouteParams>}
*/ */
const apiGunFollowsPut = (req, res) => { const apiGunFollowsPut = async (req, res) => {
try { try {
const { publicKey } = req.params; const { publicKey } = req.params;
if (!publicKey) { if (!publicKey) {
throw new Error(`Missing publicKey route param.`) throw new Error(`Missing publicKey route param.`)
} }
// await GunActions.follow(req.params.publicKey, false) await GunActions.follow(req.params.publicKey, false)
// 201 would be extraneous here. Implement it inside app.put // 201 would be extraneous here. Implement it inside app.put
return res.status(200).json({ return res.status(200).json({
@ -1946,14 +1945,14 @@ module.exports = async (
/** /**
* @type {RequestHandler<FollowsRouteParams>} * @type {RequestHandler<FollowsRouteParams>}
*/ */
const apiGunFollowsDelete = (req, res) => { const apiGunFollowsDelete = async (req, res) => {
try { try {
const { publicKey } = req.params; const { publicKey } = req.params;
if (!publicKey) { if (!publicKey) {
throw new Error(`Missing publicKey route param.`) throw new Error(`Missing publicKey route param.`)
} }
// await GunActions.unfollow(req.params.publicKey) await GunActions.unfollow(req.params.publicKey)
return res.status(200).json({ return res.status(200).json({
ok: true ok: true