@mtkruto/browser 0.119.0 → 0.120.0

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.
Files changed (128) hide show
  1. package/README.md +1 -1
  2. package/esm/0_errors.d.ts.map +1 -1
  3. package/esm/0_errors.js +9 -31
  4. package/esm/3_errors.js +2 -12
  5. package/esm/4_errors.js +2 -12
  6. package/esm/_dnt.polyfills.d.ts +0 -99
  7. package/esm/_dnt.polyfills.d.ts.map +1 -1
  8. package/esm/_dnt.polyfills.js +1 -127
  9. package/esm/client/0_abortable_loop.js +26 -39
  10. package/esm/client/0_storage_operations.js +179 -218
  11. package/esm/client/1_client_plain.js +4 -22
  12. package/esm/client/2_account_manager.js +140 -149
  13. package/esm/client/2_bot_info_manager.js +26 -38
  14. package/esm/client/2_business_connection_manager.js +10 -23
  15. package/esm/client/2_client_encrypted.js +198 -215
  16. package/esm/client/2_file_manager.js +255 -262
  17. package/esm/client/2_network_statistics_manager.js +31 -44
  18. package/esm/client/2_payment_manager.js +7 -20
  19. package/esm/client/2_reaction_manager.js +7 -20
  20. package/esm/client/2_translations_manager.js +101 -111
  21. package/esm/client/2_update_manager.js +750 -745
  22. package/esm/client/3_client_encrypted_pool.js +10 -26
  23. package/esm/client/3_message_manager.js +503 -508
  24. package/esm/client/3_video_chat_manager.js +57 -68
  25. package/esm/client/4_callback_query_manager.js +18 -30
  26. package/esm/client/4_chat_list_manager.js +140 -146
  27. package/esm/client/4_chat_manager.js +161 -169
  28. package/esm/client/4_checklist_manager.js +26 -39
  29. package/esm/client/4_context.js +244 -259
  30. package/esm/client/4_forum_manager.js +67 -73
  31. package/esm/client/4_gift_manager.js +22 -35
  32. package/esm/client/4_inline_query_manager.js +16 -28
  33. package/esm/client/4_link_preview_manager.js +6 -19
  34. package/esm/client/4_poll_manager.js +44 -57
  35. package/esm/client/4_story_manager.js +41 -53
  36. package/esm/client/5_composer.js +13 -26
  37. package/esm/client/6_client.js +866 -896
  38. package/esm/client/6_client_dispatcher.js +308 -325
  39. package/esm/client/7_client_worker.js +16 -29
  40. package/esm/connection/1_connection_tcp.js +55 -82
  41. package/esm/connection/1_connection_web_socket.js +75 -91
  42. package/esm/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  43. package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
  44. package/esm/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
  45. package/esm/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
  46. package/esm/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
  47. package/esm/session/0_session_state.js +12 -38
  48. package/esm/session/1_session.js +49 -72
  49. package/esm/session/2_session_encrypted.js +422 -420
  50. package/esm/storage/2_storage_indexed_db.js +26 -44
  51. package/esm/storage/2_storage_local_storage.js +3 -16
  52. package/esm/storage/2_storage_memory.js +24 -41
  53. package/esm/storage/2_storage_session_storage.js +3 -16
  54. package/esm/tl/1_tl_reader.d.ts +1 -1
  55. package/esm/tl/1_tl_reader.d.ts.map +1 -1
  56. package/esm/tl/1_tl_reader.js +95 -103
  57. package/esm/tl/1_tl_writer.js +169 -178
  58. package/esm/transport/0_transport.js +1 -8
  59. package/esm/transport/1_transport_abridged.js +11 -24
  60. package/esm/transport/1_transport_intermediate.js +10 -23
  61. package/esm/utilities/0_mutex.js +4 -19
  62. package/esm/utilities/0_part_stream.js +11 -25
  63. package/esm/utilities/1_crypto.js +42 -53
  64. package/esm/utilities/2_queue.js +29 -47
  65. package/package.json +1 -1
  66. package/script/0_errors.d.ts.map +1 -1
  67. package/script/0_errors.js +9 -31
  68. package/script/3_errors.js +2 -12
  69. package/script/4_errors.js +2 -12
  70. package/script/_dnt.polyfills.d.ts +0 -99
  71. package/script/_dnt.polyfills.d.ts.map +1 -1
  72. package/script/_dnt.polyfills.js +0 -128
  73. package/script/client/0_abortable_loop.js +27 -40
  74. package/script/client/0_storage_operations.js +179 -218
  75. package/script/client/1_client_plain.js +4 -22
  76. package/script/client/2_account_manager.js +140 -149
  77. package/script/client/2_bot_info_manager.js +26 -38
  78. package/script/client/2_business_connection_manager.js +10 -23
  79. package/script/client/2_client_encrypted.js +199 -216
  80. package/script/client/2_file_manager.js +255 -262
  81. package/script/client/2_network_statistics_manager.js +32 -45
  82. package/script/client/2_payment_manager.js +7 -20
  83. package/script/client/2_reaction_manager.js +7 -20
  84. package/script/client/2_translations_manager.js +102 -112
  85. package/script/client/2_update_manager.js +750 -745
  86. package/script/client/3_client_encrypted_pool.js +10 -26
  87. package/script/client/3_message_manager.js +503 -508
  88. package/script/client/3_video_chat_manager.js +57 -68
  89. package/script/client/4_callback_query_manager.js +18 -30
  90. package/script/client/4_chat_list_manager.js +140 -146
  91. package/script/client/4_chat_manager.js +161 -169
  92. package/script/client/4_checklist_manager.js +26 -39
  93. package/script/client/4_context.js +244 -259
  94. package/script/client/4_forum_manager.js +67 -73
  95. package/script/client/4_gift_manager.js +22 -35
  96. package/script/client/4_inline_query_manager.js +16 -28
  97. package/script/client/4_link_preview_manager.js +6 -19
  98. package/script/client/4_poll_manager.js +44 -57
  99. package/script/client/4_story_manager.js +41 -53
  100. package/script/client/5_composer.js +13 -26
  101. package/script/client/6_client.js +866 -896
  102. package/script/client/6_client_dispatcher.js +308 -325
  103. package/script/client/7_client_worker.js +16 -29
  104. package/script/connection/1_connection_tcp.js +55 -82
  105. package/script/connection/1_connection_web_socket.js +75 -91
  106. package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  107. package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
  108. package/script/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
  109. package/script/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
  110. package/script/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
  111. package/script/session/0_session_state.js +12 -38
  112. package/script/session/1_session.js +49 -72
  113. package/script/session/2_session_encrypted.js +423 -421
  114. package/script/storage/2_storage_indexed_db.js +26 -44
  115. package/script/storage/2_storage_local_storage.js +3 -16
  116. package/script/storage/2_storage_memory.js +24 -41
  117. package/script/storage/2_storage_session_storage.js +3 -16
  118. package/script/tl/1_tl_reader.d.ts +1 -1
  119. package/script/tl/1_tl_reader.d.ts.map +1 -1
  120. package/script/tl/1_tl_reader.js +96 -104
  121. package/script/tl/1_tl_writer.js +170 -179
  122. package/script/transport/0_transport.js +1 -8
  123. package/script/transport/1_transport_abridged.js +11 -24
  124. package/script/transport/1_transport_intermediate.js +10 -23
  125. package/script/utilities/0_mutex.js +4 -19
  126. package/script/utilities/0_part_stream.js +11 -25
  127. package/script/utilities/1_crypto.js +43 -54
  128. package/script/utilities/2_queue.js +30 -48
@@ -18,18 +18,7 @@
18
18
  * You should have received a copy of the GNU Lesser General Public License
19
19
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
20
  */
21
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
22
- if (kind === "m") throw new TypeError("Private method is not writable");
23
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
24
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
25
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
26
- };
27
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
28
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
29
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
30
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
31
- };
32
- var _UpdateManager_instances, _a, _UpdateManager_c, _UpdateManager_updateState, _UpdateManager_updateHandler, _UpdateManager_LrecoverUpdateGap, _UpdateManager_LrecoverChannelUpdateGap, _UpdateManager_L$handleUpdate, _UpdateManager_L$processUpdates, _UpdateManager_LfetchState, _UpdateManager_LopenChat, _UpdateManager_defaultDropPendingUpdates, _UpdateManager_mustDropPendingUpdates, _UpdateManager_state, _UpdateManager_getState, _UpdateManager_setState, _UpdateManager_extractMessages, _UpdateManager_extractMinPeerReferences, _UpdateManager_handleUpdateQueues, _UpdateManager_nonFirst, _UpdateManager_getChannelPtsWithDropPendingUpdatesCheck, _UpdateManager_checkGap, _UpdateManager_checkGapQts, _UpdateManager_checkChannelGap, _UpdateManager_channelUpdateQueues, _UpdateManager_processChannelPtsUpdateInner, _UpdateManager_queueUpdate, _UpdateManager_processChannelPtsUpdate, _UpdateManager_processPtsUpdateInner, _UpdateManager_ptsUpdateQueue, _UpdateManager_processPtsUpdate, _UpdateManager_processQtsUpdateInner, _UpdateManager_qtsUpdateQueue, _UpdateManager_processQtsUpdate, _UpdateManager_processUpdatesQueue, _UpdateManager_processUpdates, _UpdateManager_setUpdateStateDate, _UpdateManager_setUpdatePts, _UpdateManager_setUpdateQts, _UpdateManager_getLocalState, _UpdateManager_recoveringUpdateGap, _UpdateManager_recoverUpdateGapMutex, _UpdateManager_recoverChannelUpdateGap, _UpdateManager_handleUpdatesSet, _UpdateManager_handleStoredUpdates, _UpdateManager_handleUpdate, _UpdateManager_needsGetDifference, _UpdateManager_collectChatIds, _UpdateManager_collectChatIdsFromEntities, _UpdateManager_openChats;
21
+ var _a;
33
22
  Object.defineProperty(exports, "__esModule", { value: true });
34
23
  exports.UpdateManager = void 0;
35
24
  const _0_deps_js_1 = require("../0_deps.js");
@@ -40,37 +29,26 @@ const _3_errors_js_1 = require("../3_errors.js");
40
29
  const _4_constants_js_1 = require("../4_constants.js");
41
30
  const _2_telegram_js_1 = require("../tl/2_telegram.js");
