diff --git a/js/nostr/videoEventBuffer.js b/js/nostr/videoEventBuffer.js index df6c87a5..0af08a73 100644 --- a/js/nostr/videoEventBuffer.js +++ b/js/nostr/videoEventBuffer.js @@ -130,6 +130,7 @@ export class VideoEventBuffer { } } } + updatedVideos.push(video); continue; } diff --git a/js/services/nostrService.js b/js/services/nostrService.js index ea2fb0c7..ff1d9dc5 100644 --- a/js/services/nostrService.js +++ b/js/services/nostrService.js @@ -1743,9 +1743,59 @@ export class NostrService { limit: this.nostrClient.clampVideoRequestLimit(200), }; const subscription = this.nostrClient.subscribeVideos( - () => { - const updated = this.nostrClient.getActiveVideos(); - applyAndNotify(updated, "subscription"); + (updatedVideos) => { + if (!Array.isArray(updatedVideos) || !updatedVideos.length) { + return; + } + + const activeUpdates = []; + const deletedUpdates = []; + + for (const v of updatedVideos) { + if (v.deleted) { + deletedUpdates.push(v); + } else { + activeUpdates.push(v); + } + } + + if (deletedUpdates.length > 0) { + const map = this.ensureVideosMap(); + for (const v of deletedUpdates) { + if (v.id) { + map.delete(v.id); + } + } + this.markAuthorIndexDirty(); + } + + const filtered = this.filterVideos(activeUpdates, { + blacklistedEventIds, + isAuthorBlocked, + }); + + if (filtered.length > 0) { + this.cacheVideos(filtered); + } + + if (filtered.length > 0 || deletedUpdates.length > 0) { + // Persist the full list (latest N) from the now-updated cache + const map = this.ensureVideosMap(); + const fullList = Array.from(map.values()); + fullList.sort((a, b) => { + const aCreated = Number(a?.created_at) || 0; + const bCreated = Number(b?.created_at) || 0; + return bCreated - aCreated; + }); + + this.emit("videos:updated", { + videos: fullList, + deleted: deletedUpdates, + reason: "subscription", + }); + + persistFilteredVideos(fullList); + } }, subscriptionOptions, );