some chat fixes

This commit is contained in:
Martti Malmi 2023-09-12 13:16:48 +03:00
parent 56b44b598a
commit 8fbde57db0
2 changed files with 36 additions and 28 deletions

View File

@ -237,7 +237,7 @@ export default class Node {
* Same as on(), but will unsubscribe after the first callback
* @param callback
*/
once(callback?: Callback, returnIfUndefined = false): Promise<any> {
once(callback?: Callback, returnIfUndefined = false, recursion = 0): Promise<any> {
return new Promise((resolve) => {
let resolved = false;
const cb = (value, updatedAt, path, unsub) => {
@ -247,7 +247,7 @@ export default class Node {
callback?.(value, updatedAt, path, () => {});
unsub();
};
this.on(cb, returnIfUndefined);
this.on(cb, returnIfUndefined, recursion);
});
}
}

View File

@ -295,35 +295,43 @@ function ChatMessages({ id }) {
localState
.get('chatInvites')
.get(chatId)
.once((invite) => {
if (invite?.priv) {
setInvitedToPriv(invite.priv);
}
});
.once(
(invite) => {
if (invite?.priv) {
setInvitedToPriv(invite.priv);
}
},
false,
1,
);
const subscribeGroup = () => {
const node = localState.get('groups').get(id);
node.on((group) => {
const privKey = group.key;
const pubKey = getPublicKey(privKey);
setKeyPair({ privKey, pubKey });
subs.push(
PubSub.subscribe({ kinds: [4], '#p': [pubKey], authors: [pubKey] }, async (event) => {
const decrypted = await nip04.decrypt(privKey, pubKey, event.content);
messages.current.set(event.id, { ...event, text: decrypted });
setSortedMessages(Array.from(messages.current.values()));
const latest = node.get('latest');
const e = await latest.once();
if (!e || !e.created_at || e.created_at < event.created_at) {
latest.put({
id: event.id,
created_at: event.created_at,
text: decrypted.slice(0, decrypted.indexOf('{')),
});
}
}),
);
});
node.on(
(group) => {
const privKey = group.key;
const pubKey = getPublicKey(privKey);
setKeyPair({ privKey, pubKey });
subs.push(
PubSub.subscribe({ kinds: [4], '#p': [pubKey], authors: [pubKey] }, async (event) => {
const decrypted = await nip04.decrypt(privKey, pubKey, event.content);
messages.current.set(event.id, { ...event, text: decrypted });
setSortedMessages(Array.from(messages.current.values()));
const latest = node.get('latest');
const e = await latest.once(undefined, false, 1);
if (!e || !e.created_at || e.created_at < event.created_at) {
latest.put({
id: event.id,
created_at: event.created_at,
text: decrypted.slice(0, decrypted.indexOf('{')),
});
}
}),
);
},
false,
1,
);
};
if (Key.toNostrHexAddress(chatId)) {