42
31
  class UpdateManager {
32
+ static QTS_COUNT = 1;
33
+ static MAIN_BOX_ID = 0n;
34
+ #c;
35
+ #updateState;
36
+ #updateHandler;
37
+ #LrecoverUpdateGap;
38
+ #LrecoverChannelUpdateGap;
39
+ #L$handleUpdate;
40
+ #L$processUpdates;
41
+ #LfetchState;
42
+ #LopenChat;
43
43
  constructor(c) {
44
- _UpdateManager_instances.add(this);
45
- _UpdateManager_c.set(this, void 0);
46
- _UpdateManager_updateState.set(this, void 0);
47
- _UpdateManager_updateHandler.set(this, void 0);
48
- _UpdateManager_LrecoverUpdateGap.set(this, void 0);
49
- _UpdateManager_LrecoverChannelUpdateGap.set(this, void 0);
50
- _UpdateManager_L$handleUpdate.set(this, void 0);
51
- _UpdateManager_L$processUpdates.set(this, void 0);
52
- _UpdateManager_LfetchState.set(this, void 0);
53
- _UpdateManager_LopenChat.set(this, void 0);
54
- _UpdateManager_defaultDropPendingUpdates.set(this, null);
55
- _UpdateManager_state.set(this, null);
56
- _UpdateManager_handleUpdateQueues.set(this, new Map());
57
- _UpdateManager_nonFirst.set(this, new Set());
58
- _UpdateManager_channelUpdateQueues.set(this, new Map());
59
- _UpdateManager_ptsUpdateQueue.set(this, new _1_utilities_js_1.Queue("ptsUpdate"));
60
- _UpdateManager_qtsUpdateQueue.set(this, new _1_utilities_js_1.Queue("qtsUpdate"));
61
- _UpdateManager_processUpdatesQueue.set(this, new _1_utilities_js_1.Queue("UpdateManager/processUpdates"));
62
- _UpdateManager_recoveringUpdateGap.set(this, false);
63
- _UpdateManager_recoverUpdateGapMutex.set(this, new _1_utilities_js_1.Mutex());
64
- _UpdateManager_handleUpdatesSet.set(this, new Set());
65
- _UpdateManager_openChats.set(this, new Map());
66
- __classPrivateFieldSet(this, _UpdateManager_c, c, "f");
44
+ this.#c = c;
67
45
  const L = (0, _1_utilities_js_1.getLogger)("UpdateManager").client(c.id);
68
- __classPrivateFieldSet(this, _UpdateManager_LrecoverUpdateGap, L.branch("recoverUpdateGap"), "f");
69
- __classPrivateFieldSet(this, _UpdateManager_LrecoverChannelUpdateGap, L.branch("recoverChannelUpdateGap"), "f");
70
- __classPrivateFieldSet(this, _UpdateManager_L$handleUpdate, L.branch("#handleUpdate"), "f");
71
- __classPrivateFieldSet(this, _UpdateManager_L$processUpdates, L.branch("#processUpdates"), "f");
72
- __classPrivateFieldSet(this, _UpdateManager_LfetchState, L.branch("fetchState"), "f");
73
- __classPrivateFieldSet(this, _UpdateManager_LopenChat, L.branch("openChat"), "f");
46
+ this.#LrecoverUpdateGap = L.branch("recoverUpdateGap");
47
+ this.#LrecoverChannelUpdateGap = L.branch("recoverChannelUpdateGap");
48
+ this.#L$handleUpdate = L.branch("#handleUpdate");
49
+ this.#L$processUpdates = L.branch("#processUpdates");
50
+ this.#LfetchState = L.branch("fetchState");
51
+ this.#LopenChat = L.branch("openChat");
74
52
  }
75
53
  static isPtsUpdate(v) {
76
54
  return _2_tl_js_1.Api.isOneOf(["updateNewMessage", "updateDeleteMessages", "updateReadHistoryInbox", "updateReadHistoryOutbox", "updatePinnedChannelMessages", "updatePinnedMessages", "updateFolderPeers", "updateChannelWebPage", "updateEditMessage", "updateReadMessagesContents", "updateWebPage"], v);
@@ -86,21 +64,94 @@ class UpdateManager {
86
64
  "updateChannelTooLong",
87
65
  ], v);
88
66
  }
67
+ #defaultDropPendingUpdates = null;
68
+ #mustDropPendingUpdates() {
69
+ if (typeof this.#c.dropPendingUpdates === "boolean") {
70
+ return this.#c.dropPendingUpdates;
71
+ }
72
+ if (this.#defaultDropPendingUpdates === null) {
73
+ this.#defaultDropPendingUpdates = this.#c.storage.isBot;
74
+ }
75
+ return this.#defaultDropPendingUpdates;
76
+ }
77
+ #state = null;
78
+ async #getState() {
79
+ if (this.#mustDropPendingUpdates()) {
80
+ return this.#state ?? null;
81
+ }
82
+ if (this.#state !== null) {
83
+ return this.#state;
84
+ }
85
+ const state = await this.#c.storage.getState();
86
+ return this.#state = state;
87
+ }
88
+ async #setState(state) {
89
+ this.#state = state;
90
+ if (!this.#mustDropPendingUpdates()) {
91
+ await this.#c.storage.setState(state);
92
+ }
93
+ }
89
94
  async fetchState(source) {
90
- let state = await __classPrivateFieldGet(this, _UpdateManager_c, "f").invoke({ _: "updates.getState" });
91
- const difference = await __classPrivateFieldGet(this, _UpdateManager_c, "f").invoke({ ...state, _: "updates.getDifference" });
95
+ let state = await this.#c.invoke({ _: "updates.getState" });
96
+ const difference = await this.#c.invoke({ ...state, _: "updates.getDifference" });
92
97
  if (_2_tl_js_1.Api.is("updates.difference", difference)) {
93
98
  state = difference.state;
94
99
  }
95
100
  else if (_2_tl_js_1.Api.is("updates.differenceSlice", difference)) {
96
101
  state = difference.intermediate_state;
97
102
  }
98
- __classPrivateFieldSet(this, _UpdateManager_updateState, state, "f");
99
- __classPrivateFieldGet(this, _UpdateManager_LfetchState, "f").debug(`state fetched [${source}]`);
100
- if (__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_mustDropPendingUpdates).call(this)) {
101
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, state);
103
+ this.#updateState = state;
104
+ this.#LfetchState.debug(`state fetched [${source}]`);
105
+ if (this.#mustDropPendingUpdates()) {
106
+ await this.#setState(state);
102
107
  }
103
108
  }
