Skip to content

Commit bdb244e

Browse files
committed
fix: address review of turning sync off for left chats
- Only a chat Telegram returns decides. A refused single-chat request (CHANNEL_INVALID, PEER_ID_INVALID, ...) can mean a stale or min access hash, so it is unknown now instead of gone. - Chats the user asked for (channels watch, backfill --chat) are never turned off: Telegram calls a public channel archived without joining left. backfill --chat turns a chat the server turned off back on. - An id mtcute cannot resolve is unknown instead of failing the whole check, and ids that are not chat ids are not asked about. - What Telegram said while a chat was missing is dropped once it is back in the dialog list. - Two comparisons close together share one round of questions.
1 parent fae3d42 commit bdb244e

5 files changed

Lines changed: 129 additions & 26 deletions

File tree

‎cli.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ async function runDoctor(globalFlags, options = {}) {
27592759
console.log(`FTS: ${payload.ftsEnabled}${payload.ftsVersion ? ` (v${payload.ftsVersion})` : ''}`);
27602760
console.log(`QUEUE: pending=${queue.pending} in_progress=${queue.in_progress} idle=${queue.idle} error=${queue.error} gaps=${queue.gaps}`);
27612761
if (leftChats.syncOff) {
2762-
console.log(`LEFT: sync is off for ${leftChats.syncOff} chat(s) you left; it comes back on by itself if you rejoin.`);
2762+
console.log(`LEFT: sync is off for ${leftChats.syncOff} chat(s) you are not in (left, removed or never joined); it comes back on if you rejoin, or for good with \`tgcli channels watch --chat <id>\`.`);
27632763
}
27642764
if (leftChats.stillJoined) {
27652765
console.log(`DIALOGS: ${leftChats.stillJoined} synced chat(s) are missing from your dialog list, though Telegram says you are still in them; sync stays on.`);

‎docs/cli.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ Update available: tgcli 2.2.5 -> 2.3.0. Run `brew update && brew upgrade tgcli`
196196
nothing from it, so the archive could not keep up. A synced chat missing from the
197197
dialog list is only a reason to ask Telegram, once a day; sync goes off when Telegram
198198
says you left or were removed, or the chat is gone, and comes back on, with what it
199-
missed, when the chat returns to your dialog list. Chats you turned off yourself stay
200-
off, and private chats are never asked about.
199+
missed, when the chat returns to your dialog list. Chats you asked for yourself
200+
(`channels watch`, `backfill --chat`) are never turned off this way, so a public
201+
channel archived without joining keeps its sync; chats you turned off stay off, and
202+
private chats are never asked about.
201203
- `channels sync --chat <id|username> --enable|--disable` remains as a hidden alias of watch/unwatch.
202204

203205
## topics (forum supergroups)

‎message-sync-service.js‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

‎telegram-client.js‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ export function isChatGone(error) {
524524
return error instanceof MtPeerNotFoundError || CHAT_GONE_ERRORS.has(error?.text);
525525
}
526526

527-
// What Telegram says about the account in a channel or group: 'member', 'left' (left on
528-
// its own), 'gone' (removed, banned, the chat deleted or migrated) or 'unknown' (not in
529-
// the local peer cache, so there is nothing to ask with).
527+
// What Telegram says about the account in a channel or group: 'member', 'left' (left, or
528+
// never joined), 'gone' (removed, banned, the chat deleted or migrated) or 'unknown'.
529+
// Only a chat Telegram returns decides: an error may mean a bad access hash, not a left chat.
530530
function membershipOf(chat) {
531531
switch (chat?._) {
532532
case 'channel':
@@ -904,10 +904,8 @@ class TelegramClient {
904904
let peer;
905905
try {
906906
peer = await this.client.resolvePeer(Number(id));
907-
} catch (error) {
908-
if (!(error instanceof MtPeerNotFoundError)) {
909-
throw error;
910-
}
907+
} catch {
908+
// Not in the peer cache, or not an id mtcute can parse: nothing to ask with.
911909
result.set(String(id), 'unknown');
912910
continue;
913911
}
@@ -929,7 +927,8 @@ class TelegramClient {
929927
throw error;
930928
}
931929
if (batch.length === 1) {
932-
result.set(batch[0].id, 'gone');
930+
// A stale or min access hash is refused the same way: no answer about the chat.
931+
result.set(batch[0].id, 'unknown');
933932
continue;
934933
}
935934
// One refused chat fails the whole batch, so ask about each on its own.

‎tests/left-chats.test.js‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,23 @@ describe('membership check in the Telegram client', () => {
9797

9898
const membership = await client.getChatMembership(['-1000000000001', '-1000000000002']);
9999

100-
expect(Object.fromEntries(membership)).toEqual({ '-1000000000001': 'member', '-1000000000002': 'gone' });
100+
// A refusal of one chat may be a stale access hash, not a left chat: no answer.
101+
expect(Object.fromEntries(membership)).toEqual({ '-1000000000001': 'member', '-1000000000002': 'unknown' });
102+
});
103+
104+
it('takes an id mtcute cannot resolve as unknown, and asks about the rest', async () => {
105+
const resolvePeer = client.client.resolvePeer;
106+
client.client.resolvePeer = async (id) => {
107+
if (Number.isNaN(id)) {
108+
throw new Error('Invalid marked peer id');
109+
}
110+
return resolvePeer(id);
111+
};
112+
answer = () => ({ chats: [{ _: 'channel', id: 1, left: false }] });
113+
114+
const membership = await client.getChatMembership(['-100123abc', '-1000000000001']);
115+
116+
expect(Object.fromEntries(membership)).toEqual({ '-100123abc': 'unknown', '-1000000000001': 'member' });
101117
});
102118

103119
it('fails on an error that is not about a chat, so nothing is decided on it', async () => {
@@ -217,6 +233,67 @@ describe('sync of chats missing from the dialog list', () => {
217233
expect(channel(LEFT)).toMatchObject({ sync_enabled: 1, sync_off_reason: null, membership: null });
218234
});
219235

236+
// Telegram calls a public channel archived without joining left, and the user asked for it.
237+
it('never turns off a chat the user asked for, by watch or by backfill', async () => {
238+
service.setChannelSync(LEFT, true);
239+
service.addJob(GONE);
240+
dialogs({ id: IN_DIALOGS });
241+
242+
await service.refreshChannelsFromDialogs();
243+
await service.checkChatsMissingFromDialogs();
244+
245+
expect(telegramClient.getChatMembership).toHaveBeenCalledWith([JOINED, UNKNOWN]);
246+
expect(channel(LEFT)).toMatchObject({ sync_enabled: 1, sync_off_reason: null });
247+
expect(channel(GONE)).toMatchObject({ sync_enabled: 1, sync_off_reason: null });
248+
});
249+
250+
it('turns sync back on when the user asks to backfill a chat the server turned off', async () => {
251+
dialogs({ id: IN_DIALOGS });
252+
await service.refreshChannelsFromDialogs();
253+
await service.checkChatsMissingFromDialogs();
254+
255+
service.addJob(LEFT);
256+
257+
const row = service.db.prepare('SELECT sync_enabled, sync_off_reason, sync_by_user FROM channels WHERE channel_id = ?').get(LEFT);
258+
expect(row).toEqual({ sync_enabled: 1, sync_off_reason: null, sync_by_user: 1 });
259+
});
260+
261+
it('forgets that a chat was still joined once it is back in the dialog list', async () => {
262+
dialogs({ id: IN_DIALOGS });
263+
await service.refreshChannelsFromDialogs();
264+
await service.checkChatsMissingFromDialogs();
265+
expect(channel(JOINED).membership).toBe('member');
266+
267+
dialogs({ id: IN_DIALOGS }, { id: JOINED });
268+
await service.refreshChannelsFromDialogs();
269+
expect(channel(JOINED).membership).toBeNull();
270+
271+
membership.set(JOINED, 'left');
272+
dialogs({ id: IN_DIALOGS });
273+
await service.refreshChannelsFromDialogs();
274+
await service.checkChatsMissingFromDialogs();
275+
expect(channel(JOINED)).toMatchObject({ sync_enabled: 0, sync_off_reason: 'left' });
276+
});
277+
278+
it('asks once when two comparisons come close together', async () => {
279+
dialogs({ id: IN_DIALOGS });
280+
await service.refreshChannelsFromDialogs();
281+
282+
await Promise.all([service.checkChatsMissingFromDialogs(), service.checkChatsMissingFromDialogs()]);
283+
284+
expect(telegramClient.getChatMembership).toHaveBeenCalledTimes(1);
285+
});
286+
287+
it('does not ask about ids that are not a chat id', async () => {
288+
service.db.prepare('INSERT INTO channels (channel_id, sync_enabled) VALUES (?, 1)').run('-100123abc');
289+
dialogs({ id: IN_DIALOGS });
290+
await service.refreshChannelsFromDialogs();
291+
292+
await service.checkChatsMissingFromDialogs();
293+
294+
expect(telegramClient.getChatMembership.mock.calls[0][0]).not.toContain('-100123abc');
295+
});
296+
220297
it('asks after the comparison with the dialog list, and a failed answer changes nothing', async () => {
221298
vi.spyOn(console, 'error').mockImplementation(() => {});
222299
telegramClient.getChatMembership.mockRejectedValueOnce(rpcError(500, 'INTERNAL'));

0 commit comments

Comments
 (0)