@@ -648,6 +648,7 @@ export default class MessageSyncService {
648648 sync_off_reason TEXT,
649649 membership TEXT,
650650 membership_checked_at INTEGER,
651+ sync_by_user INTEGER,
651652 created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
652653 updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
653654 );
@@ -711,6 +712,9 @@ export default class MessageSyncService {
711712 // 'gone' or 'unknown'), and when (ms).
712713 this . _ensureChannelColumn ( 'membership' , 'TEXT' ) ;
713714 this . _ensureChannelColumn ( 'membership_checked_at' , 'INTEGER' ) ;
715+ // 1 when the user asked for the chat (channels watch, backfill --chat): a public
716+ // channel can be archived without joining it, and Telegram calls that left.
717+ this . _ensureChannelColumn ( 'sync_by_user' , 'INTEGER' ) ;
714718 if ( this . _ensureChannelColumn ( 'newer_sync_pending' , 'INTEGER NOT NULL DEFAULT 0' ) ) {
715719 // Jobs queued before this column existed still have their newer sync ahead of them.
716720 this . db . prepare ( `
@@ -1409,12 +1413,23 @@ export default class MessageSyncService {
14091413 // cannot keep up with it. Missing from the dialog list is only a reason to ask: sync goes
14101414 // off only when Telegram says the account left or was removed, or the chat is gone. One
14111415 // still joined keeps its sync and is asked about again a day later. Private chats are not
1412- // asked about: their messages arrive without a dialog.
1413- async checkChatsMissingFromDialogs ( ) {
1416+ // asked about: their messages arrive without a dialog. Nor are chats the user asked for.
1417+ checkChatsMissingFromDialogs ( ) {
1418+ // Two comparisons close together share one round of questions.
1419+ if ( ! this . membershipCheckPromise ) {
1420+ this . membershipCheckPromise = this . _checkChatsMissingFromDialogs ( ) . finally ( ( ) => {
1421+ this . membershipCheckPromise = null ;
1422+ } ) ;
1423+ }
1424+ return this . membershipCheckPromise ;
1425+ }
1426+
1427+ async _checkChatsMissingFromDialogs ( ) {
14141428 const dueBefore = Date . now ( ) - MEMBERSHIP_RECHECK_MS ;
14151429 const missing = this . db . prepare ( `
14161430 SELECT channel_id FROM channels
1417- WHERE sync_enabled = 1 AND in_dialogs = 0 AND channel_id GLOB '-[0-9]*'
1431+ WHERE sync_enabled = 1 AND in_dialogs = 0 AND COALESCE(sync_by_user, 0) = 0
1432+ AND channel_id GLOB '-[0-9]*' AND channel_id NOT GLOB '-*[^0-9]*'
14181433 AND (membership_checked_at IS NULL OR membership_checked_at < ?)
14191434 ORDER BY channel_id
14201435 ` ) . all ( dueBefore ) . map ( ( row ) => row . channel_id ) ;
@@ -1428,7 +1443,7 @@ export default class MessageSyncService {
14281443 UPDATE channels
14291444 SET sync_enabled = 0, sync_off_reason = 'left', membership = ?, membership_checked_at = ?,
14301445 updated_at = CURRENT_TIMESTAMP
1431- WHERE channel_id = ? AND sync_enabled = 1 AND in_dialogs = 0
1446+ WHERE channel_id = ? AND sync_enabled = 1 AND in_dialogs = 0 AND COALESCE(sync_by_user, 0) = 0
14321447 ` ) ;
14331448 const checked = this . db . prepare ( 'UPDATE channels SET membership = ?, membership_checked_at = ? WHERE channel_id = ?' ) ;
14341449 const stillJoined = [ ] ;
@@ -1460,9 +1475,14 @@ export default class MessageSyncService {
14601475 if ( ! dialogs . length ) {
14611476 return ;
14621477 }
1478+ // A chat back in the list says nothing more about whether the account is still in it
1479+ // later, so what Telegram said while it was missing goes.
14631480 this . db . prepare ( `
1481+ WITH listed AS (SELECT value AS channel_id FROM json_each(?))
14641482 UPDATE channels
1465- SET in_dialogs = channel_id IN (SELECT value FROM json_each(?))
1483+ SET in_dialogs = channel_id IN (SELECT channel_id FROM listed),
1484+ membership = CASE WHEN channel_id IN (SELECT channel_id FROM listed) THEN NULL ELSE membership END,
1485+ membership_checked_at = CASE WHEN channel_id IN (SELECT channel_id FROM listed) THEN NULL ELSE membership_checked_at END
14661486 ` ) . run ( JSON . stringify ( dialogs . map ( ( dialog ) => String ( dialog . id ) ) ) ) ;
14671487 }
14681488
@@ -1628,18 +1648,19 @@ export default class MessageSyncService {
16281648 const normalizedId = normalizeChannelKey ( channelId ) ;
16291649 const value = enabled ? 1 : 0 ;
16301650 const stmt = this . db . prepare ( `
1631- INSERT INTO channels (channel_id, sync_enabled, updated_at)
1632- VALUES (?, ?, CURRENT_TIMESTAMP)
1651+ INSERT INTO channels (channel_id, sync_enabled, sync_by_user, updated_at)
1652+ VALUES (?, ?, ?, CURRENT_TIMESTAMP)
16331653 ON CONFLICT(channel_id) DO UPDATE SET
16341654 sync_enabled = excluded.sync_enabled,
1655+ sync_by_user = excluded.sync_by_user,
16351656 sync_off_reason = NULL,
16361657 membership = NULL,
16371658 membership_checked_at = NULL,
16381659 updated_at = CURRENT_TIMESTAMP
16391660 RETURNING channel_id, sync_enabled;
16401661 ` ) ;
16411662
1642- return stmt . get ( normalizedId , value ) ;
1663+ return stmt . get ( normalizedId , value , value ) ;
16431664 }
16441665
16451666 setChannelTags ( channelId , tags , options = { } ) {
@@ -2138,12 +2159,16 @@ export default class MessageSyncService {
21382159 const minDateIso = minDate ? new Date ( minDate * 1000 ) . toISOString ( ) : null ;
21392160
21402161 // Stays set until a newer sync reaches the newest message, even if this job is canceled
2141- // with a cursor in place: what lies above that cursor is still to be fetched.
2162+ // with a cursor in place: what lies above that cursor is still to be fetched. Asking for
2163+ // the chat is the user's word on it, over the server turning its sync off.
21422164 this . db . prepare ( `
2143- INSERT INTO channels (channel_id, newer_sync_pending, updated_at)
2144- VALUES (?, 1, CURRENT_TIMESTAMP)
2165+ INSERT INTO channels (channel_id, newer_sync_pending, sync_by_user, updated_at)
2166+ VALUES (?, 1, 1, CURRENT_TIMESTAMP)
21452167 ON CONFLICT(channel_id) DO UPDATE SET
21462168 newer_sync_pending = 1,
2169+ sync_by_user = 1,
2170+ sync_enabled = CASE WHEN sync_off_reason = 'left' THEN 1 ELSE sync_enabled END,
2171+ sync_off_reason = NULL,
21472172 updated_at = CURRENT_TIMESTAMP
21482173 ` ) . run ( normalizedId ) ;
21492174
@@ -2748,9 +2773,6 @@ export default class MessageSyncService {
27482773 return channelIds ;
27492774 }
27502775
2751- // Archives written before 2.2.5 can hold holes nothing fills by itself. Finding them
2752- // costs as much as filling them, so this only says whether `--all` has run, or the
2753- // archive was started by a version with the fixes.
27542776 // What the server did about synced chats missing from the dialog list.
27552777 getLeftChatCounts ( ) {
27562778 return this . db . prepare ( `
@@ -2761,6 +2783,9 @@ export default class MessageSyncService {
27612783 ` ) . get ( ) ;
27622784 }
27632785
2786+ // Archives written before 2.2.5 can hold holes nothing fills by itself. Finding them
2787+ // costs as much as filling them, so this only says whether `--all` has run, or the
2788+ // archive was started by a version with the fixes.
27642789 isRepairSuggested ( ) {
27652790 if ( this . db . prepare ( "SELECT 1 FROM search_meta WHERE key = 'archive_repair'" ) . get ( ) ) {
27662791 return false ;
0 commit comments