109
+ #extractMessages(context) {
110
+ const messages = new Array();
111
+ if (Array.isArray(context)) {
112
+ for (const item of context) {
113
+ messages.push(...this.#extractMessages(item));
114
+ }
115
+ }
116
+ else if (_2_tl_js_1.Api.isOneOf(["updates", "updatesCombined"], context)) {
117
+ messages.push(...this.#extractMessages(context.updates));
118
+ }
119
+ else if (_2_tl_js_1.Api.isOneOf(["updates.difference", "updates.differenceSlice", "updates.channelDifference"], context)) {
120
+ for (const message of context.new_messages) {
121
+ if (_2_tl_js_1.Api.is("message", message)) {
122
+ messages.push(message);
123
+ }
124
+ }
125
+ messages.push(...this.#extractMessages(context.other_updates));
126
+ }
127
+ else if (_2_tl_js_1.Api.isOneOf(["updateNewMessage", "updateNewChannelMessage", "updateEditMessage", "updateEditChannelMessage", "updateBotNewBusinessMessage", "updateBotNewBusinessMessage"], context)) {
128
+ if (_2_tl_js_1.Api.is("message", context.message)) {
129
+ messages.push(context.message);
130
+ }
131
+ }
132
+ else if (_2_tl_js_1.Api.is("message", context)) {
133
+ messages.push(context);
134
+ }
135
+ else if (context !== null && typeof context === "object" && "messages" in context && Array.isArray(context.messages)) {
136
+ for (const message of context.messages) {
137
+ if (_2_tl_js_1.Api.is("message", message)) {
138
+ messages.push(message);
139
+ }
140
+ }
141
+ }
142
+ return messages;
143
+ }
144
+ #extractMinPeerReferences(context) {
145
+ const minPeerReferences = new Array();
146
+ const messages = this.#extractMessages(context);
147
+ for (const message of messages) {
148
+ if (!message.from_id) {
149
+ continue;
150
+ }
151
+ minPeerReferences.push({ chatId: _2_tl_js_1.Api.peerToChatId(message.peer_id), senderId: _2_tl_js_1.Api.peerToChatId(message.from_id), messageId: message.id });
152
+ }
153
+ return minPeerReferences;
154
+ }
104
155
  processChats(chats, _context) {
105
156
  for (const chat of chats) {
106
157
  this.processChat(chat);
@@ -113,14 +164,14 @@ class UpdateManager {
113
164
  if (_2_tl_js_1.Api.is("channel", chat) && chat.min) {
114
165
  return; // TODO
115
166
  }
116
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.setPeer(chat);
167
+ this.#c.messageStorage.setPeer(chat);
117
168
  if ("username" in chat && chat.username) {
118
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.usernames.set([chat.username], [_2_tl_js_1.Api.peerToChatId(chat), new Date()]);
169
+ this.#c.messageStorage.usernames.set([chat.username], [_2_tl_js_1.Api.peerToChatId(chat), new Date()]);
119
170
  }
120
171
  if ("usernames" in chat && chat.usernames) {
121
172
  const value = [_2_tl_js_1.Api.peerToChatId(chat), new Date()];
122
173
  for (const username of chat.usernames) {
123
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.usernames.set([username.username], value);
174
+ this.#c.messageStorage.usernames.set([username.username], value);
124
175
  }
125
176
  }
126
177
  }
@@ -160,7 +211,7 @@ class UpdateManager {
160
211
  if ("messages" in result && Array.isArray(result.messages)) {
161
212
  for (const message of result.messages) {
162
213
  if (_2_tl_js_1.Api.is("message", message) || _2_tl_js_1.Api.is("messageService", message)) {
163
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.setMessage(_2_tl_js_1.Api.peerToChatId(message.peer_id), message.id, message);
214
+ await this.#c.messageStorage.setMessage(_2_tl_js_1.Api.peerToChatId(message.peer_id), message.id, message);
164
215
  }
165
216
  }
166
217
  }
@@ -168,7 +219,7 @@ class UpdateManager {
168
219
  if (_2_tl_js_1.Api.is("messages.messages", result)) {
169
220
  for (const message of result.messages) {
170
221
  if (_2_tl_js_1.Api.is("message", message) || _2_tl_js_1.Api.is("messageService", message)) {
171
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.setMessage(_2_tl_js_1.Api.peerToChatId(message.peer_id), message.id, message);
222
+ await this.#c.messageStorage.setMessage(_2_tl_js_1.Api.peerToChatId(message.peer_id), message.id, message);
172
223
  }
173
224
  }
174
225
  }
@@ -185,19 +236,20 @@ class UpdateManager {
185
236
  if (user.min) {
186
237
  return; // TODO
187
238
  }
188
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.setPeer(user);
239
+ this.#c.messageStorage.setPeer(user);
189
240
  if (user.username) {
190
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.usernames.set([user.username], [_2_tl_js_1.Api.peerToChatId(user), new Date()]);
241
+ this.#c.messageStorage.usernames.set([user.username], [_2_tl_js_1.Api.peerToChatId(user), new Date()]);
191
242
  }
192
243
  if (user.usernames) {
193
244
  const value = [_2_tl_js_1.Api.peerToChatId(user), new Date()];
194
245
  for (const username of user.usernames) {
195
- __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.usernames.set([username.username], value);
246
+ this.#c.messageStorage.usernames.set([username.username], value);
196
247
  }
197
248
  }
198
249
  }
250
+ #handleUpdateQueues = new Map();
199
251
  getHandleUpdateQueue(boxId) {
200
- let queue = __classPrivateFieldGet(this, _UpdateManager_handleUpdateQueues, "f").get(boxId);
252
+ let queue = this.#handleUpdateQueues.get(boxId);
201
253
  if (queue !== undefined) {
202
254
  return queue;
203
255
  }
@@ -206,27 +258,365 @@ class UpdateManager {
206
258
  return queue;
207
259
  }
208
260
  }
261
+ #nonFirst = new Set();
262
+ async #getChannelPtsWithDropPendingUpdatesCheck(channelId) {
263
+ if (!(this.#mustDropPendingUpdates())) {
264
+ return await this.#c.storage.channelPts.get([channelId]);
265
+ }
266
+ const first = !this.#nonFirst.has(channelId);
267
+ if (first) {
268
+ this.#nonFirst.add(channelId);
269
+ return null;
270
+ }
271
+ else {
272
+ return await this.#c.storage.channelPts.get([channelId]);
273
+ }
274
+ }
275
+ async #checkGap(pts, ptsCount) {
276
+ const localState = await this.#getLocalState();
277
+ if (localState.pts + ptsCount < pts) {
278
+ await this.recoverUpdateGap("processUpdates[pts]");
279
+ }
280
+ }
281
+ async #checkGapQts(qts) {
282
+ const localState = await this.#getLocalState();
283
+ if (localState.qts + _a.QTS_COUNT < qts) {
284
+ await this.recoverUpdateGap("processUpdates[qts]");
285
+ }
286
+ }
287
+ async #checkChannelGap(channelId, pts, ptsCount) {
288
+ let localPts = await this.#getChannelPtsWithDropPendingUpdatesCheck(channelId);
289
+ if (!localPts) {
290
+ localPts = pts - ptsCount;
291
+ }
292
+ if (localPts + ptsCount < pts) {
293
+ await this.#recoverChannelUpdateGap(channelId, "processUpdates");
294
+ }
295
+ }
296
+ #channelUpdateQueues = new Map();
297
+ async #processChannelPtsUpdateInner(update, checkGap) {
298
+ const channelId = _2_tl_js_1.Api.is("updateNewChannelMessage", update) || _2_tl_js_1.Api.is("updateEditChannelMessage", update) ? _2_tl_js_1.Api.as("peerChannel", update.message.peer_id).channel_id : update.channel_id;
299
+ if (_2_tl_js_1.Api.is("updateChannelTooLong", update)) {
300
+ if (update.pts !== undefined) {
301
+ this.#c.storage.channelPts.set([channelId], update.pts);
302
+ }
303
+ await this.#recoverChannelUpdateGap(channelId, "updateChannelTooLong");
304
+ return;
305
+ }
306
+ if (update.pts !== 0) {
307
+ const ptsCount = update.pts_count;
308
+ if (checkGap) {
309
+ await this.#checkChannelGap(channelId, update.pts, ptsCount);
310
+ }
311
+ let currentPts = await this.#getChannelPtsWithDropPendingUpdatesCheck(channelId);
312
+ currentPts ??= update.pts - ptsCount;
313
+ if (currentPts + ptsCount > update.pts) {
314
+ return;
315
+ }
316
+ }
317
+ if (this.#c.guaranteeUpdateDelivery) {
318
+ await this.#c.storage.setUpdate(channelId, update);
319
+ }
320
+ if (update.pts !== 0) {
321
+ this.#c.storage.channelPts.set([channelId], update.pts);
322
+ }
323
+ this.#queueUpdate(update, channelId, true);
324
+ }
325
+ #queueUpdate(update, boxId, pts) {
326
+ this.getHandleUpdateQueue(boxId).add(async () => {
327
+ if (this.#c.guaranteeUpdateDelivery && pts) {
328
+ await this.#handleStoredUpdates(boxId);
329
+ }
330
+ else {
331
+ await (await this.#handleUpdate(update))();
332
+ }
333
+ });
334
+ }
335
+ #processChannelPtsUpdate(update, checkGap) {
336
+ const channelId = _2_tl_js_1.Api.is("updateNewChannelMessage", update) || _2_tl_js_1.Api.is("updateEditChannelMessage", update) ? _2_tl_js_1.Api.as("peerChannel", update.message.peer_id).channel_id : update.channel_id;
337
+ let queue = this.#channelUpdateQueues.get(channelId);
338
+ if (queue === undefined) {
339
+ queue = new _1_utilities_js_1.Queue(`channelUpdates-${channelId}`);
340
+ this.#channelUpdateQueues.set(channelId, queue);
341
+ }
342
+ queue.add(async () => {
343
+ await this.#processChannelPtsUpdateInner(update, checkGap);
344
+ });
345
+ }
346
+ async #processPtsUpdateInner(update, checkGap) {
347
+ if (update.pts !== 0 && checkGap) {
348
+ await this.#checkGap(update.pts, update.pts_count);
349
+ if (await this.#needsGetDifference(update)) {
350
+ await this.recoverUpdateGap("needsGetDifference");
351
+ }
352
+ }
353
+ const localState = await this.#getLocalState();
354
+ if (update.pts !== 0 && localState.pts + update.pts_count > update.pts) {
355
+ return;
356
+ }
357
+ if (this.#c.guaranteeUpdateDelivery) {
358
+ await this.#c.storage.setUpdate(_a.MAIN_BOX_ID, update);
359
+ }
360
+ if (update.pts !== 0) {
361
+ await this.#setUpdatePts(update.pts);
362
+ }
363
+ this.#queueUpdate(update, 1n, false);
364
+ }
365
+ #ptsUpdateQueue = new _1_utilities_js_1.Queue("ptsUpdate");
366
+ #processPtsUpdate(update, checkGap) {
367
+ this.#ptsUpdateQueue.add(async () => {
368
+ await this.#processPtsUpdateInner(update, checkGap);
369
+ });
370
+ }
371
+ async #processQtsUpdateInner(update, checkGap) {
372
+ const localState = await this.#getLocalState();
373
+ if (update.qts !== 0) {
374
+ if (checkGap) {
375
+ await this.#checkGapQts(update.qts);
376
+ }
377
+ if (localState.qts + _a.QTS_COUNT > update.qts) {
378
+ return;
379
+ }
380
+ }
381
+ if (this.#c.guaranteeUpdateDelivery) {
382
+ await this.#c.storage.setUpdate(_a.MAIN_BOX_ID, update);
383
+ }
384
+ if (update.qts !== 0) {
385
+ await this.#setUpdateQts(update.qts);
386
+ }
387
+ this.#queueUpdate(update, 0n, true);
388
+ }
389
+ #qtsUpdateQueue = new _1_utilities_js_1.Queue("qtsUpdate");
390
+ #processQtsUpdate(update, checkGap) {
391
+ this.#qtsUpdateQueue.add(async () => {
392
+ await this.#processQtsUpdateInner(update, checkGap);
393
+ });
394
+ }
395
+ #processUpdatesQueue = new _1_utilities_js_1.Queue("UpdateManager/processUpdates");
209
396
  processUpdates(updates, checkGap, call = null, callback) {
210
- __classPrivateFieldGet(this, _UpdateManager_processUpdatesQueue, "f").add(() => __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, updates, checkGap, call).finally(callback));
397
+ this.#processUpdatesQueue.add(() => this.#processUpdates(updates, checkGap, call).finally(callback));
398
+ }
399
+ async #processUpdates(updates_, checkGap, call = null) {
400
+ /// First, individual updates (Update[1]) are extracted from Updates.[2]
401
+ ///
402
+ /// If an updatesTooLong[3] was received, an update gap recovery is initiated and no further action will be taken.
403
+ ///
404
+ /// [1]: https://core.telegram.org/type/Update
405
+ /// [2]: https://core.telegram.org/type/Updates
406
+ /// [3]: https://core.telegram.org/constructor/updatesTooLong
407
+ let updates;
408
+ if (_2_tl_js_1.Api.is("updatesCombined", updates_) || _2_tl_js_1.Api.is("updates", updates_)) {
409
+ updates = updates_.updates;
410
+ const seq = updates_.seq;
411
+ const seqStart = "seq_start" in updates_ ? updates_.seq_start : updates_.seq;
412
+ if (checkGap) {
413
+ if (seqStart === 0) {
414
+ checkGap = false;
415
+ this.#L$processUpdates.debug("seqStart=0");
416
+ }
417
+ else {
418
+ const localState = await this.#getLocalState();
419
+ const localSeq = localState.seq;
420
+ if (localSeq + 1 === seqStart) {
421
+ // The update sequence can be applied.
422
+ localState.seq = seq;
423
+ localState.date = updates_.date;
424
+ await this.#setUpdateStateDate(updates_.date);
425
+ await this.#setState(localState);
426
+ }
427
+ else if (localSeq + 1 > seqStart) {
428
+ // The update sequence was already applied, and must be ignored.
429
+ this.#L$processUpdates.debug("localSeq + 1 > seqStart");
430
+ return;
431
+ }
432
+ else if (localSeq + 1 < seqStart) {
433
+ // There's an updates gap that must be filled.
434
+ await this.recoverUpdateGap("localSeq + 1 < seqStart");
435
+ }
436
+ }
437
+ }
438
+ }
439
+ else if (_2_tl_js_1.Api.is("updateShort", updates_)) {
440
+ updates = [updates_.update];
441
+ }
442
+ else if (_2_tl_js_1.Api.is("updateShortMessage", updates_)) {
443
+ updates = [
444
+ {
445
+ _: "updateNewMessage",
446
+ message: {
447
+ _: "message",
448
+ out: updates_.out,
449
+ mentioned: updates_.mentioned,
450
+ media_unread: updates_.media_unread,
451
+ silent: updates_.silent,
452
+ id: updates_.id,
453
+ from_id: updates_.out ? ({ _: "peerUser", user_id: BigInt(await this.#c.getSelfId()) }) : ({ _: "peerUser", user_id: updates_.user_id }),
454
+ peer_id: { _: "peerUser", user_id: updates_.user_id },
455
+ message: updates_.message,
456
+ date: updates_.date,
457
+ fwd_from: updates_.fwd_from,
458
+ via_bot_id: updates_.via_bot_id,
459
+ reply_to: updates_.reply_to,
460
+ entities: updates_.entities,
461
+ ttl_period: updates_.ttl_period,
462
+ },
463
+ pts: updates_.pts,
464
+ pts_count: updates_.pts_count,
465
+ },
466
+ ];
467
+ }
468
+ else if (_2_tl_js_1.Api.is("updateShortChatMessage", updates_)) {
469
+ updates = [
470
+ {
471
+ _: "updateNewMessage",
472
+ message: {
473
+ _: "message",
474
+ mentioned: updates_.mentioned,
475
+ media_unread: updates_.media_unread,
476
+ silent: updates_.silent,
477
+ id: updates_.id,
478
+ from_id: { _: "peerUser", user_id: updates_.from_id },
479
+ peer_id: { _: "peerChat", chat_id: updates_.chat_id },
480
+ fwd_from: updates_.fwd_from,
481
+ via_bot_id: updates_.via_bot_id,
482
+ reply_to: updates_.reply_to,
483
+ date: updates_.date,
484
+ message: updates_.message,
485
+ entities: updates_.entities,
486
+ ttl_period: updates_.ttl_period,
487
+ },
488
+ pts: updates_.pts,
489
+ pts_count: updates_.pts_count,
490
+ },
491
+ ];
492
+ }
493
+ else if (_2_tl_js_1.Api.is("updateShortSentMessage", updates_)) {
494
+ if (!_2_tl_js_1.Api.is("messages.sendMessage", call)) {
495
+ (0, _0_deps_js_1.unreachable)();
496
+ }
497
+ updates = [{
498
+ _: "updateNewMessage",
499
+ message: {
500
+ _: "message",
501
+ out: updates_.out,
502
+ silent: call.silent,
503
+ id: updates_.id,
504
+ from_id: { _: "peerUser", user_id: BigInt(await this.#c.getSelfId()) },
505
+ peer_id: _2_tl_js_1.Api.inputPeerToPeer(call.peer),
506
+ message: call.message,
507
+ media: updates_.media,
508
+ date: updates_.date,
509
+ // reply_to: call.reply_to, // TODO?
510
+ entities: updates_.entities,
511
+ ttl_period: updates_.ttl_period,
512
+ },
513
+ pts: updates_.pts,
514
+ pts_count: updates_.pts_count,
515
+ }];
516
+ }
517
+ else if (_2_tl_js_1.Api.is("updatesTooLong", updates_)) {
518
+ await this.recoverUpdateGap("updatesTooLong");
519
+ return;
520
+ }
521
+ else if (_2_tl_js_1.Api.isOfEnum("Update", updates_)) {
522
+ updates = [updates_];
523
+ }
524
+ else {
525
+ (0, _0_deps_js_1.unreachable)();
526
+ }
527
+ /// We process the updates when we are sure there is no gap.
528
+ if (_2_tl_js_1.Api.is("updates", updates_) || _2_tl_js_1.Api.is("updatesCombined", updates_)) {
529
+ this.processChats(updates_.chats, updates_);
530
+ this.processUsers(updates_.users, updates_);
531
+ this.#setUpdateStateDate(updates_.date);
532
+ }
533
+ else if (_2_tl_js_1.Api.isOneOf([
534
+ "updateShort",
535
+ "updateShortMessage",
536
+ "updateShortChatMessage",
537
+ "updateShortSentMessage",
538
+ ], updates_)) {
539
+ await this.#setUpdateStateDate(updates_.date);
540
+ }
541
+ for (const update of updates) {
542
+ if (_2_tl_js_1.Api.is("updatePtsChanged", update)) {
543
+ await this.fetchState("updatePtsChanged");
544
+ if (this.#updateState) {
545
+ await this.#setState(this.#updateState);
546
+ }
547
+ else {
548
+ (0, _0_deps_js_1.unreachable)();
549
+ }
550
+ }
551
+ else if (_a.isPtsUpdate(update)) {
552
+ this.#processPtsUpdate(update, checkGap);
553
+ }
554
+ else if (_a.isChannelPtsUpdate(update)) {
555
+ this.#processChannelPtsUpdate(update, checkGap);
556
+ }
557
+ else if (_a.isQtsUpdate(update)) {
558
+ this.#processQtsUpdate(update, checkGap);
559
+ }
560
+ else {
561
+ this.#queueUpdate(update, 0n, false);
562
+ }
563
+ }
564
+ }
565
+ async #setUpdateStateDate(date) {
566
+ const localState = await this.#getLocalState();
567
+ localState.date = date;
568
+ await this.#setState(localState);
569
+ }
570
+ async #setUpdatePts(pts) {
571
+ const localState = await this.#getLocalState();
572
+ localState.pts = pts;
573
+ await this.#setState(localState);
574
+ }
575
+ async #setUpdateQts(qts) {
576
+ const localState = await this.#getLocalState();
577
+ localState.qts = qts;
578
+ await this.#setState(localState);
579
+ }
580
+ async #getLocalState() {
581
+ let localState = await this.#getState();
582
+ if (!localState) {
583
+ if (this.#updateState) {
584
+ localState = this.#updateState;
585
+ await this.#setState(localState);
586
+ }
587
+ else {
588
+ await this.fetchState("getLocalState");
589
+ if (this.#updateState) {
590
+ localState = this.#updateState;
591
+ await this.#setState(localState);
592
+ }
593
+ else {
594
+ (0, _0_deps_js_1.unreachable)();
595
+ }
596
+ }
597
+ }
598
+ return localState;
211
599
  }
600
+ #recoveringUpdateGap = false;
601
+ #recoverUpdateGapMutex = new _1_utilities_js_1.Mutex();
212
602
  async recoverUpdateGap(source) {
213
- const wasRecoveringUpdateGap = __classPrivateFieldGet(this, _UpdateManager_recoveringUpdateGap, "f");
214
- const unlock = await __classPrivateFieldGet(this, _UpdateManager_recoverUpdateGapMutex, "f").lock();
603
+ const wasRecoveringUpdateGap = this.#recoveringUpdateGap;
604
+ const unlock = await this.#recoverUpdateGapMutex.lock();
215
605
  if (wasRecoveringUpdateGap) {
216
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").debug(`update gap was just recovered [${source}]`);
606
+ this.#LrecoverUpdateGap.debug(`update gap was just recovered [${source}]`);
217
607
  unlock();
218
608
  return;
219
609
  }
220
- __classPrivateFieldSet(this, _UpdateManager_recoveringUpdateGap, true, "f");
221
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").debug(`recovering from update gap [${source}]`);
222
- __classPrivateFieldGet(this, _UpdateManager_c, "f").setConnectionState("updating");
610
+ this.#recoveringUpdateGap = true;
611
+ this.#LrecoverUpdateGap.debug(`recovering from update gap [${source}]`);
612
+ this.#c.setConnectionState("updating");
223
613
  try {
224
614
  let retryIn = 5;
225
- let state = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
615
+ let state = await this.#getLocalState();
226
616
  while (true) {
227
617
  let difference;
228
618
  try {
229
- difference = await __classPrivateFieldGet(this, _UpdateManager_c, "f").invoke({ _: "updates.getDifference", pts: state.pts, date: state.date, qts: state.qts ?? 0 });
619
+ difference = await this.#c.invoke({ _: "updates.getDifference", pts: state.pts, date: state.date, qts: state.qts ?? 0 });
230
620
  }
231
621
  catch (err) {
232
622
  if (err instanceof _3_errors_js_1.PersistentTimestampInvalid) {
@@ -245,32 +635,32 @@ class UpdateManager {
245
635
  this.processChats(difference.chats, difference);
246
636
  this.processUsers(difference.users, difference);
247
637
  for (const message of difference.new_messages) {
248
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, { _: "updateNewMessage", message, pts: 0, pts_count: 0 }, false);
638
+ await this.#processUpdates({ _: "updateNewMessage", message, pts: 0, pts_count: 0 }, false);
249
639
  }
250
640
  for (const update of difference.other_updates) {
251
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, update, false);
641
+ await this.#processUpdates(update, false);
252
642
  }
253
643
  if (_2_tl_js_1.Api.is("updates.difference", difference)) {
254
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, difference.state);
255
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").debug("recovered from update gap");
644
+ await this.#setState(difference.state);
645
+ this.#LrecoverUpdateGap.debug("recovered from update gap");
256
646
  break;
257
647
  }
258
648
  else if (_2_tl_js_1.Api.is("updates.differenceSlice", difference)) {
259
649
  state = difference.intermediate_state;
260
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, state);
650
+ await this.#setState(state);
261
651
  }
262
652
  else {
263
653
  (0, _0_deps_js_1.unreachable)();
264
654
  }
265
655
  }
266
656
  else if (_2_tl_js_1.Api.is("updates.differenceTooLong", difference)) {
267
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.deleteMessages();
657
+ await this.#c.messageStorage.deleteMessages();
268
658
  state.pts = difference.pts;
269
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").debug("received differenceTooLong");
659
+ this.#LrecoverUpdateGap.debug("received differenceTooLong");
270
660
  }
271
661
  else if (_2_tl_js_1.Api.is("updates.differenceEmpty", difference)) {
272
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdateStateDate).call(this, difference.date);
273
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").debug("there was no update gap");
662
+ await this.#setUpdateStateDate(difference.date);
663
+ this.#LrecoverUpdateGap.debug("there was no update gap");
274
664
  break;
275
665
  }
276
666
  else {
@@ -279,43 +669,309 @@ class UpdateManager {
279
669
  }
280
670
  }
281
671
  catch (err) {
282
- __classPrivateFieldGet(this, _UpdateManager_LrecoverUpdateGap, "f").error(err);
672
+ this.#LrecoverUpdateGap.error(err);
283
673
  }
284
674
  finally {
285
675
  unlock();
286
- __classPrivateFieldGet(this, _UpdateManager_c, "f").resetConnectionState();
287
- __classPrivateFieldSet(this, _UpdateManager_recoveringUpdateGap, false, "f");
676
+ this.#c.resetConnectionState();
677
+ this.#recoveringUpdateGap = false;
678
+ }
679
+ }
680
+ async #recoverChannelUpdateGap(channelId, source) {
681
+ let lastTimeout = 10;
682
+ this.#LrecoverChannelUpdateGap.debug(`recovering channel update gap [${channelId}, ${source}]`);
683
+ const pts_ = await this.#c.storage.channelPts.get([channelId]);
684
+ let pts = pts_ === null ? 1 : pts_;
685
+ let retryIn = 5;
686
+ while (true) {
687
+ const { access_hash } = await this.#c.getInputPeer(_1_utilities_js_1.ZERO_CHANNEL_ID + -Number(channelId)).then((v) => _2_tl_js_1.Api.as("inputPeerChannel", v));
688
+ let difference;
689
+ try {
690
+ difference = await this.#c.invoke({
691
+ _: "updates.getChannelDifference",
692
+ pts,
693
+ channel: { _: "inputChannel", channel_id: channelId, access_hash },
694
+ filter: { _: "channelMessagesFilterEmpty" },
695
+ limit: this.#c.storage.isBot ? _4_constants_js_1.CHANNEL_DIFFERENCE_LIMIT_BOT : _4_constants_js_1.CHANNEL_DIFFERENCE_LIMIT_USER,
696
+ });
697
+ lastTimeout = difference.timeout ?? lastTimeout;
698
+ }
699
+ catch (err) {
700
+ if (err instanceof _3_errors_js_1.PersistentTimestampInvalid) {
701
+ await (0, _0_deps_js_1.delay)(retryIn * _0_deps_js_1.SECOND);
702
+ retryIn += 5;
703
+ if (retryIn > 60) {
704
+ retryIn = 60;
705
+ }
706
+ continue;
707
+ }
708
+ else {
709
+ throw err;
710
+ }
711
+ }
712
+ if (_2_tl_js_1.Api.is("updates.channelDifference", difference)) {
713
+ this.processChats(difference.chats, difference);
714
+ this.processUsers(difference.users, difference);
715
+ for (const message of difference.new_messages) {
716
+ await this.#processUpdates({ _: "updateNewChannelMessage", message, pts: 0, pts_count: 0 }, false);
717
+ }
718
+ for (const update of difference.other_updates) {
719
+ await this.#processUpdates(update, false);
720
+ }
721
+ this.#c.storage.channelPts.set([channelId], difference.pts);
722
+ this.#LrecoverChannelUpdateGap.debug(`recovered from update gap [${channelId}, ${source}]`, channelId, source);
723
+ break;
724
+ }
725
+ else if (_2_tl_js_1.Api.is("updates.channelDifferenceTooLong", difference)) {
726
+ // TODO: invalidate messages
727
+ this.#LrecoverChannelUpdateGap.debug("received channelDifferenceTooLong");
728
+ this.processChats(difference.chats, difference);
729
+ this.processUsers(difference.users, difference);
730
+ for (const message of difference.messages) {
731
+ await this.#processUpdates({ _: "updateNewChannelMessage", message, pts: 0, pts_count: 0 }, false);
732
+ }
733
+ const pts_ = _2_tl_js_1.Api.as("dialog", difference.dialog).pts;
734
+ if (pts_ !== undefined) {
735
+ pts = pts_;
736
+ }
737
+ else {
738
+ (0, _0_deps_js_1.unreachable)();
739
+ }
740
+ this.#LrecoverChannelUpdateGap.debug("processed channelDifferenceTooLong");
741
+ }
742
+ else if (_2_tl_js_1.Api.is("updates.channelDifferenceEmpty", difference)) {
743
+ this.#LrecoverChannelUpdateGap.debug("there was no update gap");
744
+ break;
745
+ }
746
+ }
747
+ return lastTimeout;
748
+ }
749
+ #handleUpdatesSet = new Set();
750
+ async #handleStoredUpdates(boxId) {
751
+ if (this.#handleUpdatesSet.has(boxId)) {
752
+ return;
753
+ }
754
+ this.#handleUpdatesSet.add(boxId);
755
+ do {
756
+ const maybeUpdate = await this.#c.storage.getFirstUpdate(boxId);
757
+ if (maybeUpdate === null) {
758
+ break;
759
+ }
760
+ const [key, update] = maybeUpdate;
761
+ for (let i = 0; i < 100; ++i) {
762
+ try {
763
+ const handle = await this.#handleUpdate(update);
764
+ handle: for (let i = 0; i < 2; ++i) {
765
+ try {
766
+ await handle();
767
+ break handle;
768
+ }
769
+ catch {
770
+ continue handle;
771
+ }
772
+ }
773
+ break;
774
+ }
775
+ catch (err) {
776
+ this.#L$handleUpdate.error(err);
777
+ }
778
+ }
779
+ await this.#c.storage.set(key, null);
780
+ } while (true);
781
+ this.#handleUpdatesSet.delete(boxId);
782
+ }
783
+ #handleUpdate(update) {
784
+ const handler = this.#updateHandler;
785
+ if (handler) {
786
+ return handler(update);
787
+ }
788
+ else {
789
+ return Promise.resolve(() => Promise.resolve());
790
+ }
791
+ }
792
+ async #needsGetDifference(update) {
793
+ const chatIds = this.#collectChatIds(update);
794
+ if (!chatIds.size) {
795
+ return false;
796
+ }
797
+ return (await Promise.all(chatIds.values().map((v) => this.#c.messageStorage.peers.get([v])))).some((v) => !v);
798
+ }
799
+ #collectChatIds(object) {
800
+ const chatIds = new Set();
801
+ if (_2_tl_js_1.Api.is("messageFwdHeader", object)) {
802
+ if (object.from_id) {
803
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.from_id));
804
+ }
805
+ if (object.saved_from_peer) {
806
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.saved_from_peer));
807
+ }
808
+ return chatIds;
809
+ }
810
+ if (_2_tl_js_1.Api.isOfEnum("MessageMedia", object)) {
811
+ switch (object._) {
812
+ case "messageMediaContact":
813
+ if (object.user_id) {
814
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.user_id }));
815
+ }
816
+ break;
817
+ case "messageMediaStory":
818
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.peer));
819
+ break;
820
+ case "messageMediaGiveaway":
821
+ for (const chatId of object.channels.map((v) => (0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: v }))) {
822
+ chatIds.add(chatId);
823
+ }
824
+ break;
825
+ case "messageMediaGiveawayResults":
826
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: object.channel_id }));
827
+ for (const chatId of object.winners.map((user_id) => (0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }))) {
828
+ chatIds.add(chatId);
829
+ }
830
+ }
831
+ return chatIds;
832
+ }
833
+ // messsages
834
+ if (!("message" in object)) {
835
+ return chatIds;
836
+ }
837
+ if (_2_tl_js_1.Api.is("messageEmpty", object.message)) {
838
+ return chatIds;
839
+ }
840
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.peer_id));
841
+ if (object.message.from_id) {
842
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.from_id));
843
+ }
844
+ if (_2_tl_js_1.Api.is("messageService", object.message)) {
845
+ switch (object.message.action._) {
846
+ case "messageActionChatCreate":
847
+ case "messageActionChatAddUser":
848
+ case "messageActionInviteToGroupCall":
849
+ for (const user_id of object.message.action.users) {
850
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }));
851
+ }
852
+ break;
853
+ case "messageActionChatDeleteUser":
854
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.action.user_id }));
855
+ break;
856
+ case "messageActionChatMigrateTo":
857
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: object.message.action.channel_id }));
858
+ break;
859
+ case "messageActionChannelMigrateFrom":
860
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChat", chat_id: object.message.action.chat_id }));
861
+ break;
862
+ case "messageActionConferenceCall":
863
+ if (object.message.action.other_participants) {
864
+ for (const participant of object.message.action.other_participants) {
865
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(participant));
866
+ }
867
+ }
868
+ break;
869
+ case "messageActionPaymentRefunded":
870
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.action.peer));
871
+ break;
872
+ case "messageActionGiftCode":
873
+ if (object.message.action.boost_peer) {
874
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.action.boost_peer));
875
+ }
876
+ break;
877
+ case "messageActionRequestedPeer":
878
+ if (!this.#c.storage.isBot) {
879
+ for (const peer of object.message.action.peers) {
880
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(peer));
881
+ }
882
+ }
883
+ break;
884
+ case "messageActionSetMessagesTTL":
885
+ if (object.message.action.auto_setting_from) {
886
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.action.auto_setting_from }));
887
+ }
888
+ }
288
889
  }
890
+ else {
891
+ if (object.message.reply_to) {
892
+ switch (object.message.reply_to._) {
893
+ case "messageReplyHeader":
894
+ if (object.message.reply_to.reply_to_peer_id) {
895
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.reply_to.reply_to_peer_id));
896
+ }
897
+ if (object.message.reply_to.reply_from) {
898
+ for (const chatId of this.#collectChatIds(object.message.reply_to.reply_from)) {
899
+ chatIds.add(chatId);
900
+ }
901
+ }
902
+ if (object.message.reply_to.quote_entities) {
903
+ for (const chatId of this.#collectChatIdsFromEntities(object.message.reply_to.quote_entities)) {
904
+ chatIds.add(chatId);
905
+ }
906
+ }
907
+ if (object.message.reply_to.reply_media) {
908
+ for (const chatId of this.#collectChatIds(object.message.reply_to.reply_media)) {
909
+ chatIds.add(chatId);
910
+ }
911
+ }
912
+ break;
913
+ case "messageReplyStoryHeader":
914
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.reply_to.peer));
915
+ }
916
+ }
917
+ if (object.message.fwd_from) {
918
+ for (const chatId of this.#collectChatIds(object.message.fwd_from)) {
919
+ chatIds.add(chatId);
920
+ }
921
+ }
922
+ if (object.message.via_bot_id) {
923
+ chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.via_bot_id }));
924
+ }
925
+ if (object.message.entities) {
926
+ for (const chatId of this.#collectChatIdsFromEntities(object.message.entities)) {
927
+ chatIds.add(chatId);
928
+ }
929
+ }
930
+ if (object.message.media) {
931
+ for (const chatId of this.#collectChatIds(object.message.media)) {
932
+ chatIds.add(chatId);
933
+ }
934
+ }
935
+ }
936
+ return chatIds;
937
+ }
938
+ #collectChatIdsFromEntities(entities) {
939
+ const chatIds = new Array();
940
+ for (const user_id of entities.filter((v) => _2_tl_js_1.Api.is("messageEntityMentionName", v)).map((v) => v.user_id)) {
941
+ chatIds.push((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }));
942
+ }
943
+ return chatIds;
289
944
  }
290
945
  setUpdateHandler(handler) {
291
- __classPrivateFieldSet(this, _UpdateManager_updateHandler, handler, "f");
946
+ this.#updateHandler = handler;
292
947
  }
