From 64184678874064e6548e37f5250725af83e4eb28 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 28 Dec 2022 14:27:18 +0200 Subject: [PATCH] reply ordering, simpler sub code --- src/js/Nostr.ts | 40 ++++++++---------------------- src/js/components/PublicMessage.js | 3 +-- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/js/Nostr.ts b/src/js/Nostr.ts index 533e1f9e..12c174d8 100644 --- a/src/js/Nostr.ts +++ b/src/js/Nostr.ts @@ -211,44 +211,24 @@ export default { subscribeToAuthors: debounce((_this) => { console.log('subscribe to', Array.from(_this.subscribedUsers)); for (const relay of _this.relays.values()) { - const go = () => { - // first sub to profiles, then everything else - const sub = relay.sub([{ kinds: [0, 3], authors: Array.from(_this.subscribedUsers) }], {}); + // first sub to profiles, then everything else + const sub = relay.sub([{ kinds: [0, 3], authors: Array.from(_this.subscribedUsers) }], {}); + // TODO update relay lastSeen + sub.on('event', (event) => _this.handleEvent(event)); + setTimeout(() => { + const sub2 = relay.sub([{ authors: Array.from(_this.subscribedUsers), limit: 40000 }], {}); // TODO update relay lastSeen - sub.on('event', (event) => _this.handleEvent(event)); - setTimeout(() => { - const sub2 = relay.sub([{ authors: Array.from(_this.subscribedUsers), limit: 20000 }], {}); - // TODO update relay lastSeen - sub2.on('event', (event) => _this.handleEvent(event)); - }, 500); - }; - const status = getRelayStatus(relay); - if (status === 0) { - relay.on('connect', () => { - go(); - }); - } else if (status === 1) { - go(); - } + sub2.on('event', (event) => _this.handleEvent(event)); + }, 500); } }, 1000), subscribeToPosts: debounce((_this) => { if (_this.subscribedPosts.size === 0) return; console.log('subscribe to posts', Array.from(_this.subscribedPosts)); for (const relay of _this.relays.values()) { - const go = () => { const sub = relay.sub([{ ids: Array.from(_this.subscribedPosts) }], {}); // TODO update relay lastSeen sub.on('event', (event) => _this.handleEvent(event)); - }; - const status = getRelayStatus(relay); - if (status === 0) { - relay.on('connect', () => { - go(); - }); - } else if (status === 1) { - go(); - } } }, 1000), subscribe: function (filters: Filter[], cb: Function | undefined) { @@ -290,6 +270,7 @@ export default { return count; }, manageRelays: function () { + // TODO keep track of subscriptions and send them to new relays const go = () => { const relays: Array = Array.from(this.relays.values()); // ws status codes: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent @@ -338,7 +319,8 @@ export default { } this.directRepliesByMessageId.get(replyingTo)?.add(event.id); - const repliedMsgs = event.tags.filter((tag) => tag[0] === 'e').map(tag => tag[1]); + // are boost messages screwing this up? + const repliedMsgs = event.tags.filter((tag) => tag[0] === 'e').map(tag => tag[1]).slice(0,2); for (const id of repliedMsgs) { if (!this.threadRepliesByMessageId.has(id)) { this.threadRepliesByMessageId.set(id, new Set()); diff --git a/src/js/components/PublicMessage.js b/src/js/components/PublicMessage.js index 91567108..1616eefd 100644 --- a/src/js/components/PublicMessage.js +++ b/src/js/components/PublicMessage.js @@ -37,7 +37,6 @@ class PublicMessage extends Message { super(); this.i = 0; this.likedBy = new Set(); - this.subscribedReplies = new Set(); this.state = { sortedReplies: [] }; } @@ -157,7 +156,7 @@ class PublicMessage extends Message { if (nostrId) { Nostr.getRepliesAndLikes(nostrId, (replies, likes, threadReplyCount) => { this.likedBy = new Set(likes); - const sortedReplies = replies && Array.from(replies).sort((a, b) => a.time - b.time); + const sortedReplies = replies && Array.from(replies).sort((a, b) => Nostr.messagesById.get(a)?.time - Nostr.messagesById.get(b)?.time); this.setState({ likes: this.likedBy.size, liked: this.likedBy.has(myPub),