@@ -164,6 +164,21 @@ export function createInboxFeed({
164164 signal ,
165165 ) ;
166166 }
167+ /** Channels discovery may read: verified, current, unarchived membership. */
168+ const discoveryChannels = ( ) =>
169+ channels
170+ . list ( )
171+ . channels . filter (
172+ ( channel ) =>
173+ ! channel . huddle &&
174+ ! channel . cached &&
175+ ! channel . archived &&
176+ ! channel . metadataPending &&
177+ channel . members ?. includes ( viewer ) ,
178+ )
179+ . sort ( ( a , b ) => a . id . localeCompare ( b . id ) ) ;
180+ /** Restored channels the latest discovery skipped until verified. */
181+ let unverified : ReadonlySet < string > | undefined ;
167182 async function refresh ( ) {
168183 if ( closed ) return ;
169184 if ( work ) return work ;
@@ -207,34 +222,22 @@ export function createInboxFeed({
207222 await settle ( targets , owned . signal ) ;
208223 if ( closed || generation !== epoch ) return ;
209224 publish ( { incomplete : [ ] } ) ;
210- if ( threadHistory )
211- await discoverInboxThreads ( {
212- viewer,
213- channelIds : channels
214- . list ( )
215- . channels . filter (
216- ( channel ) =>
217- ! channel . huddle &&
218- ! channel . cached &&
219- ! channel . archived &&
220- ! channel . metadataPending &&
221- channel . members ?. includes ( viewer ) ,
222- )
223- . map ( ( channel ) => channel . id )
224- . sort ( ) ,
225- soloChannelIds : channels
225+ if ( threadHistory ) {
226+ const eligible = discoveryChannels ( ) ;
227+ unverified = new Set (
228+ channels
226229 . list ( )
227230 . channels . filter (
228- ( channel ) =>
229- channel . channelType === "solo" &&
230- ! channel . huddle &&
231- ! channel . cached &&
232- ! channel . archived &&
233- ! channel . metadataPending &&
234- channel . members ?. includes ( viewer ) ,
231+ ( channel ) => channel . cached || channel . metadataPending ,
235232 )
236- . map ( ( channel ) => channel . id )
237- . sort ( ) ,
233+ . map ( ( channel ) => channel . id ) ,
234+ ) ;
235+ await discoverInboxThreads ( {
236+ viewer,
237+ channelIds : eligible . map ( ( channel ) => channel . id ) ,
238+ soloChannelIds : eligible
239+ . filter ( ( channel ) => channel . channelType === "solo" )
240+ . map ( ( channel ) => channel . id ) ,
238241 choices : threadHistory . choices ( ) ,
239242 since : Math . floor ( Date . now ( ) / 1000 ) - INBOX_THREAD_WINDOW_SECONDS ,
240243 read : threadHistory . read ,
@@ -264,6 +267,7 @@ export function createInboxFeed({
264267 return threadHistory . incomingIds ( flatOnly ) ;
265268 } ,
266269 } ) ;
270+ }
267271 if ( closed || generation !== epoch ) return ;
268272 publish ( {
269273 status : "ready" ,
@@ -295,9 +299,38 @@ export function createInboxFeed({
295299 controller ?. abort ( ) ;
296300 controller = undefined ;
297301 work = undefined ;
302+ unverified = undefined ;
298303 // Previously demanded data recovers explicitly or through reconnect.
299304 publish ( { status : "idle" , incomplete, error : undefined } ) ;
300305 }
306+ function stale ( ) {
307+ epoch ++ ;
308+ controller ?. abort ( ) ;
309+ controller = undefined ;
310+ work = undefined ;
311+ unverified = undefined ;
312+ publish ( {
313+ status : "idle" ,
314+ incomplete : retainedIncomplete ( ) ,
315+ error : undefined ,
316+ } ) ;
317+ }
318+ function reconnect ( ) {
319+ if ( requested ) void refresh ( ) ;
320+ }
321+ // A restored channel joins discovery once verified. Demanded discovery that
322+ // skipped it renews, as a follow change does. A newly joined channel does
323+ // not: its pre-membership history needs fresh shared admission instead.
324+ const stopChannels = threadHistory
325+ ? channels . subscribeList ( ( ) => {
326+ const skipped = unverified ;
327+ if ( closed || ! skipped ?. size ) return ;
328+ if ( discoveryChannels ( ) . some ( ( channel ) => skipped . has ( channel . id ) ) ) {
329+ stale ( ) ;
330+ reconnect ( ) ;
331+ }
332+ } )
333+ : undefined ;
301334 const retainedIncomplete = ( ) =>
302335 Object . freeze ( snapshot . incomplete . filter ( ( id ) => retainedEvent ( id ) ) ) ;
303336 return Object . freeze ( {
@@ -311,24 +344,13 @@ export function createInboxFeed({
311344 ensure : ( ) =>
312345 work ?? ( snapshot . status === "idle" ? refresh ( ) : Promise . resolve ( ) ) ,
313346 refresh,
314- stale ( ) {
315- epoch ++ ;
316- controller ?. abort ( ) ;
317- controller = undefined ;
318- work = undefined ;
319- publish ( {
320- status : "idle" ,
321- incomplete : retainedIncomplete ( ) ,
322- error : undefined ,
323- } ) ;
324- } ,
325- reconnect ( ) {
326- if ( requested ) void refresh ( ) ;
327- } ,
347+ stale,
348+ reconnect,
328349 clear : ( ) => clear ( ) ,
329350 purge : ( ) => clear ( retainedIncomplete ( ) ) ,
330351 dispose ( ) {
331352 closed = true ;
353+ stopChannels ?.( ) ;
332354 epoch ++ ;
333355 controller ?. abort ( ) ;
334356 controller = undefined ;
0 commit comments