948
+ #openChats = new Map();
293
949
  async openChat(chatId, params) {
294
950
  if (params?.timeout !== undefined && (params.timeout < 0 || params?.timeout === 0)) {
295
951
  throw new _0_errors_js_1.InputError("An invalid timeout was specified.");
296
952
  }
297
- const channel = await __classPrivateFieldGet(this, _UpdateManager_c, "f").getInputChannel(chatId);
953
+ const channel = await this.#c.getInputChannel(chatId);
298
954
  const channelId = channel.channel_id;
299
- if (__classPrivateFieldGet(this, _UpdateManager_openChats, "f").has(channelId)) {
955
+ if (this.#openChats.has(channelId)) {
300
956
  return;
301
957
  }
302
958
  const controller = new AbortController();
303
959
  const promise = Promise.resolve().then(async () => {
304
- const logger = __classPrivateFieldGet(this, _UpdateManager_LopenChat, "f").branch(_2_tl_js_1.Api.peerToChatId(channel) + "");
960
+ const logger = this.#LopenChat.branch(_2_tl_js_1.Api.peerToChatId(channel) + "");
305
961
  while (true) {
306
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").isDisconnected()) {
962
+ if (this.#c.isDisconnected()) {
307
963
  logger.debug("disconnected, stopping the loop");
308
- __classPrivateFieldGet(this, _UpdateManager_openChats, "f").delete(channelId);
964
+ this.#openChats.delete(channelId);
309
965
  break;
310
966
  }
311
- if (!__classPrivateFieldGet(this, _UpdateManager_openChats, "f").has(channelId)) {
967
+ if (!this.#openChats.has(channelId)) {
312
968
  const aborted = controller.signal.aborted;
313
969
  logger.debug(`closed${(aborted ? " (aborted)" : "")}, stopping the loop`);
314
970
  break;
315
971
  }
316
972
  try {
317
973
  const Ti = Date.now();
318
- const otherTimeout = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_recoverChannelUpdateGap).call(this, channelId, "openChat");
974
+ const otherTimeout = await this.#recoverChannelUpdateGap(channelId, "openChat");
319
975
  const timeout = params?.timeout ?? otherTimeout;
320
976
  const dT = Date.now() - Ti;
321
977
  const delayMs = Math.max(timeout * _0_deps_js_1.SECOND - dT, 0);
@@ -325,20 +981,20 @@ class UpdateManager {
325
981
  }
326
982
  }
327
983
  catch (err) {
328
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").isDisconnected()) {
984
+ if (this.#c.isDisconnected()) {
329
985
  continue; // breaks the loop
330
986
  }
331
- __classPrivateFieldGet(this, _UpdateManager_LopenChat, "f").error("an unexpected error occurred:", err);
987
+ this.#LopenChat.error("an unexpected error occurred:", err);
332
988
  }
333
989
  }
334
990
  });
335
- __classPrivateFieldGet(this, _UpdateManager_openChats, "f").set(channelId, { controller, promise });
991
+ this.#openChats.set(channelId, { controller, promise });
336
992
  }
337
993
  async closeChat(chatId) {
338
- const { channel_id } = await __classPrivateFieldGet(this, _UpdateManager_c, "f").getInputChannel(chatId);
339
- const openChat = __classPrivateFieldGet(this, _UpdateManager_openChats, "f").get(channel_id);
994
+ const { channel_id } = await this.#c.getInputChannel(chatId);
995
+ const openChat = this.#openChats.get(channel_id);
340
996
  if (openChat) {
341
- __classPrivateFieldGet(this, _UpdateManager_openChats, "f").delete(channel_id);
997
+ this.#openChats.delete(channel_id);
342
998
  openChat.controller.abort();
343
999
  }
344
1000
  else {
@@ -346,662 +1002,11 @@ class UpdateManager {
346
1002
  }
347
1003
  }
348
1004
  closeAllChats() {
349
- for (const [channelId, openChat] of __classPrivateFieldGet(this, _UpdateManager_openChats, "f").entries()) {
350
- __classPrivateFieldGet(this, _UpdateManager_openChats, "f").delete(channelId);
1005
+ for (const [channelId, openChat] of this.#openChats.entries()) {
1006
+ this.#openChats.delete(channelId);
351
1007
  openChat.controller.abort();
352
1008
  }
353
1009
  }
354
1010
  }
355
1011
  exports.UpdateManager = UpdateManager;
356
- _a = UpdateManager, _UpdateManager_c = new WeakMap(), _UpdateManager_updateState = new WeakMap(), _UpdateManager_updateHandler = new WeakMap(), _UpdateManager_LrecoverUpdateGap = new WeakMap(), _UpdateManager_LrecoverChannelUpdateGap = new WeakMap(), _UpdateManager_L$handleUpdate = new WeakMap(), _UpdateManager_L$processUpdates = new WeakMap(), _UpdateManager_LfetchState = new WeakMap(), _UpdateManager_LopenChat = new WeakMap(), _UpdateManager_defaultDropPendingUpdates = new WeakMap(), _UpdateManager_state = new WeakMap(), _UpdateManager_handleUpdateQueues = new WeakMap(), _UpdateManager_nonFirst = new WeakMap(), _UpdateManager_channelUpdateQueues = new WeakMap(), _UpdateManager_ptsUpdateQueue = new WeakMap(), _UpdateManager_qtsUpdateQueue = new WeakMap(), _UpdateManager_processUpdatesQueue = new WeakMap(), _UpdateManager_recoveringUpdateGap = new WeakMap(), _UpdateManager_recoverUpdateGapMutex = new WeakMap(), _UpdateManager_handleUpdatesSet = new WeakMap(), _UpdateManager_openChats = new WeakMap(), _UpdateManager_instances = new WeakSet(), _UpdateManager_mustDropPendingUpdates = function _UpdateManager_mustDropPendingUpdates() {
357
- if (typeof __classPrivateFieldGet(this, _UpdateManager_c, "f").dropPendingUpdates === "boolean") {
358
- return __classPrivateFieldGet(this, _UpdateManager_c, "f").dropPendingUpdates;
359
- }
360
- if (__classPrivateFieldGet(this, _UpdateManager_defaultDropPendingUpdates, "f") === null) {
361
- __classPrivateFieldSet(this, _UpdateManager_defaultDropPendingUpdates, __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.isBot, "f");
362
- }
363
- return __classPrivateFieldGet(this, _UpdateManager_defaultDropPendingUpdates, "f");
364
- }, _UpdateManager_getState = async function _UpdateManager_getState() {
365
- if (__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_mustDropPendingUpdates).call(this)) {
366
- return __classPrivateFieldGet(this, _UpdateManager_state, "f") ?? null;
367
- }
368
- if (__classPrivateFieldGet(this, _UpdateManager_state, "f") !== null) {
369
- return __classPrivateFieldGet(this, _UpdateManager_state, "f");
370
- }
371
- const state = await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.getState();
372
- return __classPrivateFieldSet(this, _UpdateManager_state, state, "f");
373
- }, _UpdateManager_setState = async function _UpdateManager_setState(state) {
374
- __classPrivateFieldSet(this, _UpdateManager_state, state, "f");
375
- if (!__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_mustDropPendingUpdates).call(this)) {
376
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.setState(state);
377
- }
378
- }, _UpdateManager_extractMessages = function _UpdateManager_extractMessages(context) {
379
- const messages = new Array();
380
- if (Array.isArray(context)) {
381
- for (const item of context) {
382
- messages.push(...__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_extractMessages).call(this, item));
383
- }
384
- }
385
- else if (_2_tl_js_1.Api.isOneOf(["updates", "updatesCombined"], context)) {
386
- messages.push(...__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_extractMessages).call(this, context.updates));
387
- }
388
- else if (_2_tl_js_1.Api.isOneOf(["updates.difference", "updates.differenceSlice", "updates.channelDifference"], context)) {
389
- for (const message of context.new_messages) {
390
- if (_2_tl_js_1.Api.is("message", message)) {
391
- messages.push(message);
392
- }
393
- }
394
- messages.push(...__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_extractMessages).call(this, context.other_updates));
395
- }
396
- else if (_2_tl_js_1.Api.isOneOf(["updateNewMessage", "updateNewChannelMessage", "updateEditMessage", "updateEditChannelMessage", "updateBotNewBusinessMessage", "updateBotNewBusinessMessage"], context)) {
397
- if (_2_tl_js_1.Api.is("message", context.message)) {
398
- messages.push(context.message);
399
- }
400
- }
401
- else if (_2_tl_js_1.Api.is("message", context)) {
402
- messages.push(context);
403
- }
404
- else if (context !== null && typeof context === "object" && "messages" in context && Array.isArray(context.messages)) {
405
- for (const message of context.messages) {
406
- if (_2_tl_js_1.Api.is("message", message)) {
407
- messages.push(message);
408
- }
409
- }
410
- }
411
- return messages;
412
- }, _UpdateManager_extractMinPeerReferences = function _UpdateManager_extractMinPeerReferences(context) {
413
- const minPeerReferences = new Array();
414
- const messages = __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_extractMessages).call(this, context);
415
- for (const message of messages) {
416
- if (!message.from_id) {
417
- continue;
418
- }
419
- minPeerReferences.push({ chatId: _2_tl_js_1.Api.peerToChatId(message.peer_id), senderId: _2_tl_js_1.Api.peerToChatId(message.from_id), messageId: message.id });
420
- }
421
- return minPeerReferences;
422
- }, _UpdateManager_getChannelPtsWithDropPendingUpdatesCheck = async function _UpdateManager_getChannelPtsWithDropPendingUpdatesCheck(channelId) {
423
- if (!(__classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_mustDropPendingUpdates).call(this))) {
424
- return await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.get([channelId]);
425
- }
426
- const first = !__classPrivateFieldGet(this, _UpdateManager_nonFirst, "f").has(channelId);
427
- if (first) {
428
- __classPrivateFieldGet(this, _UpdateManager_nonFirst, "f").add(channelId);
429
- return null;
430
- }
431
- else {
432
- return await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.get([channelId]);
433
- }
434
- }, _UpdateManager_checkGap = async function _UpdateManager_checkGap(pts, ptsCount) {
435
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
436
- if (localState.pts + ptsCount < pts) {
437
- await this.recoverUpdateGap("processUpdates[pts]");
438
- }
439
- }, _UpdateManager_checkGapQts = async function _UpdateManager_checkGapQts(qts) {
440
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
441
- if (localState.qts + _a.QTS_COUNT < qts) {
442
- await this.recoverUpdateGap("processUpdates[qts]");
443
- }
444
- }, _UpdateManager_checkChannelGap = async function _UpdateManager_checkChannelGap(channelId, pts, ptsCount) {
445
- let localPts = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getChannelPtsWithDropPendingUpdatesCheck).call(this, channelId);
446
- if (!localPts) {
447
- localPts = pts - ptsCount;
448
- }
449
- if (localPts + ptsCount < pts) {
450
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_recoverChannelUpdateGap).call(this, channelId, "processUpdates");
451
- }
452
- }, _UpdateManager_processChannelPtsUpdateInner = async function _UpdateManager_processChannelPtsUpdateInner(update, checkGap) {
453
- const channelId = _2_tl_js_1.Api.is("updateNewChannelMessage", update) || _2_tl_js_1.Api.is("updateEditChannelMessage", update) ? _2_tl_js_1.Api.as("peerChannel", update.message.peer_id).channel_id : update.channel_id;
454
- if (_2_tl_js_1.Api.is("updateChannelTooLong", update)) {
455
- if (update.pts !== undefined) {
456
- __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.set([channelId], update.pts);
457
- }
458
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_recoverChannelUpdateGap).call(this, channelId, "updateChannelTooLong");
459
- return;
460
- }
461
- if (update.pts !== 0) {
462
- const ptsCount = update.pts_count;
463
- if (checkGap) {
464
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_checkChannelGap).call(this, channelId, update.pts, ptsCount);
465
- }
466
- let currentPts = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getChannelPtsWithDropPendingUpdatesCheck).call(this, channelId);
467
- currentPts ??= update.pts - ptsCount;
468
- if (currentPts + ptsCount > update.pts) {
469
- return;
470
- }
471
- }
472
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").guaranteeUpdateDelivery) {
473
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.setUpdate(channelId, update);
474
- }
475
- if (update.pts !== 0) {
476
- __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.set([channelId], update.pts);
477
- }
478
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_queueUpdate).call(this, update, channelId, true);
479
- }, _UpdateManager_queueUpdate = function _UpdateManager_queueUpdate(update, boxId, pts) {
480
- this.getHandleUpdateQueue(boxId).add(async () => {
481
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").guaranteeUpdateDelivery && pts) {
482
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_handleStoredUpdates).call(this, boxId);
483
- }
484
- else {
485
- await (await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_handleUpdate).call(this, update))();
486
- }
487
- });
488
- }, _UpdateManager_processChannelPtsUpdate = function _UpdateManager_processChannelPtsUpdate(update, checkGap) {
489
- const channelId = _2_tl_js_1.Api.is("updateNewChannelMessage", update) || _2_tl_js_1.Api.is("updateEditChannelMessage", update) ? _2_tl_js_1.Api.as("peerChannel", update.message.peer_id).channel_id : update.channel_id;
490
- let queue = __classPrivateFieldGet(this, _UpdateManager_channelUpdateQueues, "f").get(channelId);
491
- if (queue === undefined) {
492
- queue = new _1_utilities_js_1.Queue(`channelUpdates-${channelId}`);
493
- __classPrivateFieldGet(this, _UpdateManager_channelUpdateQueues, "f").set(channelId, queue);
494
- }
495
- queue.add(async () => {
496
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processChannelPtsUpdateInner).call(this, update, checkGap);
497
- });
498
- }, _UpdateManager_processPtsUpdateInner = async function _UpdateManager_processPtsUpdateInner(update, checkGap) {
499
- if (update.pts !== 0 && checkGap) {
500
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_checkGap).call(this, update.pts, update.pts_count);
501
- if (await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_needsGetDifference).call(this, update)) {
502
- await this.recoverUpdateGap("needsGetDifference");
503
- }
504
- }
505
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
506
- if (update.pts !== 0 && localState.pts + update.pts_count > update.pts) {
507
- return;
508
- }
509
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").guaranteeUpdateDelivery) {
510
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.setUpdate(_a.MAIN_BOX_ID, update);
511
- }
512
- if (update.pts !== 0) {
513
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdatePts).call(this, update.pts);
514
- }
515
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_queueUpdate).call(this, update, 1n, false);
516
- }, _UpdateManager_processPtsUpdate = function _UpdateManager_processPtsUpdate(update, checkGap) {
517
- __classPrivateFieldGet(this, _UpdateManager_ptsUpdateQueue, "f").add(async () => {
518
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processPtsUpdateInner).call(this, update, checkGap);
519
- });
520
- }, _UpdateManager_processQtsUpdateInner = async function _UpdateManager_processQtsUpdateInner(update, checkGap) {
521
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
522
- if (update.qts !== 0) {
523
- if (checkGap) {
524
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_checkGapQts).call(this, update.qts);
525
- }
526
- if (localState.qts + _a.QTS_COUNT > update.qts) {
527
- return;
528
- }
529
- }
530
- if (__classPrivateFieldGet(this, _UpdateManager_c, "f").guaranteeUpdateDelivery) {
531
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.setUpdate(_a.MAIN_BOX_ID, update);
532
- }
533
- if (update.qts !== 0) {
534
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdateQts).call(this, update.qts);
535
- }
536
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_queueUpdate).call(this, update, 0n, true);
537
- }, _UpdateManager_processQtsUpdate = function _UpdateManager_processQtsUpdate(update, checkGap) {
538
- __classPrivateFieldGet(this, _UpdateManager_qtsUpdateQueue, "f").add(async () => {
539
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processQtsUpdateInner).call(this, update, checkGap);
540
- });
541
- }, _UpdateManager_processUpdates = async function _UpdateManager_processUpdates(updates_, checkGap, call = null) {
542
- /// First, individual updates (Update[1]) are extracted from Updates.[2]
543
- ///
544
- /// If an updatesTooLong[3] was received, an update gap recovery is initiated and no further action will be taken.
545
- ///
546
- /// [1]: https://core.telegram.org/type/Update
547
- /// [2]: https://core.telegram.org/type/Updates
548
- /// [3]: https://core.telegram.org/constructor/updatesTooLong
549
- let updates;
550
- if (_2_tl_js_1.Api.is("updatesCombined", updates_) || _2_tl_js_1.Api.is("updates", updates_)) {
551
- updates = updates_.updates;
552
- const seq = updates_.seq;
553
- const seqStart = "seq_start" in updates_ ? updates_.seq_start : updates_.seq;
554
- if (checkGap) {
555
- if (seqStart === 0) {
556
- checkGap = false;
557
- __classPrivateFieldGet(this, _UpdateManager_L$processUpdates, "f").debug("seqStart=0");
558
- }
559
- else {
560
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
561
- const localSeq = localState.seq;
562
- if (localSeq + 1 === seqStart) {
563
- // The update sequence can be applied.
564
- localState.seq = seq;
565
- localState.date = updates_.date;
566
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdateStateDate).call(this, updates_.date);
567
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
568
- }
569
- else if (localSeq + 1 > seqStart) {
570
- // The update sequence was already applied, and must be ignored.
571
- __classPrivateFieldGet(this, _UpdateManager_L$processUpdates, "f").debug("localSeq + 1 > seqStart");
572
- return;
573
- }
574
- else if (localSeq + 1 < seqStart) {
575
- // There's an updates gap that must be filled.
576
- await this.recoverUpdateGap("localSeq + 1 < seqStart");
577
- }
578
- }
579
- }
580
- }
581
- else if (_2_tl_js_1.Api.is("updateShort", updates_)) {
582
- updates = [updates_.update];
583
- }
584
- else if (_2_tl_js_1.Api.is("updateShortMessage", updates_)) {
585
- updates = [
586
- {
587
- _: "updateNewMessage",
588
- message: {
589
- _: "message",
590
- out: updates_.out,
591
- mentioned: updates_.mentioned,
592
- media_unread: updates_.media_unread,
593
- silent: updates_.silent,
594
- id: updates_.id,
595
- from_id: updates_.out ? ({ _: "peerUser", user_id: BigInt(await __classPrivateFieldGet(this, _UpdateManager_c, "f").getSelfId()) }) : ({ _: "peerUser", user_id: updates_.user_id }),
596
- peer_id: { _: "peerUser", user_id: updates_.user_id },
597
- message: updates_.message,
598
- date: updates_.date,
599
- fwd_from: updates_.fwd_from,
600
- via_bot_id: updates_.via_bot_id,
601
- reply_to: updates_.reply_to,
602
- entities: updates_.entities,
603
- ttl_period: updates_.ttl_period,
604
- },
605
- pts: updates_.pts,
606
- pts_count: updates_.pts_count,
607
- },
608
- ];
609
- }
610
- else if (_2_tl_js_1.Api.is("updateShortChatMessage", updates_)) {
611
- updates = [
612
- {
613
- _: "updateNewMessage",
614
- message: {
615
- _: "message",
616
- mentioned: updates_.mentioned,
617
- media_unread: updates_.media_unread,
618
- silent: updates_.silent,
619
- id: updates_.id,
620
- from_id: { _: "peerUser", user_id: updates_.from_id },
621
- peer_id: { _: "peerChat", chat_id: updates_.chat_id },
622
- fwd_from: updates_.fwd_from,
623
- via_bot_id: updates_.via_bot_id,
624
- reply_to: updates_.reply_to,
625
- date: updates_.date,
626
- message: updates_.message,
627
- entities: updates_.entities,
628
- ttl_period: updates_.ttl_period,
629
- },
630
- pts: updates_.pts,
631
- pts_count: updates_.pts_count,
632
- },
633
- ];
634
- }
635
- else if (_2_tl_js_1.Api.is("updateShortSentMessage", updates_)) {
636
- if (!_2_tl_js_1.Api.is("messages.sendMessage", call)) {
637
- (0, _0_deps_js_1.unreachable)();
638
- }
639
- updates = [{
640
- _: "updateNewMessage",
641
- message: {
642
- _: "message",
643
- out: updates_.out,
644
- silent: call.silent,
645
- id: updates_.id,
646
- from_id: { _: "peerUser", user_id: BigInt(await __classPrivateFieldGet(this, _UpdateManager_c, "f").getSelfId()) },
647
- peer_id: _2_tl_js_1.Api.inputPeerToPeer(call.peer),
648
- message: call.message,
649
- media: updates_.media,
650
- date: updates_.date,
651
- // reply_to: call.reply_to, // TODO?
652
- entities: updates_.entities,
653
- ttl_period: updates_.ttl_period,
654
- },
655
- pts: updates_.pts,
656
- pts_count: updates_.pts_count,
657
- }];
658
- }
659
- else if (_2_tl_js_1.Api.is("updatesTooLong", updates_)) {
660
- await this.recoverUpdateGap("updatesTooLong");
661
- return;
662
- }
663
- else if (_2_tl_js_1.Api.isOfEnum("Update", updates_)) {
664
- updates = [updates_];
665
- }
666
- else {
667
- (0, _0_deps_js_1.unreachable)();
668
- }
669
- /// We process the updates when we are sure there is no gap.
670
- if (_2_tl_js_1.Api.is("updates", updates_) || _2_tl_js_1.Api.is("updatesCombined", updates_)) {
671
- this.processChats(updates_.chats, updates_);
672
- this.processUsers(updates_.users, updates_);
673
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdateStateDate).call(this, updates_.date);
674
- }
675
- else if (_2_tl_js_1.Api.isOneOf([
676
- "updateShort",
677
- "updateShortMessage",
678
- "updateShortChatMessage",
679
- "updateShortSentMessage",
680
- ], updates_)) {
681
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setUpdateStateDate).call(this, updates_.date);
682
- }
683
- for (const update of updates) {
684
- if (_2_tl_js_1.Api.is("updatePtsChanged", update)) {
685
- await this.fetchState("updatePtsChanged");
686
- if (__classPrivateFieldGet(this, _UpdateManager_updateState, "f")) {
687
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, __classPrivateFieldGet(this, _UpdateManager_updateState, "f"));
688
- }
689
- else {
690
- (0, _0_deps_js_1.unreachable)();
691
- }
692
- }
693
- else if (_a.isPtsUpdate(update)) {
694
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processPtsUpdate).call(this, update, checkGap);
695
- }
696
- else if (_a.isChannelPtsUpdate(update)) {
697
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processChannelPtsUpdate).call(this, update, checkGap);
698
- }
699
- else if (_a.isQtsUpdate(update)) {
700
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processQtsUpdate).call(this, update, checkGap);
701
- }
702
- else {
703
- __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_queueUpdate).call(this, update, 0n, false);
704
- }
705
- }
706
- }, _UpdateManager_setUpdateStateDate = async function _UpdateManager_setUpdateStateDate(date) {
707
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
708
- localState.date = date;
709
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
710
- }, _UpdateManager_setUpdatePts = async function _UpdateManager_setUpdatePts(pts) {
711
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
712
- localState.pts = pts;
713
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
714
- }, _UpdateManager_setUpdateQts = async function _UpdateManager_setUpdateQts(qts) {
715
- const localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getLocalState).call(this);
716
- localState.qts = qts;
717
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
718
- }, _UpdateManager_getLocalState = async function _UpdateManager_getLocalState() {
719
- let localState = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_getState).call(this);
720
- if (!localState) {
721
- if (__classPrivateFieldGet(this, _UpdateManager_updateState, "f")) {
722
- localState = __classPrivateFieldGet(this, _UpdateManager_updateState, "f");
723
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
724
- }
725
- else {
726
- await this.fetchState("getLocalState");
727
- if (__classPrivateFieldGet(this, _UpdateManager_updateState, "f")) {
728
- localState = __classPrivateFieldGet(this, _UpdateManager_updateState, "f");
729
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_setState).call(this, localState);
730
- }
731
- else {
732
- (0, _0_deps_js_1.unreachable)();
733
- }
734
- }
735
- }
736
- return localState;
737
- }, _UpdateManager_recoverChannelUpdateGap = async function _UpdateManager_recoverChannelUpdateGap(channelId, source) {
738
- let lastTimeout = 10;
739
- __classPrivateFieldGet(this, _UpdateManager_LrecoverChannelUpdateGap, "f").debug(`recovering channel update gap [${channelId}, ${source}]`);
740
- const pts_ = await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.get([channelId]);
741
- let pts = pts_ === null ? 1 : pts_;
742
- let retryIn = 5;
743
- while (true) {
744
- const { access_hash } = await __classPrivateFieldGet(this, _UpdateManager_c, "f").getInputPeer(_1_utilities_js_1.ZERO_CHANNEL_ID + -Number(channelId)).then((v) => _2_tl_js_1.Api.as("inputPeerChannel", v));
745
- let difference;
746
- try {
747
- difference = await __classPrivateFieldGet(this, _UpdateManager_c, "f").invoke({
748
- _: "updates.getChannelDifference",
749
- pts,
750
- channel: { _: "inputChannel", channel_id: channelId, access_hash },
751
- filter: { _: "channelMessagesFilterEmpty" },
752
- limit: __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.isBot ? _4_constants_js_1.CHANNEL_DIFFERENCE_LIMIT_BOT : _4_constants_js_1.CHANNEL_DIFFERENCE_LIMIT_USER,
753
- });
754
- lastTimeout = difference.timeout ?? lastTimeout;
755
- }
756
- catch (err) {
757
- if (err instanceof _3_errors_js_1.PersistentTimestampInvalid) {
758
- await (0, _0_deps_js_1.delay)(retryIn * _0_deps_js_1.SECOND);
759
- retryIn += 5;
760
- if (retryIn > 60) {
761
- retryIn = 60;
762
- }
763
- continue;
764
- }
765
- else {
766
- throw err;
767
- }
768
- }
769
- if (_2_tl_js_1.Api.is("updates.channelDifference", difference)) {
770
- this.processChats(difference.chats, difference);
771
- this.processUsers(difference.users, difference);
772
- for (const message of difference.new_messages) {
773
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, { _: "updateNewChannelMessage", message, pts: 0, pts_count: 0 }, false);
774
- }
775
- for (const update of difference.other_updates) {
776
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, update, false);
777
- }
778
- __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.channelPts.set([channelId], difference.pts);
779
- __classPrivateFieldGet(this, _UpdateManager_LrecoverChannelUpdateGap, "f").debug(`recovered from update gap [${channelId}, ${source}]`, channelId, source);
780
- break;
781
- }
782
- else if (_2_tl_js_1.Api.is("updates.channelDifferenceTooLong", difference)) {
783
- // TODO: invalidate messages
784
- __classPrivateFieldGet(this, _UpdateManager_LrecoverChannelUpdateGap, "f").debug("received channelDifferenceTooLong");
785
- this.processChats(difference.chats, difference);
786
- this.processUsers(difference.users, difference);
787
- for (const message of difference.messages) {
788
- await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_processUpdates).call(this, { _: "updateNewChannelMessage", message, pts: 0, pts_count: 0 }, false);
789
- }
790
- const pts_ = _2_tl_js_1.Api.as("dialog", difference.dialog).pts;
791
- if (pts_ !== undefined) {
792
- pts = pts_;
793
- }
794
- else {
795
- (0, _0_deps_js_1.unreachable)();
796
- }
797
- __classPrivateFieldGet(this, _UpdateManager_LrecoverChannelUpdateGap, "f").debug("processed channelDifferenceTooLong");
798
- }
799
- else if (_2_tl_js_1.Api.is("updates.channelDifferenceEmpty", difference)) {
800
- __classPrivateFieldGet(this, _UpdateManager_LrecoverChannelUpdateGap, "f").debug("there was no update gap");
801
- break;
802
- }
803
- }
804
- return lastTimeout;
805
- }, _UpdateManager_handleStoredUpdates = async function _UpdateManager_handleStoredUpdates(boxId) {
806
- if (__classPrivateFieldGet(this, _UpdateManager_handleUpdatesSet, "f").has(boxId)) {
807
- return;
808
- }
809
- __classPrivateFieldGet(this, _UpdateManager_handleUpdatesSet, "f").add(boxId);
810
- do {
811
- const maybeUpdate = await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.getFirstUpdate(boxId);
812
- if (maybeUpdate === null) {
813
- break;
814
- }
815
- const [key, update] = maybeUpdate;
816
- for (let i = 0; i < 100; ++i) {
817
- try {
818
- const handle = await __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_handleUpdate).call(this, update);
819
- handle: for (let i = 0; i < 2; ++i) {
820
- try {
821
- await handle();
822
- break handle;
823
- }
824
- catch {
825
- continue handle;
826
- }
827
- }
828
- break;
829
- }
830
- catch (err) {
831
- __classPrivateFieldGet(this, _UpdateManager_L$handleUpdate, "f").error(err);
832
- }
833
- }
834
- await __classPrivateFieldGet(this, _UpdateManager_c, "f").storage.set(key, null);
835
- } while (true);
836
- __classPrivateFieldGet(this, _UpdateManager_handleUpdatesSet, "f").delete(boxId);
837
- }, _UpdateManager_handleUpdate = function _UpdateManager_handleUpdate(update) {
838
- const handler = __classPrivateFieldGet(this, _UpdateManager_updateHandler, "f");
839
- if (handler) {
840
- return handler(update);
841
- }
842
- else {
843
- return Promise.resolve(() => Promise.resolve());
844
- }
845
- }, _UpdateManager_needsGetDifference = async function _UpdateManager_needsGetDifference(update) {
846
- const chatIds = __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIds).call(this, update);
847
- if (!chatIds.size) {
848
- return false;
849
- }
850
- return (await Promise.all(chatIds.values().map((v) => __classPrivateFieldGet(this, _UpdateManager_c, "f").messageStorage.peers.get([v])))).some((v) => !v);
851
- }, _UpdateManager_collectChatIds = function _UpdateManager_collectChatIds(object) {
852
- const chatIds = new Set();
853
- if (_2_tl_js_1.Api.is("messageFwdHeader", object)) {
854
- if (object.from_id) {
855
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.from_id));
856
- }
857
- if (object.saved_from_peer) {
858
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.saved_from_peer));
859
- }
860
- return chatIds;
861
- }
862
- if (_2_tl_js_1.Api.isOfEnum("MessageMedia", object)) {
863
- switch (object._) {
864
- case "messageMediaContact":
865
- if (object.user_id) {
866
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.user_id }));
867
- }
868
- break;
869
- case "messageMediaStory":
870
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.peer));
871
- break;
872
- case "messageMediaGiveaway":
873
- for (const chatId of object.channels.map((v) => (0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: v }))) {
874
- chatIds.add(chatId);
875
- }
876
- break;
877
- case "messageMediaGiveawayResults":
878
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: object.channel_id }));
879
- for (const chatId of object.winners.map((user_id) => (0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }))) {
880
- chatIds.add(chatId);
881
- }
882
- }
883
- return chatIds;
884
- }
885
- // messsages
886
- if (!("message" in object)) {
887
- return chatIds;
888
- }
889
- if (_2_tl_js_1.Api.is("messageEmpty", object.message)) {
890
- return chatIds;
891
- }
892
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.peer_id));
893
- if (object.message.from_id) {
894
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.from_id));
895
- }
896
- if (_2_tl_js_1.Api.is("messageService", object.message)) {
897
- switch (object.message.action._) {
898
- case "messageActionChatCreate":
899
- case "messageActionChatAddUser":
900
- case "messageActionInviteToGroupCall":
901
- for (const user_id of object.message.action.users) {
902
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }));
903
- }
904
- break;
905
- case "messageActionChatDeleteUser":
906
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.action.user_id }));
907
- break;
908
- case "messageActionChatMigrateTo":
909
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChannel", channel_id: object.message.action.channel_id }));
910
- break;
911
- case "messageActionChannelMigrateFrom":
912
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerChat", chat_id: object.message.action.chat_id }));
913
- break;
914
- case "messageActionConferenceCall":
915
- if (object.message.action.other_participants) {
916
- for (const participant of object.message.action.other_participants) {
917
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(participant));
918
- }
919
- }
920
- break;
921
- case "messageActionPaymentRefunded":
922
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.action.peer));
923
- break;
924
- case "messageActionGiftCode":
925
- if (object.message.action.boost_peer) {
926
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.action.boost_peer));
927
- }
928
- break;
929
- case "messageActionRequestedPeer":
930
- if (!__classPrivateFieldGet(this, _UpdateManager_c, "f").storage.isBot) {
931
- for (const peer of object.message.action.peers) {
932
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(peer));
933
- }
934
- }
935
- break;
936
- case "messageActionSetMessagesTTL":
937
- if (object.message.action.auto_setting_from) {
938
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.action.auto_setting_from }));
939
- }
940
- }
941
- }
942
- else {
943
- if (object.message.reply_to) {
944
- switch (object.message.reply_to._) {
945
- case "messageReplyHeader":
946
- if (object.message.reply_to.reply_to_peer_id) {
947
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.reply_to.reply_to_peer_id));
948
- }
949
- if (object.message.reply_to.reply_from) {
950
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIds).call(this, object.message.reply_to.reply_from)) {
951
- chatIds.add(chatId);
952
- }
953
- }
954
- if (object.message.reply_to.quote_entities) {
955
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIdsFromEntities).call(this, object.message.reply_to.quote_entities)) {
956
- chatIds.add(chatId);
957
- }
958
- }
959
- if (object.message.reply_to.reply_media) {
960
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIds).call(this, object.message.reply_to.reply_media)) {
961
- chatIds.add(chatId);
962
- }
963
- }
964
- break;
965
- case "messageReplyStoryHeader":
966
- chatIds.add((0, _2_telegram_js_1.peerToChatId)(object.message.reply_to.peer));
967
- }
968
- }
969
- if (object.message.fwd_from) {
970
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIds).call(this, object.message.fwd_from)) {
971
- chatIds.add(chatId);
972
- }
973
- }
974
- if (object.message.via_bot_id) {
975
- chatIds.add((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id: object.message.via_bot_id }));
976
- }
977
- if (object.message.entities) {
978
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIdsFromEntities).call(this, object.message.entities)) {
979
- chatIds.add(chatId);
980
- }
981
- }
982
- if (object.message.media) {
983
- for (const chatId of __classPrivateFieldGet(this, _UpdateManager_instances, "m", _UpdateManager_collectChatIds).call(this, object.message.media)) {
984
- chatIds.add(chatId);
985
- }
986
- }
987
- }
988
- return chatIds;
989
- }, _UpdateManager_collectChatIdsFromEntities = function _UpdateManager_collectChatIdsFromEntities(entities) {
990
- const chatIds = new Array();
991
- for (const user_id of entities.filter((v) => _2_tl_js_1.Api.is("messageEntityMentionName", v)).map((v) => v.user_id)) {
992
- chatIds.push((0, _2_telegram_js_1.peerToChatId)({ _: "peerUser", user_id }));
993
- }
994
- return chatIds;
995
- };
996
- Object.defineProperty(UpdateManager, "QTS_COUNT", {
997
- enumerable: true,
998
- configurable: true,
999
- writable: true,
1000
- value: 1
1001
- });
1002
- Object.defineProperty(UpdateManager, "MAIN_BOX_ID", {
1003
- enumerable: true,
1004
- configurable: true,
1005
- writable: true,
1006
- value: 0n
1007
- });
1012
+ _a = UpdateManager;