log
This commit is contained in:
parent
f96114b7fc
commit
3d685eb714
1 changed files with 80 additions and 67 deletions
|
|
@ -120,6 +120,8 @@ const notifyIncomingsListeners = () => {
|
||||||
/** @type {Set<string>} */
|
/** @type {Set<string>} */
|
||||||
const pubFeedPairsWithIncomingListeners = new Set()
|
const pubFeedPairsWithIncomingListeners = new Set()
|
||||||
|
|
||||||
|
let subbed = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {IncomingsListener} cb
|
* @param {IncomingsListener} cb
|
||||||
*/
|
*/
|
||||||
|
|
@ -129,81 +131,92 @@ const onIncoming = cb => {
|
||||||
const user = require('../../Mediator').getUser()
|
const user = require('../../Mediator').getUser()
|
||||||
const SEA = require('../../Mediator').mySEA
|
const SEA = require('../../Mediator').mySEA
|
||||||
|
|
||||||
user.get(Key.USER_TO_INCOMING).open(uti => {
|
if (!subbed) {
|
||||||
if (typeof uti !== 'object' || uti === null) {
|
user.get(Key.USER_TO_INCOMING).open(uti => {
|
||||||
return
|
if (typeof uti !== 'object' || uti === null) {
|
||||||
}
|
|
||||||
|
|
||||||
Object.entries(uti).forEach(async ([pub, encFeed]) => {
|
|
||||||
if (typeof encFeed !== 'string') {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const ourSecret = await SEA.secret(await Utils.pubToEpub(pub), user._.sea)
|
|
||||||
const mySecret = await Utils.mySecret()
|
|
||||||
|
|
||||||
const feed = await SEA.decrypt(encFeed, mySecret)
|
Object.entries(uti).forEach(async ([pub, encFeed]) => {
|
||||||
|
if (typeof encFeed !== 'string') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const ourSecret = await SEA.secret(
|
||||||
|
await Utils.pubToEpub(pub),
|
||||||
|
user._.sea
|
||||||
|
)
|
||||||
|
const mySecret = await Utils.mySecret()
|
||||||
|
|
||||||
if (pubFeedPairsWithIncomingListeners.add(pub + '--' + feed)) {
|
const feed = await SEA.decrypt(encFeed, mySecret)
|
||||||
require('../../Mediator')
|
|
||||||
.getGun()
|
|
||||||
.user(pub)
|
|
||||||
.get(Key.OUTGOINGS)
|
|
||||||
.get(feed)
|
|
||||||
.open(async data => {
|
|
||||||
if (data === null) {
|
|
||||||
pubToIncoming[pub] = null
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof data !== 'object') {
|
if (pubFeedPairsWithIncomingListeners.add(pub + '--' + feed)) {
|
||||||
return
|
require('../../Mediator')
|
||||||
}
|
.getGun()
|
||||||
|
.user(pub)
|
||||||
if (typeof data.with !== 'string') {
|
.get(Key.OUTGOINGS)
|
||||||
return
|
.get(feed)
|
||||||
}
|
.open(async data => {
|
||||||
|
if (data === null) {
|
||||||
if (typeof data.messages !== 'object') {
|
pubToIncoming[pub] = null
|
||||||
return
|
return
|
||||||
}
|
|
||||||
|
|
||||||
if (data.messages === null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const msgs = /** @type {[string, Schema.Message][]} */ (Object.entries(
|
|
||||||
data.messages
|
|
||||||
).filter(([_, msg]) => Schema.isMessage(msg)))
|
|
||||||
|
|
||||||
// eslint-disable-next-line require-atomic-updates
|
|
||||||
pubToIncoming[pub] = await Utils.asyncMap(
|
|
||||||
msgs,
|
|
||||||
async ([msgid, msg]) => {
|
|
||||||
let decryptedBody = ''
|
|
||||||
|
|
||||||
if (msg.body === INITIAL_MSG) {
|
|
||||||
decryptedBody = INITIAL_MSG
|
|
||||||
} else {
|
|
||||||
decryptedBody = await SEA.decrypt(msg.body, ourSecret)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {Schema.ChatMessage} */
|
|
||||||
const finalMsg = {
|
|
||||||
body: decryptedBody,
|
|
||||||
id: msgid,
|
|
||||||
outgoing: false,
|
|
||||||
timestamp: msg.timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
return finalMsg
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
notifyIncomingsListeners()
|
if (typeof data !== 'object') {
|
||||||
})
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof data.with !== 'string') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof data.messages !== 'object') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.messages === null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgs = /** @type {[string, Schema.Message][]} */ (Object.entries(
|
||||||
|
data.messages
|
||||||
|
).filter(([_, msg]) => Schema.isMessage(msg)))
|
||||||
|
|
||||||
|
// eslint-disable-next-line require-atomic-updates
|
||||||
|
pubToIncoming[pub] = await Utils.asyncMap(
|
||||||
|
msgs,
|
||||||
|
async ([msgid, msg]) => {
|
||||||
|
let decryptedBody = ''
|
||||||
|
|
||||||
|
if (msg.body === INITIAL_MSG) {
|
||||||
|
decryptedBody = INITIAL_MSG
|
||||||
|
} else {
|
||||||
|
decryptedBody = await SEA.decrypt(msg.body, ourSecret)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {Schema.ChatMessage} */
|
||||||
|
const finalMsg = {
|
||||||
|
body: decryptedBody,
|
||||||
|
id: msgid,
|
||||||
|
outgoing: false,
|
||||||
|
timestamp: msg.timestamp
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalMsg
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
console.log('--------------------------------')
|
||||||
|
console.log(`msgs: ${JSON.stringify(msgs)}`)
|
||||||
|
console.log('--------------------------------')
|
||||||
|
|
||||||
|
notifyIncomingsListeners()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
subbed = true
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
incomingsListeners.delete(cb)
|
incomingsListeners.delete(cb)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue