@imessaging/telegram-mtproto 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imessaging/telegram-mtproto",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "Telegram MTProto user-account transport for imessaging",
5
5
  "keywords": [
6
6
  "messaging",
@@ -231,6 +231,41 @@ export class TelegramMtprotoTransport
231
231
  }
232
232
  }
233
233
 
234
+ /**
235
+ * ДОГРУЗКА ПРОПУЩЕННОГО. Возвращает число сообщений, отданных подписчикам.
236
+ *
237
+ * Живая подписка теряет часть сообщений — чаще всего в супергруппах с темами: обновление
238
+ * приходит по каналу, которого клиент не помнит, и молча отбрасывается. Замер 07.09.2026:
239
+ * сообщение в теме форума видно по ссылке, а в подписке его нет ни у одного потребителя.
240
+ * Продукт italent живёт с этим давно и добирает последние сообщения опросом — здесь то же
241
+ * самое, только на уровне транспорта, чтобы каждому приложению не изобретать своё.
242
+ *
243
+ * Повторы неизбежны и это нормально: потребитель обязан отсеивать по идентификатору сообщения,
244
+ * а не полагаться на то, что канал не повторится. Он и так повторяется в обычной жизни.
245
+ */
246
+ async catchUp(options: { limit?: number; dialogs?: number } = {}): Promise<number> {
247
+ const limit = options.limit ?? 20;
248
+ const dialogs = await this.client.getDialogs({ limit: options.dialogs ?? 200 });
249
+ let dispatched = 0;
250
+ for (const dialog of dialogs) {
251
+ if (!dialog.isGroup && !dialog.isChannel) continue;
252
+ const entity = dialog.entity;
253
+ if (!entity) continue;
254
+ const messages = await this.client.getMessages(entity, { limit }).catch(() => []);
255
+ // От старых к новым: порядок обработки должен совпадать с тем, в каком они пришли бы живьём.
256
+ for (const message of [...messages].reverse()) {
257
+ const delivered = await this.incoming.dispatch(
258
+ message as unknown as MtprotoMessageLike,
259
+ this.id,
260
+ this.options.accountId,
261
+ );
262
+ if (delivered) dispatched += 1;
263
+ }
264
+ }
265
+ this.lastActivityAt = new Date();
266
+ return dispatched;
267
+ }
268
+
234
269
  private stopListeningIfIdle(): void {
235
270
  if (this.incoming.size > 0 || this.incomingListener === null) return;
236
271
  this.client.removeEventHandler(this.incomingListener, new NewMessage({}));