tweak chats
This commit is contained in:
parent
b9abcb06cc
commit
180c485bb8
1 changed files with 35 additions and 40 deletions
|
|
@ -3,7 +3,6 @@
|
||||||
*/
|
*/
|
||||||
const debounce = require('lodash/debounce')
|
const debounce = require('lodash/debounce')
|
||||||
|
|
||||||
const Actions = require('../actions')
|
|
||||||
const ErrorCode = require('../errorCode')
|
const ErrorCode = require('../errorCode')
|
||||||
const Key = require('../key')
|
const Key = require('../key')
|
||||||
const Schema = require('../schema')
|
const Schema = require('../schema')
|
||||||
|
|
@ -363,9 +362,6 @@ const onOutgoing = cb => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/** @type {Outgoings} */
|
|
||||||
let outgoings = {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {(chats: Chat[]) => void} ChatsListener
|
* @typedef {(chats: Chat[]) => void} ChatsListener
|
||||||
*/
|
*/
|
||||||
|
|
@ -380,17 +376,27 @@ const notifyChatsListeners = () => {
|
||||||
chatsListeners.forEach(l => l(currentChats))
|
chatsListeners.forEach(l => l(currentChats))
|
||||||
}
|
}
|
||||||
|
|
||||||
const processChats = () => {
|
const processChats = debounce(() => {
|
||||||
const pubToAvatar = Streams.getPubToAvatar()
|
const pubToAvatar = Streams.getPubToAvatar()
|
||||||
const pubToDn = Streams.getPubToDn()
|
const pubToDn = Streams.getPubToDn()
|
||||||
const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries(
|
const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries(
|
||||||
outgoings
|
getCurrentOutgoings()
|
||||||
).filter(([_, o]) => o !== null))
|
).filter(([_, o]) => o !== null))
|
||||||
|
const pubToFeed = Streams.getPubToFeed()
|
||||||
|
|
||||||
/** @type {Chat[]} */
|
/** @type {Chat[]} */
|
||||||
const chats = []
|
const newChats = []
|
||||||
|
|
||||||
for (const [outID, out] of existingOutgoings) {
|
for (const [outID, out] of existingOutgoings) {
|
||||||
|
if (typeof pubToAvatar[out.with] === 'undefined') {
|
||||||
|
// eslint-disable-next-line no-empty-function
|
||||||
|
Streams.onAvatar(() => {}, out.with)
|
||||||
|
}
|
||||||
|
if (typeof pubToDn[out.with] === 'undefined') {
|
||||||
|
// eslint-disable-next-line no-empty-function
|
||||||
|
Streams.onDisplayName(() => {}, out.with)
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {ChatMessage[]} */
|
/** @type {ChatMessage[]} */
|
||||||
let msgs = Object.entries(out.messages).map(([mid, m]) => ({
|
let msgs = Object.entries(out.messages).map(([mid, m]) => ({
|
||||||
id: mid,
|
id: mid,
|
||||||
|
|
@ -399,17 +405,12 @@ const processChats = () => {
|
||||||
timestamp: m.timestamp
|
timestamp: m.timestamp
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const incoming = Streams.getPubToIncoming()[out.with]
|
const incoming = pubToFeed[out.with]
|
||||||
|
|
||||||
if (Array.isArray(incoming)) {
|
if (Array.isArray(incoming)) {
|
||||||
msgs = [...msgs, ...incoming]
|
msgs = [...msgs, ...incoming]
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('-------------------------------------------------')
|
|
||||||
console.log(`msgs before filtering`)
|
|
||||||
console.log(msgs)
|
|
||||||
console.log('-------------------------------------------------')
|
|
||||||
|
|
||||||
/** @type {Chat} */
|
/** @type {Chat} */
|
||||||
const chat = {
|
const chat = {
|
||||||
recipientPublicKey: out.with,
|
recipientPublicKey: out.with,
|
||||||
|
|
@ -420,26 +421,15 @@ const processChats = () => {
|
||||||
recipientDisplayName: pubToDn[out.with] || null
|
recipientDisplayName: pubToDn[out.with] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
chats.push(chat)
|
newChats.push(chat)
|
||||||
|
|
||||||
if (typeof pubToAvatar[out.with] === 'undefined') {
|
|
||||||
// eslint-disable-next-line no-empty-function
|
|
||||||
Streams.onAvatar(() => {}, out.with)
|
|
||||||
}
|
|
||||||
if (typeof pubToDn[out.with] === 'undefined') {
|
|
||||||
// eslint-disable-next-line no-empty-function
|
|
||||||
Streams.onDisplayName(() => {}, out.with)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentChats = chats
|
currentChats = newChats
|
||||||
.filter(c => c.messages.length > 0)
|
|
||||||
.filter(
|
|
||||||
c =>
|
|
||||||
typeof Streams.getPubToIncoming()[c.recipientPublicKey] !== 'undefined'
|
|
||||||
)
|
|
||||||
notifyChatsListeners()
|
notifyChatsListeners()
|
||||||
}
|
}, 750)
|
||||||
|
|
||||||
|
let onChatsSubbed = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Massages all of the more primitive data structures into a more manageable
|
* Massages all of the more primitive data structures into a more manageable
|
||||||
|
|
@ -448,20 +438,24 @@ const processChats = () => {
|
||||||
* @returns {() => void}
|
* @returns {() => void}
|
||||||
*/
|
*/
|
||||||
const onChats = cb => {
|
const onChats = cb => {
|
||||||
chatsListeners.add(cb)
|
if (!chatsListeners.add(cb)) {
|
||||||
|
throw new Error('Tried to subscribe twice')
|
||||||
|
}
|
||||||
cb(currentChats)
|
cb(currentChats)
|
||||||
|
|
||||||
onOutgoing(outs => {
|
if (!onChatsSubbed) {
|
||||||
outgoings = outs
|
onOutgoing(processChats)
|
||||||
processChats()
|
Streams.onAvatar(processChats)
|
||||||
})
|
Streams.onDisplayName(processChats)
|
||||||
|
Streams.onPubToFeed(processChats)
|
||||||
|
|
||||||
Streams.onAvatar(processChats)
|
onChatsSubbed = true
|
||||||
Streams.onDisplayName(processChats)
|
}
|
||||||
Streams.onIncoming(processChats)
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
chatsListeners.delete(cb)
|
if (!chatsListeners.delete(cb)) {
|
||||||
|
throw new Error('Tried to unsubscribe twice')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -532,5 +526,6 @@ module.exports = {
|
||||||
onSimplerSentRequests: require('./onSentReqs').onSentReqs,
|
onSimplerSentRequests: require('./onSentReqs').onSentReqs,
|
||||||
getCurrentSentReqs: require('./onSentReqs').getCurrentSentReqs,
|
getCurrentSentReqs: require('./onSentReqs').getCurrentSentReqs,
|
||||||
onBio,
|
onBio,
|
||||||
onSeedBackup
|
onSeedBackup,
|
||||||
|
onChats
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue