better handling of outgoings

This commit is contained in:
Daniel Lugo 2020-01-29 21:09:15 -04:00
parent 4a03cde744
commit 905cdd6f00

View file

@ -342,18 +342,21 @@ const processOutgoings = async () => {
return return
} }
const withPub = await SEA.decrypt(out.with, mySecret)
const ourSecret = await SEA.secret(
await Utils.pubToEpub(withPub),
user._.sea
)
if (!Schema.isPartialOutgoing(out)) { if (!Schema.isPartialOutgoing(out)) {
// incomplete data // incomplete data
return return
} }
if (typeof currentOutgoings[id] === 'undefined') { if (typeof currentOutgoings[id] === 'undefined') {
// We disable this rule because we are awaiting the result of the whole
// for each AND each callback looks only at one single ID
// eslint-disable-next-line require-atomic-updates
currentOutgoings[id] = { currentOutgoings[id] = {
messages: {}, messages: {},
with: await SEA.decrypt(out.with, mySecret) with: withPub
} }
} }
@ -362,30 +365,22 @@ const processOutgoings = async () => {
return return
} }
// on each open() only "messages" should change, not "with"
// also messages are non-nullable and non-editable
const ourSecret = await SEA.secret(
await Utils.pubToEpub(currentOut.with),
user._.sea
)
await Utils.asyncForEach( await Utils.asyncForEach(
Object.entries(currentOut.messages), Object.entries(currentOut.messages || {}),
async ([msgID, msg]) => { async ([msgID, msg]) => {
if (!Schema.isMessage(msg)) { if (!Schema.isMessage(msg)) {
// incomplete data // incomplete data
return return
} }
let decryptedBody = ''
if (msg.body === Actions.INITIAL_MSG) {
decryptedBody = Actions.INITIAL_MSG
} else {
decryptedBody = await SEA.decrypt(msg.body, ourSecret)
}
if (!currentOut.messages[msgID]) { if (!currentOut.messages[msgID]) {
let decryptedBody = ''
if (msg.body === Actions.INITIAL_MSG) {
decryptedBody = Actions.INITIAL_MSG
} else {
decryptedBody = await SEA.decrypt(msg.body, ourSecret)
}
// each callback only looks at one particular msgID // each callback only looks at one particular msgID
// eslint-disable-next-line require-atomic-updates
currentOut.messages[msgID] = { currentOut.messages[msgID] = {
body: decryptedBody, body: decryptedBody,
timestamp: msg.timestamp timestamp: msg.timestamp
@ -438,9 +433,6 @@ let pubToAvatar = {}
/** @type {Streams.DisplayNames} */ /** @type {Streams.DisplayNames} */
let pubToDn = {} let pubToDn = {}
/** @type {Streams.Incomings} */
let pubToIncoming = {}
/** /**
* @typedef {(chats: Chat[]) => void} ChatsListener * @typedef {(chats: Chat[]) => void} ChatsListener
*/ */
@ -472,7 +464,7 @@ const processChats = () => {
timestamp: m.timestamp timestamp: m.timestamp
})) }))
const incoming = pubToIncoming[out.with] const incoming = Streams.getPubToIncoming()[out.with]
if (Array.isArray(incoming)) { if (Array.isArray(incoming)) {
msgs = [...msgs, ...incoming] msgs = [...msgs, ...incoming]
@ -502,7 +494,10 @@ const processChats = () => {
currentChats = chats currentChats = chats
.filter(c => c.messages.length > 0) .filter(c => c.messages.length > 0)
.filter(c => typeof pubToIncoming[c.recipientPublicKey] !== 'undefined') .filter(
c =>
typeof Streams.getPubToIncoming()[c.recipientPublicKey] !== 'undefined'
)
notifyChatsListeners() notifyChatsListeners()
} }
@ -529,10 +524,7 @@ const onChats = cb => {
pubToDn = ptd pubToDn = ptd
processChats() processChats()
}) })
Streams.onIncoming(pti => { Streams.onIncoming(processChats)
pubToIncoming = pti
processChats()
})
return () => { return () => {
chatsListeners.delete(cb) chatsListeners.delete(cb)