refined outgoing handling

This commit is contained in:
Daniel Lugo 2020-02-04 00:09:09 -04:00
parent 1f0ca74779
commit 92a7ef0894

View file

@ -323,8 +323,7 @@ const notifyOutgoingsListeners = () => {
outgoingsListeners.forEach(l => l(currentOutgoings)) outgoingsListeners.forEach(l => l(currentOutgoings))
} }
/** @type {UserGUNNode|null} */ let outSubbed = false
let lastUserWithListener = null
/** /**
* @param {OutgoingsListener} cb * @param {OutgoingsListener} cb
@ -334,88 +333,92 @@ const onOutgoing = cb => {
outgoingsListeners.add(cb) outgoingsListeners.add(cb)
cb(currentOutgoings) cb(currentOutgoings)
const currentUser = require('../../Mediator').getUser() if (!outSubbed) {
const user = require('../../Mediator').getUser()
if (lastUserWithListener !== currentUser) { user.get(Key.OUTGOINGS).open(
// in case user changed gun alias debounce(async data => {
currentOutgoings = {} try {
lastUserWithListener = currentUser if (typeof data !== 'object' || data === null) {
currentOutgoings = {}
currentUser.get(Key.OUTGOINGS).open(async data => { notifyOutgoingsListeners()
// deactivate this listener when user changes return
if (lastUserWithListener !== require('../../Mediator').getUser()) {
return
}
if (typeof data !== 'object' || data === null) {
currentOutgoings = {}
notifyOutgoingsListeners()
return
}
/** @type {Record<string, Outgoing>} */
const newOuts = {}
const SEA = require('../../Mediator').mySEA
const mySecret = await Utils.mySecret()
await Utils.asyncForEach(Object.entries(data), async ([id, out]) => {
if (typeof out !== 'object') {
return
}
if (out === null) {
// @ts-ignore
newOuts[id] = null
return
}
const { with: encPub, messages } = out
if (typeof encPub !== 'string') {
return
}
const pub = await SEA.decrypt(encPub, mySecret)
if (!newOuts[id]) {
newOuts[id] = {
with: pub,
messages: {}
} }
}
const ourSec = await SEA.secret( /** @type {Record<string, Outgoing|null>} */
await Utils.pubToEpub(pub), const newOuts = {}
require('../../Mediator').getUser()._.sea
)
if (typeof messages === 'object' && messages !== null) { const SEA = require('../../Mediator').mySEA
await Utils.asyncForEach( const mySecret = await Utils.mySecret()
Object.entries(messages),
async ([mid, msg]) => { await Utils.asyncForEach(Object.entries(data), async ([id, out]) => {
if (typeof msg === 'object' && msg !== null) { if (typeof out !== 'object') {
if ( return
typeof msg.body === 'string' && }
typeof msg.timestamp === 'number'
) { if (out === null) {
newOuts[id].messages[mid] = { newOuts[id] = null
body: return
msg.body === Actions.INITIAL_MSG }
? Actions.INITIAL_MSG
: await SEA.decrypt(msg.body, ourSec), const { with: encPub, messages } = out
timestamp: msg.timestamp
} if (typeof encPub !== 'string') {
} return
}
const pub = await SEA.decrypt(encPub, mySecret)
if (!newOuts[id]) {
newOuts[id] = {
with: pub,
messages: {}
} }
} }
)
}
})
currentOutgoings = newOuts const ourSec = await SEA.secret(
notifyOutgoingsListeners() await Utils.pubToEpub(pub),
}) user._.sea
)
if (typeof messages === 'object' && messages !== null) {
await Utils.asyncForEach(
Object.entries(messages),
async ([mid, msg]) => {
if (typeof msg === 'object' && msg !== null) {
if (
typeof msg.body === 'string' &&
typeof msg.timestamp === 'number'
) {
const newOut = newOuts[id]
if (!newOut) {
return
}
newOut.messages[mid] = {
body:
msg.body === Actions.INITIAL_MSG
? Actions.INITIAL_MSG
: await SEA.decrypt(msg.body, ourSec),
timestamp: msg.timestamp
}
}
}
}
)
}
})
currentOutgoings = newOuts
notifyOutgoingsListeners()
} catch (e) {
console.log('--------------------------')
console.log('Events -> onOutgoing')
console.log(e)
console.log('--------------------------')
}
}, 400)
)
outSubbed = true
} }
return () => { return () => {