@@ -52,27 +52,16 @@ export class ReadStateManager {
5252 private relayClient : RelayClient ;
5353 private clientId : string ;
5454 private slotId : string ;
55-
56- // Effective merged state across all blobs
5755 private effectiveState = new Map < string , number > ( ) ;
58-
59- // Contexts this client is allowed to include in its published blob. This
60- // excludes first-load local baselines for channels the user has not opened.
6156 private publishableContextIds = new Set < string > ( ) ;
62-
63- // Last-published blob content (for diff to suppress no-op publishes)
6457 private lastPublishedContexts : Record < string , number > = { } ;
6558 private debounceTimer : number | null = null ;
6659 private listeners = new Set < ( ) => void > ( ) ;
6760 private unsubscribeLive : ( ( ) => void ) | null = null ;
6861 private initialized = false ;
69-
70- // Track the max created_at seen from fetched blobs so publishes can satisfy
71- // NIP-RS clock-skew monotonicity for our d-tag coordinate.
7262 private maxFetchedCreatedAt = 0 ;
73-
74- // Contexts rolled back by mark-unread, protected from merge until next publish.
7563 private forcedContexts = new Set < string > ( ) ;
64+ private contextSourceCreatedAt = new Map < string , number > ( ) ;
7665
7766 constructor ( pubkey : string , relayClient : RelayClient ) {
7867 this . pubkey = pubkey ;
@@ -103,16 +92,17 @@ export class ReadStateManager {
10392 markContextRead ( contextId : string , unixTimestamp : number ) : void {
10493 this . forcedContexts . delete ( contextId ) ;
10594 this . advanceContext ( contextId , unixTimestamp , { publishable : true } ) ;
95+ this . contextSourceCreatedAt . set (
96+ contextId ,
97+ Math . max ( Math . floor ( Date . now ( ) / 1_000 ) , this . maxFetchedCreatedAt + 1 ) ,
98+ ) ;
10699 }
107100
108101 seedContextRead ( contextId : string , unixTimestamp : number ) : void {
109102 this . advanceContext ( contextId , unixTimestamp , { publishable : false } ) ;
110103 }
111104
112105 markContextUnread ( contextId : string , lastMessageUnix : number ) : void {
113- // Roll back the read timestamp to just before the last message so the
114- // channel appears unread. This is published via NIP-RS and syncs across
115- // devices.
116106 const rollbackTo = lastMessageUnix - 1 ;
117107 this . effectiveState . set ( contextId , rollbackTo ) ;
118108 this . publishableContextIds . add ( contextId ) ;
@@ -128,7 +118,6 @@ export class ReadStateManager {
128118 options : { publishable : boolean } ,
129119 ) : void {
130120 const current = this . effectiveState . get ( contextId ) ?? 0 ;
131- // Monotonic: only advance, never lower
132121 if ( unixTimestamp <= current ) {
133122 if ( ! options . publishable || this . publishableContextIds . has ( contextId ) ) {
134123 return ;
@@ -203,16 +192,13 @@ export class ReadStateManager {
203192 let ownBlobCreatedAt = 0 ;
204193
205194 for ( const event of events ) {
206- // Only process our own events
207195 if ( event . pubkey !== this . pubkey ) continue ;
208196
209- // Validate d tag (exactly one, with correct prefix)
210197 const dTags = event . tags . filter ( ( t ) => t [ 0 ] === "d" ) ;
211198 if ( dTags . length !== 1 ) continue ;
212199 const dTag = dTags [ 0 ] ;
213200 if ( ! isValidReadStateDTag ( dTag [ 1 ] ) ) continue ;
214201
215- // Validate t tag (exactly one)
216202 const tTags = event . tags . filter (
217203 ( t ) => t [ 0 ] === "t" && t [ 1 ] === "read-state" ,
218204 ) ;
@@ -237,17 +223,19 @@ export class ReadStateManager {
237223 continue ;
238224 }
239225
240- // Merge into effective state
241226 for ( const [ ctx , ts ] of Object . entries ( blob . contexts ) ) {
242227 if ( this . forcedContexts . has ( ctx ) ) continue ;
228+ const sourceCreatedAt = this . contextSourceCreatedAt . get ( ctx ) ?? 0 ;
243229 const current = this . effectiveState . get ( ctx ) ?? 0 ;
244- if ( ts > current ) {
230+ if ( event . created_at > sourceCreatedAt ) {
231+ this . effectiveState . set ( ctx , ts ) ;
232+ this . contextSourceCreatedAt . set ( ctx , event . created_at ) ;
233+ } else if ( event . created_at === sourceCreatedAt && ts > current ) {
245234 this . effectiveState . set ( ctx , ts ) ;
246235 }
247236 this . publishableContextIds . add ( ctx ) ;
248237 }
249238
250- // Track own blob (most recent event for our client_id)
251239 if ( blob . client_id === this . clientId ) {
252240 if ( event . created_at > ownBlobCreatedAt ) {
253241 ownBlob = blob ;
@@ -307,13 +295,11 @@ export class ReadStateManager {
307295 private async handleIncomingEvent ( event : RelayEvent ) : Promise < void > {
308296 if ( event . pubkey !== this . pubkey ) return ;
309297
310- // Validate d tag (exactly one, with correct prefix)
311298 const dTags = event . tags . filter ( ( t ) => t [ 0 ] === "d" ) ;
312299 if ( dTags . length !== 1 ) return ;
313300 const dTag = dTags [ 0 ] ;
314301 if ( ! isValidReadStateDTag ( dTag [ 1 ] ) ) return ;
315302
316- // Validate t tag (exactly one)
317303 const tTags = event . tags . filter (
318304 ( t ) => t [ 0 ] === "t" && t [ 1 ] === "read-state" ,
319305 ) ;
@@ -338,16 +324,25 @@ export class ReadStateManager {
338324 return ;
339325 }
340326
341- // Merge into effective state
342327 let anyAdvanced = false ;
343328 for ( const [ ctx , ts ] of Object . entries ( blob . contexts ) ) {
344329 if ( this . forcedContexts . has ( ctx ) ) continue ;
330+ const sourceCreatedAt = this . contextSourceCreatedAt . get ( ctx ) ?? 0 ;
345331 const current = this . effectiveState . get ( ctx ) ?? 0 ;
346- if ( ts > current ) {
332+ if ( event . created_at > sourceCreatedAt ) {
333+ if ( this . effectiveState . get ( ctx ) !== ts ) {
334+ this . effectiveState . set ( ctx , ts ) ;
335+ anyAdvanced = true ;
336+ }
337+ this . contextSourceCreatedAt . set ( ctx , event . created_at ) ;
338+ } else if ( event . created_at === sourceCreatedAt && ts > current ) {
347339 this . effectiveState . set ( ctx , ts ) ;
348340 anyAdvanced = true ;
349341 }
350- this . publishableContextIds . add ( ctx ) ;
342+ if ( ! this . publishableContextIds . has ( ctx ) ) {
343+ this . publishableContextIds . add ( ctx ) ;
344+ anyAdvanced = true ;
345+ }
351346 }
352347
353348 if ( anyAdvanced ) {
@@ -416,6 +411,9 @@ export class ReadStateManager {
416411
417412 this . lastPublishedContexts = contexts ;
418413 this . forcedContexts . clear ( ) ;
414+ for ( const key of Object . keys ( contexts ) ) {
415+ this . contextSourceCreatedAt . set ( key , createdAt ) ;
416+ }
419417 this . maxFetchedCreatedAt = Math . max (
420418 this . maxFetchedCreatedAt ,
421419 event . created_at ,
@@ -473,6 +471,9 @@ export class ReadStateManager {
473471 for ( const contextId of stored . publishableContextIds ) {
474472 this . publishableContextIds . add ( contextId ) ;
475473 }
474+ for ( const [ contextId , createdAt ] of stored . contextSourceCreatedAt ) {
475+ this . contextSourceCreatedAt . set ( contextId , createdAt ) ;
476+ }
476477 this . persistLocalState ( ) ;
477478 }
478479
@@ -481,6 +482,7 @@ export class ReadStateManager {
481482 this . pubkey ,
482483 this . effectiveState ,
483484 this . publishableContextIds ,
485+ this . contextSourceCreatedAt ,
484486 ) ;
485487 }
486488
0 commit comments