@mtkruto/node 0.1.108 → 0.1.110

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 (67) hide show
  1. package/esm/0_deps.d.ts +1 -1
  2. package/esm/0_deps.js +1 -1
  3. package/esm/4_constants.d.ts +1 -1
  4. package/esm/4_constants.js +1 -1
  5. package/esm/client/0_html.js +26 -14
  6. package/esm/client/0_utilities.d.ts +1 -1
  7. package/esm/client/0_utilities.js +1 -2
  8. package/esm/client/1_client_abstract.d.ts +1 -1
  9. package/esm/client/1_client_abstract.js +16 -8
  10. package/esm/client/2_client_plain.d.ts +1 -2
  11. package/esm/client/2_client_plain.js +20 -15
  12. package/esm/client/4_client.d.ts +4 -55
  13. package/esm/client/4_client.js +619 -716
  14. package/esm/connection/1_connection_web_socket.d.ts +1 -9
  15. package/esm/connection/1_connection_web_socket.js +92 -114
  16. package/esm/storage/0_storage.d.ts +3 -2
  17. package/esm/storage/0_storage.js +35 -26
  18. package/esm/storage/0_utilities.d.ts +1 -1
  19. package/esm/storage/0_utilities.js +57 -27
  20. package/esm/storage/0_utilities_test.d.ts +1 -0
  21. package/esm/storage/1_storage_local_storage.d.ts +1 -1
  22. package/esm/storage/1_storage_session_storage.d.ts +1 -1
  23. package/esm/transport/1_transport_abridged.d.ts +1 -2
  24. package/esm/transport/1_transport_abridged.js +24 -19
  25. package/esm/transport/1_transport_intermediate.d.ts +1 -2
  26. package/esm/transport/1_transport_intermediate.js +23 -18
  27. package/esm/types/0_color.d.ts +2 -1
  28. package/esm/types/0_color.js +21 -1
  29. package/esm/types/1_chat.js +5 -5
  30. package/esm/types/1_user.js +2 -2
  31. package/esm/utilities/0_base64.js +3 -3
  32. package/esm/utilities/0_queue.d.ts +2 -4
  33. package/esm/utilities/0_queue.js +39 -36
  34. package/package.json +1 -1
  35. package/script/0_deps.d.ts +1 -1
  36. package/script/0_deps.js +3 -3
  37. package/script/4_constants.d.ts +1 -1
  38. package/script/4_constants.js +1 -1
  39. package/script/client/0_html.js +26 -14
  40. package/script/client/0_utilities.d.ts +1 -1
  41. package/script/client/0_utilities.js +1 -2
  42. package/script/client/1_client_abstract.d.ts +1 -1
  43. package/script/client/1_client_abstract.js +16 -8
  44. package/script/client/2_client_plain.d.ts +1 -2
  45. package/script/client/2_client_plain.js +20 -15
  46. package/script/client/4_client.d.ts +4 -55
  47. package/script/client/4_client.js +619 -716
  48. package/script/connection/1_connection_web_socket.d.ts +1 -9
  49. package/script/connection/1_connection_web_socket.js +93 -115
  50. package/script/storage/0_storage.d.ts +3 -2
  51. package/script/storage/0_storage.js +35 -26
  52. package/script/storage/0_utilities.d.ts +1 -1
  53. package/script/storage/0_utilities.js +56 -26
  54. package/script/storage/0_utilities_test.d.ts +1 -0
  55. package/script/storage/1_storage_local_storage.d.ts +1 -1
  56. package/script/storage/1_storage_session_storage.d.ts +1 -1
  57. package/script/transport/1_transport_abridged.d.ts +1 -2
  58. package/script/transport/1_transport_abridged.js +24 -19
  59. package/script/transport/1_transport_intermediate.d.ts +1 -2
  60. package/script/transport/1_transport_intermediate.js +23 -18
  61. package/script/types/0_color.d.ts +2 -1
  62. package/script/types/0_color.js +24 -3
  63. package/script/types/1_chat.js +4 -4
  64. package/script/types/1_user.js +1 -1
  65. package/script/utilities/0_base64.js +2 -2
  66. package/script/utilities/0_queue.d.ts +2 -4
  67. package/script/utilities/0_queue.js +39 -36
@@ -1,3 +1,15 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Client_instances, _a, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_updateState, _Client_errorHandler, _Client_publicKeys, _Client_autoStart, _Client_propagateConnectionState, _Client_lastPropagatedConnectionState, _Client_storageInited, _Client_setAuth, _Client_authKeyWasCreated, _Client_connectMutex, _Client_assertUser, _Client_assertBot, _Client_fetchState, _Client_connectionInited, _Client_initConnection, _Client_lastPropagatedAuthorizationState, _Client_propagateAuthorizationState, _Client_selfId, _Client_getSelfId, _Client_receiveLoop, _Client_pingLoop, _Client_lastOutgoing, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_processChats, _Client_processUsers, _Client_handleUpdateQueue, _Client_processUpdatesQueue, _Client_checkChannelGap, _Client_processUpdates, _Client_setUpdateStateDate, _Client_getLocalState, _Client_recoverUpdateGap, _Client_recoverChannelUpdateGap, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_updatesToMessages, _Client_resolveSendAs, _Client_parseText, _Client_getMessagesInner, _Client_downloadInner, _Client_handleUpdate, _Client_usernameResolver, _Client_constructReplyMarkup, _Client_assertMsgHas, _Client_handle, _Client_setMyInfo, _Client_getMyInfo;
1
13
  import { debug, gunzip, Mutex } from "../0_deps.js";
2
14
  import { bigIntFromBuffer, drop, getRandomBigInt, getRandomId, mod, mustPrompt, mustPromptOneOf, Queue, sha1, UNREACHABLE } from "../1_utilities.js";
3
15
  import { as, functions, getChannelChatId, Message_, MessageContainer, peerToChatId, RPCResult, TLError, TLReader, types } from "../2_tl.js";
@@ -35,6 +47,7 @@ export class Client extends ClientAbstract {
35
47
  */
36
48
  constructor(storage, apiId = 0, apiHash = "", params) {
37
49
  super(params);
50
+ _Client_instances.add(this);
38
51
  Object.defineProperty(this, "apiId", {
39
52
  enumerable: true,
40
53
  configurable: true,
@@ -47,48 +60,13 @@ export class Client extends ClientAbstract {
47
60
  writable: true,
48
61
  value: apiHash
49
62
  });
50
- Object.defineProperty(this, "auth", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: null
55
- });
56
- Object.defineProperty(this, "sessionId", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: getRandomBigInt(8, true, false)
61
- });
62
- Object.defineProperty(this, "state", {
63
- enumerable: true,
64
- configurable: true,
65
- writable: true,
66
- value: { salt: 0n, seqNo: 0 }
67
- });
68
- Object.defineProperty(this, "promises", {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: new Map()
73
- });
74
- Object.defineProperty(this, "toAcknowledge", {
75
- enumerable: true,
76
- configurable: true,
77
- writable: true,
78
- value: new Set()
79
- });
80
- Object.defineProperty(this, "updateState", {
81
- enumerable: true,
82
- configurable: true,
83
- writable: true,
84
- value: void 0
85
- });
86
- Object.defineProperty(this, "errorHandler", {
87
- enumerable: true,
88
- configurable: true,
89
- writable: true,
90
- value: void 0
91
- });
63
+ _Client_auth.set(this, null);
64
+ _Client_sessionId.set(this, getRandomBigInt(8, true, false));
65
+ _Client_state.set(this, { salt: 0n, seqNo: 0 });
66
+ _Client_promises.set(this, new Map());
67
+ _Client_toAcknowledge.set(this, new Set());
68
+ _Client_updateState.set(this, void 0);
69
+ _Client_errorHandler.set(this, void 0);
92
70
  Object.defineProperty(this, "storage", {
93
71
  enumerable: true,
94
72
  configurable: true,
@@ -137,35 +115,20 @@ export class Client extends ClientAbstract {
137
115
  writable: true,
138
116
  value: void 0
139
117
  });
140
- Object.defineProperty(this, "publicKeys", {
141
- enumerable: true,
142
- configurable: true,
143
- writable: true,
144
- value: void 0
145
- });
146
- Object.defineProperty(this, "autoStart", {
147
- enumerable: true,
148
- configurable: true,
149
- writable: true,
150
- value: void 0
151
- });
152
- Object.defineProperty(this, "lastPropagatedConnectionState", {
153
- enumerable: true,
154
- configurable: true,
155
- writable: true,
156
- value: null
157
- });
118
+ _Client_publicKeys.set(this, void 0);
119
+ _Client_autoStart.set(this, void 0);
120
+ _Client_lastPropagatedConnectionState.set(this, null);
158
121
  Object.defineProperty(this, "stateChangeHandler", {
159
122
  enumerable: true,
160
123
  configurable: true,
161
124
  writable: true,
162
125
  value: ((connected) => {
163
- this.connectMutex.acquire().then(async (release) => {
126
+ __classPrivateFieldGet(this, _Client_connectMutex, "f").acquire().then(async (release) => {
164
127
  try {
165
128
  const connectionState = connected ? "ready" : "notConnected";
166
- if (this.connected == connected && this.lastPropagatedConnectionState != connectionState) {
167
- await this.propagateConnectionState(connectionState);
168
- this.lastPropagatedConnectionState = connectionState;
129
+ if (this.connected == connected && __classPrivateFieldGet(this, _Client_lastPropagatedConnectionState, "f") != connectionState) {
130
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, connectionState);
131
+ __classPrivateFieldSet(this, _Client_lastPropagatedConnectionState, connectionState, "f");
169
132
  }
170
133
  }
171
134
  finally {
@@ -174,81 +137,23 @@ export class Client extends ClientAbstract {
174
137
  });
175
138
  }).bind(this)
176
139
  });
177
- Object.defineProperty(this, "storageInited", {
178
- enumerable: true,
179
- configurable: true,
180
- writable: true,
181
- value: false
182
- });
183
- Object.defineProperty(this, "connectMutex", {
184
- enumerable: true,
185
- configurable: true,
186
- writable: true,
187
- value: new Mutex()
188
- });
189
- Object.defineProperty(this, "connectionInited", {
190
- enumerable: true,
191
- configurable: true,
192
- writable: true,
193
- value: false
194
- });
195
- Object.defineProperty(this, "lastPropagatedAuthorizationState", {
196
- enumerable: true,
197
- configurable: true,
198
- writable: true,
199
- value: null
200
- });
201
- Object.defineProperty(this, "selfId", {
202
- enumerable: true,
203
- configurable: true,
204
- writable: true,
205
- value: null
206
- });
207
- Object.defineProperty(this, "pingLoopStarted", {
208
- enumerable: true,
209
- configurable: true,
210
- writable: true,
211
- value: false
212
- });
213
- Object.defineProperty(this, "autoStarted", {
214
- enumerable: true,
215
- configurable: true,
216
- writable: true,
217
- value: false
218
- });
219
- Object.defineProperty(this, "lastMsgId", {
220
- enumerable: true,
221
- configurable: true,
222
- writable: true,
223
- value: 0n
224
- });
225
- Object.defineProperty(this, "handleUpdateQueue", {
226
- enumerable: true,
227
- configurable: true,
228
- writable: true,
229
- value: new Queue("handleUpdate")
230
- });
231
- Object.defineProperty(this, "processUpdatesQueue", {
232
- enumerable: true,
233
- configurable: true,
234
- writable: true,
235
- value: new Queue("processUpdates")
236
- });
237
- Object.defineProperty(this, "usernameResolver", {
238
- enumerable: true,
239
- configurable: true,
240
- writable: true,
241
- value: async (v) => {
242
- const inputPeer = await this.getInputPeer(v).then((v) => v[as](types.InputPeerUser));
243
- return new types.InputUser({ userId: inputPeer.userId, accessHash: inputPeer.accessHash });
244
- }
245
- });
246
- Object.defineProperty(this, "handle", {
247
- enumerable: true,
248
- configurable: true,
249
- writable: true,
250
- value: skip
140
+ _Client_storageInited.set(this, false);
141
+ _Client_authKeyWasCreated.set(this, true);
142
+ _Client_connectMutex.set(this, new Mutex());
143
+ _Client_connectionInited.set(this, false);
144
+ _Client_lastPropagatedAuthorizationState.set(this, null);
145
+ _Client_selfId.set(this, null);
146
+ _Client_lastOutgoing.set(this, null);
147
+ _Client_pingLoopStarted.set(this, false);
148
+ _Client_autoStarted.set(this, false);
149
+ _Client_lastMsgId.set(this, 0n);
150
+ _Client_handleUpdateQueue.set(this, new Queue("handleUpdate"));
151
+ _Client_processUpdatesQueue.set(this, new Queue("processUpdates"));
152
+ _Client_usernameResolver.set(this, async (v) => {
153
+ const inputPeer = await this.getInputPeer(v).then((v) => v[as](types.InputPeerUser));
154
+ return new types.InputUser({ userId: inputPeer.userId, accessHash: inputPeer.accessHash });
251
155
  });
156
+ _Client_handle.set(this, skip);
252
157
  this.storage = storage ?? new StorageMemory();
253
158
  this.parseMode = params?.parseMode ?? null;
254
159
  this.appVersion = params?.appVersion ?? APP_VERSION;
@@ -257,12 +162,9 @@ export class Client extends ClientAbstract {
257
162
  this.langPack = params?.langPack ?? LANG_PACK;
258
163
  this.systemLangCode = params?.systemLangCode ?? SYSTEM_LANG_CODE;
259
164
  this.systemVersion = params?.systemVersion ?? SYSTEM_VERSION;
260
- this.publicKeys = params?.publicKeys;
261
- this.autoStart = params?.autoStart ?? true;
262
- this.errorHandler = params?.errorHandler;
263
- }
264
- propagateConnectionState(connectionState) {
265
- return this.handle({ connectionState }, resolve);
165
+ __classPrivateFieldSet(this, _Client_publicKeys, params?.publicKeys, "f");
166
+ __classPrivateFieldSet(this, _Client_autoStart, params?.autoStart ?? true, "f");
167
+ __classPrivateFieldSet(this, _Client_errorHandler, params?.errorHandler, "f");
266
168
  }
267
169
  /**
268
170
  * Sets the DC and resets the auth key stored in the session provider
@@ -271,9 +173,9 @@ export class Client extends ClientAbstract {
271
173
  * @param dc The DC to change to.
272
174
  */
273
175
  async setDc(dc) {
274
- if (!this.storageInited) {
176
+ if (!__classPrivateFieldGet(this, _Client_storageInited, "f")) {
275
177
  await this.storage.init();
276
- this.storageInited = true;
178
+ __classPrivateFieldSet(this, _Client_storageInited, true, "f");
277
179
  }
278
180
  if (await this.storage.getDc() != dc) {
279
181
  await this.storage.setDc(dc);
@@ -282,11 +184,6 @@ export class Client extends ClientAbstract {
282
184
  }
283
185
  super.setDc(dc);
284
186
  }
285
- async setAuth(key) {
286
- const hash = await sha1(key);
287
- const id = bigIntFromBuffer(hash.slice(-8), true, false);
288
- this.auth = { key, id };
289
- }
290
187
  /**
291
188
  * Loads the session if `setDc` was not called, initializes and connnects
292
189
  * a `ClientPlain` to generate auth key if there was none, and connects the client.
@@ -296,15 +193,15 @@ export class Client extends ClientAbstract {
296
193
  if (this.connected) {
297
194
  return;
298
195
  }
299
- const release = await this.connectMutex.acquire();
196
+ const release = await __classPrivateFieldGet(this, _Client_connectMutex, "f").acquire();
300
197
  try {
301
- if (!this.storageInited) {
198
+ if (!__classPrivateFieldGet(this, _Client_storageInited, "f")) {
302
199
  await this.storage.init();
303
- this.storageInited = true;
200
+ __classPrivateFieldSet(this, _Client_storageInited, true, "f");
304
201
  }
305
202
  const authKey = await this.storage.getAuthKey();
306
203
  if (authKey == null) {
307
- const plain = new ClientPlain({ initialDc: this.initialDc, transportProvider: this.transportProvider, cdn: this.cdn, publicKeys: this.publicKeys });
204
+ const plain = new ClientPlain({ initialDc: this.initialDc, transportProvider: this.transportProvider, cdn: this.cdn, publicKeys: __classPrivateFieldGet(this, _Client_publicKeys, "f") });
308
205
  const dc = await this.storage.getDc();
309
206
  if (dc != null) {
310
207
  plain.setDc(dc);
@@ -313,11 +210,16 @@ export class Client extends ClientAbstract {
313
210
  const { authKey, salt } = await plain.createAuthKey();
314
211
  await plain.disconnect();
315
212
  await this.storage.setAuthKey(authKey);
316
- await this.setAuth(authKey);
317
- this.state.salt = salt;
213
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setAuth).call(this, authKey);
214
+ __classPrivateFieldGet(this, _Client_state, "f").salt = salt;
215
+ await this.storage.setServerSalt(salt);
318
216
  }
319
217
  else {
320
- await this.setAuth(authKey);
218
+ if (__classPrivateFieldGet(this, _Client_state, "f").salt == 0n) {
219
+ __classPrivateFieldGet(this, _Client_state, "f").salt = await this.storage.getServerSalt() ?? 0n;
220
+ }
221
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setAuth).call(this, authKey);
222
+ __classPrivateFieldSet(this, _Client_authKeyWasCreated, false, "f");
321
223
  }
322
224
  const dc = await this.storage.getDc();
323
225
  if (dc != null) {
@@ -328,28 +230,31 @@ export class Client extends ClientAbstract {
328
230
  await this.storage.setDc(this.initialDc);
329
231
  }
330
232
  d("encrypted client connected");
331
- drop(this.receiveLoop());
233
+ drop(__classPrivateFieldGet(this, _Client_instances, "m", _Client_receiveLoop).call(this));
332
234
  }
333
235
  finally {
334
236
  release();
335
237
  }
336
238
  }
337
- async assertUser(source) {
239
+ async [(_Client_auth = new WeakMap(), _Client_sessionId = new WeakMap(), _Client_state = new WeakMap(), _Client_promises = new WeakMap(), _Client_toAcknowledge = new WeakMap(), _Client_updateState = new WeakMap(), _Client_errorHandler = new WeakMap(), _Client_publicKeys = new WeakMap(), _Client_autoStart = new WeakMap(), _Client_lastPropagatedConnectionState = new WeakMap(), _Client_storageInited = new WeakMap(), _Client_authKeyWasCreated = new WeakMap(), _Client_connectMutex = new WeakMap(), _Client_connectionInited = new WeakMap(), _Client_lastPropagatedAuthorizationState = new WeakMap(), _Client_selfId = new WeakMap(), _Client_lastOutgoing = new WeakMap(), _Client_pingLoopStarted = new WeakMap(), _Client_autoStarted = new WeakMap(), _Client_lastMsgId = new WeakMap(), _Client_handleUpdateQueue = new WeakMap(), _Client_processUpdatesQueue = new WeakMap(), _Client_usernameResolver = new WeakMap(), _Client_handle = new WeakMap(), _Client_instances = new WeakSet(), _Client_propagateConnectionState = function _Client_propagateConnectionState(connectionState) {
240
+ return __classPrivateFieldGet(this, _Client_handle, "f").call(this, { connectionState }, resolve);
241
+ }, _Client_setAuth = async function _Client_setAuth(key) {
242
+ const hash = await sha1(key);
243
+ const id = bigIntFromBuffer(hash.slice(-8), true, false);
244
+ __classPrivateFieldSet(this, _Client_auth, { key, id }, "f");
245
+ }, _Client_assertUser = async function _Client_assertUser(source) {
338
246
  if (await this.storage.getAccountType() != "user") {
339
247
  throw new Error(`${source}: not user a client`);
340
248
  }
341
- }
342
- async assertBot(source) {
249
+ }, _Client_assertBot = async function _Client_assertBot(source) {
343
250
  if (await this.storage.getAccountType() != "bot") {
344
251
  throw new Error(`${source}: not a bot client`);
345
252
  }
346
- }
347
- async fetchState(source) {
253
+ }, _Client_fetchState = async function _Client_fetchState(source) {
348
254
  const state = await this.invoke(new functions.UpdatesGetState());
349
- this.updateState = state;
255
+ __classPrivateFieldSet(this, _Client_updateState, state, "f");
350
256
  d("state fetched [%s]", source);
351
- }
352
- async [handleMigrationError](err) {
257
+ }, handleMigrationError)](err) {
353
258
  let newDc = String(err.dc);
354
259
  if (Math.abs(this.dcId) >= 10000) {
355
260
  newDc += "-test";
@@ -358,40 +263,9 @@ export class Client extends ClientAbstract {
358
263
  d("migrated to DC%s", newDc);
359
264
  }
360
265
  disconnect() {
361
- this.connectionInited = false;
266
+ __classPrivateFieldSet(this, _Client_connectionInited, false, "f");
362
267
  return super.disconnect();
363
268
  }
364
- async initConnection() {
365
- if (!this.connectionInited) {
366
- await this.invoke(new functions.InitConnection({
367
- apiId: this.apiId,
368
- appVersion: this.appVersion,
369
- deviceModel: this.deviceModel,
370
- langCode: this.langCode,
371
- langPack: this.langPack,
372
- query: new functions.InvokeWithLayer({
373
- layer: LAYER,
374
- query: new functions.HelpGetConfig(),
375
- }),
376
- systemLangCode: this.systemLangCode,
377
- systemVersion: this.systemVersion,
378
- }));
379
- this.connectionInited = true;
380
- d("connection inited");
381
- }
382
- }
383
- async propagateAuthorizationState(authorized) {
384
- if (this.lastPropagatedAuthorizationState != authorized) {
385
- await this.handle({ authorizationState: { authorized } }, resolve);
386
- this.lastPropagatedAuthorizationState = authorized;
387
- }
388
- }
389
- async getSelfId() {
390
- if (this.selfId == null) {
391
- this.selfId = await this.getMe().then((v) => v.id);
392
- }
393
- return this.selfId;
394
- }
395
269
  /**
396
270
  * Calls [initConnection](1) and authorizes the client with one of the following:
397
271
  *
@@ -426,10 +300,10 @@ export class Client extends ClientAbstract {
426
300
  }
427
301
  }
428
302
  dAuth("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
429
- await this.initConnection();
303
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initConnection).call(this);
430
304
  try {
431
- await this.fetchState("authorize");
432
- await this.propagateAuthorizationState(true);
305
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "authorize");
306
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, true);
433
307
  d("already authorized");
434
308
  return;
435
309
  }
@@ -442,14 +316,14 @@ export class Client extends ClientAbstract {
442
316
  while (true) {
443
317
  try {
444
318
  const auth = await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
445
- this.selfId = Number(auth[as](types.AuthAuthorization).user.id);
319
+ __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.AuthAuthorization).user.id), "f");
446
320
  await this.storage.setAccountType("bot");
447
321
  break;
448
322
  }
449
323
  catch (err) {
450
324
  if (err instanceof Migrate) {
451
325
  await this[handleMigrationError](err);
452
- await this.initConnection();
326
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initConnection).call(this);
453
327
  continue;
454
328
  }
455
329
  else {
@@ -458,8 +332,8 @@ export class Client extends ClientAbstract {
458
332
  }
459
333
  }
460
334
  dAuth("authorized as bot");
461
- await this.propagateAuthorizationState(true);
462
- await this.fetchState("authorize");
335
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, true);
336
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "authorize");
463
337
  return;
464
338
  }
465
339
  if (params instanceof types.AuthExportedAuthorization) {
@@ -467,89 +341,187 @@ export class Client extends ClientAbstract {
467
341
  dAuth("authorization imported");
468
342
  return;
469
343
  }
470
- let phone;
471
- let sentCode;
472
- while (true) {
344
+ auth: while (true) {
473
345
  try {
474
- phone = typeof params.phone === "string" ? params.phone : await params.phone();
475
- const sendCode = () => this.invoke(new functions.AuthSendCode({
476
- phoneNumber: phone,
477
- apiId: this.apiId,
478
- apiHash: this.apiHash,
479
- settings: new types.CodeSettings(),
480
- })).then((v) => v[as](types.AuthSentCode));
481
- try {
482
- sentCode = await sendCode();
346
+ let phone;
347
+ let sentCode;
348
+ while (true) {
349
+ try {
350
+ phone = typeof params.phone === "string" ? params.phone : await params.phone();
351
+ const sendCode = () => this.invoke(new functions.AuthSendCode({
352
+ phoneNumber: phone,
353
+ apiId: this.apiId,
354
+ apiHash: this.apiHash,
355
+ settings: new types.CodeSettings(),
356
+ })).then((v) => v[as](types.AuthSentCode));
357
+ try {
358
+ sentCode = await sendCode();
359
+ }
360
+ catch (err) {
361
+ if (err instanceof Migrate) {
362
+ await this[handleMigrationError](err);
363
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initConnection).call(this);
364
+ sentCode = await sendCode();
365
+ }
366
+ else {
367
+ throw err;
368
+ }
369
+ }
370
+ break;
371
+ }
372
+ catch (err) {
373
+ if (err instanceof PhoneNumberInvalid) {
374
+ continue;
375
+ }
376
+ else {
377
+ throw err;
378
+ }
379
+ }
483
380
  }
484
- catch (err) {
485
- if (err instanceof Migrate) {
486
- await this[handleMigrationError](err);
487
- await this.initConnection();
488
- sentCode = await sendCode();
381
+ dAuth("verification code sent");
382
+ let err;
383
+ code: while (true) {
384
+ const code = typeof params.code === "string" ? params.code : await params.code();
385
+ try {
386
+ const auth = await this.invoke(new functions.AuthSignIn({
387
+ phoneNumber: phone,
388
+ phoneCode: code,
389
+ phoneCodeHash: sentCode.phoneCodeHash,
390
+ }));
391
+ __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.AuthAuthorization).user.id), "f");
392
+ await this.storage.setAccountType("user");
393
+ dAuth("authorized as user");
394
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, true);
395
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "authorize");
396
+ return;
489
397
  }
490
- else {
491
- throw err;
398
+ catch (err_) {
399
+ if (err_ instanceof types.RPCError && err_.errorMessage == "PHONE_CODE_INVALID") {
400
+ continue code;
401
+ }
402
+ else {
403
+ err = err_;
404
+ break code;
405
+ }
406
+ }
407
+ }
408
+ if (!(err instanceof SessionPasswordNeeded)) {
409
+ throw err;
410
+ }
411
+ password: while (true) {
412
+ const ap = await this.invoke(new functions.AccountGetPassword());
413
+ if (!(ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
414
+ throw new Error(`Handling ${ap.currentAlgo?.constructor.name} not implemented`);
415
+ }
416
+ try {
417
+ const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
418
+ const input = await checkPassword(password, ap);
419
+ const auth = await this.invoke(new functions.AuthCheckPassword({ password: input }));
420
+ __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.AuthAuthorization).user.id), "f");
421
+ await this.storage.setAccountType("user");
422
+ dAuth("authorized as user");
423
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, true);
424
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "authorize");
425
+ break password;
426
+ }
427
+ catch (err) {
428
+ if (err instanceof PasswordHashInvalid) {
429
+ continue password;
430
+ }
431
+ else {
432
+ throw err;
433
+ }
492
434
  }
493
435
  }
494
- break;
495
436
  }
496
437
  catch (err) {
497
- if (err instanceof PhoneNumberInvalid) {
498
- continue;
438
+ if (err == restartAuth) {
439
+ continue auth;
499
440
  }
500
441
  else {
501
442
  throw err;
502
443
  }
503
444
  }
504
445
  }
505
- dAuth("verification code sent");
506
- let err;
507
- while (true) {
508
- const code = typeof params.code === "string" ? params.code : await params.code();
509
- try {
510
- const auth = await this.invoke(new functions.AuthSignIn({
511
- phoneNumber: phone,
512
- phoneCode: code,
513
- phoneCodeHash: sentCode.phoneCodeHash,
514
- }));
515
- this.selfId = Number(auth[as](types.AuthAuthorization).user.id);
516
- await this.storage.setAccountType("user");
517
- dAuth("authorized as user");
518
- await this.propagateAuthorizationState(true);
519
- await this.fetchState("authorize");
520
- return;
521
- }
522
- catch (err_) {
523
- if (err_ instanceof types.RPCError && err_.errorMessage == "PHONE_CODE_INVALID") {
524
- continue;
525
- }
526
- else {
527
- err = err_;
528
- break;
529
- }
446
+ }
447
+ /**
448
+ * Same as calling `.connect()` followed by `.authorize(params)` if the session didn't have an auth key.
449
+ */
450
+ async start(params) {
451
+ await this.connect();
452
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_initConnection).call(this);
453
+ if (!__classPrivateFieldGet(this, _Client_authKeyWasCreated, "f")) {
454
+ return;
455
+ }
456
+ try {
457
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "authorize");
458
+ d("already authorized");
459
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, true);
460
+ return;
461
+ }
462
+ catch (err) {
463
+ if (!(err instanceof AuthKeyUnregistered)) {
464
+ throw err;
465
+ }
466
+ }
467
+ await this.authorize(params);
468
+ }
469
+ async invoke(function_, noWait) {
470
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
471
+ if (__classPrivateFieldGet(this, _Client_autoStart, "f") && !__classPrivateFieldGet(this, _Client_autoStarted, "f")) {
472
+ await this.start();
473
+ }
474
+ else {
475
+ throw new ConnectionError("Not connected");
530
476
  }
531
477
  }
532
- if (!(err instanceof SessionPasswordNeeded)) {
533
- throw err;
478
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
479
+ UNREACHABLE();
534
480
  }
481
+ let n = 1;
535
482
  while (true) {
536
- const ap = await this.invoke(new functions.AccountGetPassword());
537
- if (!(ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
538
- throw new Error(`Handling ${ap.currentAlgo?.constructor.name} not implemented`);
539
- }
540
483
  try {
541
- const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
542
- const input = await checkPassword(password, ap);
543
- const auth = await this.invoke(new functions.AuthCheckPassword({ password: input }));
544
- this.selfId = Number(auth[as](types.AuthAuthorization).user.id);
545
- await this.storage.setAccountType("user");
546
- dAuth("authorized as user");
547
- await this.propagateAuthorizationState(true);
548
- await this.fetchState("authorize");
549
- break;
484
+ let seqNo = __classPrivateFieldGet(this, _Client_state, "f").seqNo * 2;
485
+ if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
486
+ seqNo++;
487
+ __classPrivateFieldGet(this, _Client_state, "f").seqNo++;
488
+ }
489
+ const messageId = __classPrivateFieldSet(this, _Client_lastMsgId, getMessageId(__classPrivateFieldGet(this, _Client_lastMsgId, "f")), "f");
490
+ const message = new Message_(messageId, seqNo, function_);
491
+ await this.transport.transport.send(await encryptMessage(message, __classPrivateFieldGet(this, _Client_auth, "f").key, __classPrivateFieldGet(this, _Client_auth, "f").id, __classPrivateFieldGet(this, _Client_state, "f").salt, __classPrivateFieldGet(this, _Client_sessionId, "f")));
492
+ __classPrivateFieldSet(this, _Client_lastOutgoing, new Date(), "f");
493
+ d("invoked %s", function_.constructor.name);
494
+ if (noWait) {
495
+ return;
496
+ }
497
+ let result;
498
+ try {
499
+ result = await new Promise((resolve, reject) => {
500
+ __classPrivateFieldGet(this, _Client_promises, "f").set(message.id, { resolve, reject });
501
+ });
502
+ }
503
+ catch (err) {
504
+ if (err instanceof AuthKeyUnregistered) {
505
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, false);
506
+ }
507
+ throw err;
508
+ }
509
+ if (result instanceof types.BadServerSalt) {
510
+ return await this.invoke(function_);
511
+ }
512
+ else {
513
+ if (!__classPrivateFieldGet(this, _Client_pingLoopStarted, "f")) {
514
+ drop(__classPrivateFieldGet(this, _Client_instances, "m", _Client_pingLoop).call(this));
515
+ __classPrivateFieldSet(this, _Client_pingLoopStarted, true, "f");
516
+ }
517
+ return result;
518
+ }
550
519
  }
551
520
  catch (err) {
552
- if (err instanceof PasswordHashInvalid) {
521
+ if (__classPrivateFieldGet(this, _Client_errorHandler, "f") === undefined) {
522
+ throw err;
523
+ }
524
+ if (__classPrivateFieldGet(this, _Client_errorHandler, "f") !== undefined && await __classPrivateFieldGet(this, _Client_errorHandler, "f").call(this, err, function_, n++)) {
553
525
  continue;
554
526
  }
555
527
  else {
@@ -559,42 +531,85 @@ export class Client extends ClientAbstract {
559
531
  }
560
532
  }
561
533
  /**
562
- * Same as calling `.connect()` followed by `.authorize(params)`.
534
+ * Alias for `invoke` with its second parameter being `true`.
563
535
  */
564
- async start(params) {
565
- await this.connect();
566
- await this.initConnection();
567
- try {
568
- await this.fetchState("authorize");
569
- d("already authorized");
570
- await this.propagateAuthorizationState(true);
571
- return;
536
+ send(function_) {
537
+ return this.invoke(function_, true);
538
+ }
539
+ async checkGap(pts, ptsCount, assertNoGap) {
540
+ const localState = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
541
+ if (localState.pts + ptsCount < pts) {
542
+ if (assertNoGap) {
543
+ UNREACHABLE();
544
+ }
545
+ else {
546
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverUpdateGap).call(this, "processUpdates");
547
+ }
572
548
  }
573
- catch (err) {
574
- if (!(err instanceof AuthKeyUnregistered)) {
575
- throw err;
549
+ }
550
+ async getUserAccessHash(userId) {
551
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUser({ userId, accessHash: 0n })] }));
552
+ return users[0][as](types.User).accessHash ?? 0n;
553
+ }
554
+ async getInputPeer(id) {
555
+ const inputPeer = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getInputPeerInner).call(this, id);
556
+ if (inputPeer instanceof types.InputPeerUser || inputPeer instanceof types.InputPeerChannel && inputPeer.accessHash == 0n && await this.storage.getAccountType() == "bot") {
557
+ if ("channelId" in inputPeer) {
558
+ inputPeer.accessHash = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChannelAccessHash).call(this, inputPeer.channelId);
559
+ }
560
+ else {
561
+ inputPeer.accessHash = await this.getUserAccessHash(inputPeer.userId);
562
+ await this.storage.setUserAccessHash(inputPeer.userId, inputPeer.accessHash);
576
563
  }
577
564
  }
578
- await this.authorize(params);
565
+ return inputPeer;
579
566
  }
580
- async receiveLoop() {
581
- if (!this.auth || !this.transport) {
567
+ [(_Client_initConnection = async function _Client_initConnection() {
568
+ if (!__classPrivateFieldGet(this, _Client_connectionInited, "f")) {
569
+ await this.invoke(new functions.InitConnection({
570
+ apiId: this.apiId,
571
+ appVersion: this.appVersion,
572
+ deviceModel: this.deviceModel,
573
+ langCode: this.langCode,
574
+ langPack: this.langPack,
575
+ query: new functions.InvokeWithLayer({
576
+ layer: LAYER,
577
+ query: new functions.HelpGetConfig(),
578
+ }),
579
+ systemLangCode: this.systemLangCode,
580
+ systemVersion: this.systemVersion,
581
+ }));
582
+ __classPrivateFieldSet(this, _Client_connectionInited, true, "f");
583
+ d("connection inited");
584
+ }
585
+ }, _Client_propagateAuthorizationState = async function _Client_propagateAuthorizationState(authorized) {
586
+ if (__classPrivateFieldGet(this, _Client_lastPropagatedAuthorizationState, "f") != authorized) {
587
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { authorizationState: { authorized } }, resolve);
588
+ __classPrivateFieldSet(this, _Client_lastPropagatedAuthorizationState, authorized, "f");
589
+ }
590
+ }, _Client_getSelfId = async function _Client_getSelfId() {
591
+ if (__classPrivateFieldGet(this, _Client_selfId, "f") == null) {
592
+ __classPrivateFieldSet(this, _Client_selfId, await this.getMe().then((v) => v.id), "f");
593
+ }
594
+ return __classPrivateFieldGet(this, _Client_selfId, "f");
595
+ }, _Client_receiveLoop = async function _Client_receiveLoop() {
596
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
582
597
  throw new ConnectionError("Not connected");
583
598
  }
584
599
  while (this.connected) {
585
600
  try {
586
- if (this.toAcknowledge.size >= ACK_THRESHOLD) {
587
- await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
588
- this.toAcknowledge.clear();
601
+ if (__classPrivateFieldGet(this, _Client_toAcknowledge, "f").size >= ACK_THRESHOLD) {
602
+ await this.send(new types.MsgsAck({ msgIds: [...__classPrivateFieldGet(this, _Client_toAcknowledge, "f")] }));
603
+ __classPrivateFieldGet(this, _Client_toAcknowledge, "f").clear();
589
604
  }
590
605
  const buffer = await this.transport.transport.receive();
591
606
  let decrypted;
592
607
  try {
593
- decrypted = await (decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId));
608
+ decrypted = await (decryptMessage(buffer, __classPrivateFieldGet(this, _Client_auth, "f").key, __classPrivateFieldGet(this, _Client_auth, "f").id, __classPrivateFieldGet(this, _Client_sessionId, "f")));
594
609
  }
595
610
  catch (err) {
596
611
  dRecv("failed to decrypt message: %o", err);
597
- drop(this.recoverUpdateGap("decryption"));
612
+ drop(__classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverUpdateGap).call(this, "decryption"));
598
613
  continue;
599
614
  }
600
615
  const messages = decrypted instanceof MessageContainer ? decrypted.messages : [decrypted];
@@ -605,10 +620,11 @@ export class Client extends ClientAbstract {
605
620
  }
606
621
  dRecv("received %s", body.constructor.name);
607
622
  if (body instanceof types._TypeUpdates || body instanceof types._TypeUpdate) {
608
- this.processUpdatesQueue.add(() => this.processUpdates(body));
623
+ __classPrivateFieldGet(this, _Client_processUpdatesQueue, "f").add(() => __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, body));
609
624
  }
610
625
  else if (body instanceof types.NewSessionCreated) {
611
- this.state.salt = body.serverSalt;
626
+ __classPrivateFieldGet(this, _Client_state, "f").salt = body.serverSalt;
627
+ await this.storage.setServerSalt(__classPrivateFieldGet(this, _Client_state, "f").salt);
612
628
  }
613
629
  else if (message.body instanceof RPCResult) {
614
630
  let result = message.body.result;
@@ -623,7 +639,7 @@ export class Client extends ClientAbstract {
623
639
  }
624
640
  const messageId = message.body.messageId;
625
641
  const resolvePromise = () => {
626
- const promise = this.promises.get(messageId);
642
+ const promise = __classPrivateFieldGet(this, _Client_promises, "f").get(messageId);
627
643
  if (promise) {
628
644
  if (result instanceof types.RPCError) {
629
645
  promise.reject(upgradeInstance(result));
@@ -631,12 +647,12 @@ export class Client extends ClientAbstract {
631
647
  else {
632
648
  promise.resolve(result);
633
649
  }
634
- this.promises.delete(messageId);
650
+ __classPrivateFieldGet(this, _Client_promises, "f").delete(messageId);
635
651
  }
636
652
  };
637
653
  if (result instanceof types._TypeUpdates || result instanceof types._TypeUpdate) {
638
- this.processUpdatesQueue.add(async () => {
639
- await this.processUpdates(result);
654
+ __classPrivateFieldGet(this, _Client_processUpdatesQueue, "f").add(async () => {
655
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, result);
640
656
  resolvePromise();
641
657
  });
642
658
  }
@@ -646,22 +662,23 @@ export class Client extends ClientAbstract {
646
662
  }
647
663
  }
648
664
  else if (message.body instanceof types.Pong) {
649
- const promise = this.promises.get(message.body.msgId);
665
+ const promise = __classPrivateFieldGet(this, _Client_promises, "f").get(message.body.msgId);
650
666
  if (promise) {
651
667
  promise.resolve(message.body);
652
- this.promises.delete(message.body.msgId);
668
+ __classPrivateFieldGet(this, _Client_promises, "f").delete(message.body.msgId);
653
669
  }
654
670
  }
655
671
  else if (message.body instanceof types.BadServerSalt) {
656
672
  d("server salt reassigned");
657
- this.state.salt = message.body.newServerSalt;
658
- const promise = this.promises.get(message.body.badMsgId);
673
+ __classPrivateFieldGet(this, _Client_state, "f").salt = message.body.newServerSalt;
674
+ await this.storage.setServerSalt(__classPrivateFieldGet(this, _Client_state, "f").salt);
675
+ const promise = __classPrivateFieldGet(this, _Client_promises, "f").get(message.body.badMsgId);
659
676
  if (promise) {
660
677
  promise.resolve(message.body);
661
- this.promises.delete(message.body.badMsgId);
678
+ __classPrivateFieldGet(this, _Client_promises, "f").delete(message.body.badMsgId);
662
679
  }
663
680
  }
664
- this.toAcknowledge.add(message.id);
681
+ __classPrivateFieldGet(this, _Client_toAcknowledge, "f").add(message.id);
665
682
  }
666
683
  }
667
684
  catch (err) {
@@ -670,7 +687,7 @@ export class Client extends ClientAbstract {
670
687
  }
671
688
  else if (err instanceof TLError) {
672
689
  dRecv("failed to deserialize: %o", err);
673
- drop(this.recoverUpdateGap("deserialize"));
690
+ drop(__classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverUpdateGap).call(this, "deserialize"));
674
691
  }
675
692
  else {
676
693
  dRecv("uncaught error: %o", err);
@@ -678,95 +695,27 @@ export class Client extends ClientAbstract {
678
695
  }
679
696
  }
680
697
  if (!this.connected) {
681
- for (const { reject } of this.promises.values()) {
698
+ for (const { reject } of __classPrivateFieldGet(this, _Client_promises, "f").values()) {
682
699
  reject(new ConnectionError("Connection was closed"));
683
700
  }
684
701
  }
685
702
  else {
686
703
  UNREACHABLE();
687
704
  }
688
- }
689
- async pingLoop() {
705
+ }, _Client_pingLoop = async function _Client_pingLoop() {
690
706
  while (this.connected) {
691
- try {
692
- await this.invoke(new functions.Ping({ pingId: getRandomBigInt(8, true, false) }));
693
- }
694
- catch (err) {
695
- d("ping loop error: %o", err);
696
- }
697
707
  await new Promise((r) => setTimeout(r, 60 * 1000));
698
- }
699
- }
700
- async invoke(function_, noWait) {
701
- if (!this.auth || !this.transport) {
702
- if (this.autoStart && !this.autoStarted) {
703
- await this.start();
704
- }
705
- else {
706
- throw new ConnectionError("Not connected");
708
+ if (!__classPrivateFieldGet(this, _Client_lastOutgoing, "f") || (Date.now() - __classPrivateFieldGet(this, _Client_lastOutgoing, "f").getTime() < 60 * 1000)) {
709
+ continue;
707
710
  }
708
- }
709
- if (!this.auth || !this.transport) {
710
- UNREACHABLE();
711
- }
712
- let n = 1;
713
- while (true) {
714
711
  try {
715
- let seqNo = this.state.seqNo * 2;
716
- if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
717
- seqNo++;
718
- this.state.seqNo++;
719
- }
720
- const messageId = this.lastMsgId = getMessageId(this.lastMsgId);
721
- const message = new Message_(messageId, seqNo, function_);
722
- await this.transport.transport.send(await encryptMessage(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
723
- d("invoked %s", function_.constructor.name);
724
- if (noWait) {
725
- return;
726
- }
727
- let result;
728
- try {
729
- result = await new Promise((resolve, reject) => {
730
- this.promises.set(message.id, { resolve, reject });
731
- });
732
- }
733
- catch (err) {
734
- if (err instanceof AuthKeyUnregistered) {
735
- await this.propagateAuthorizationState(false);
736
- }
737
- throw err;
738
- }
739
- if (result instanceof types.BadServerSalt) {
740
- return await this.invoke(function_);
741
- }
742
- else {
743
- if (!this.pingLoopStarted) {
744
- drop(this.pingLoop());
745
- this.pingLoopStarted = true;
746
- }
747
- return result;
748
- }
712
+ await this.invoke(new functions.Ping({ pingId: getRandomBigInt(8, true, false) }));
749
713
  }
750
714
  catch (err) {
751
- if (this.errorHandler === undefined) {
752
- throw err;
753
- }
754
- if (this.errorHandler !== undefined && await this.errorHandler(err, function_, n++)) {
755
- continue;
756
- }
757
- else {
758
- throw err;
759
- }
715
+ d("ping loop error: %o", err);
760
716
  }
761
717
  }
762
- }
763
- /**
764
- * Alias for `invoke` with its second parameter being `true`.
765
- */
766
- send(function_) {
767
- return this.invoke(function_, true);
768
- }
769
- async processChats(chats) {
718
+ }, _Client_processChats = async function _Client_processChats(chats) {
770
719
  for (const chat of chats) {
771
720
  if (chat instanceof types.Channel && chat.accessHash) {
772
721
  await this.storage.setEntity(chat);
@@ -782,8 +731,7 @@ export class Client extends ClientAbstract {
782
731
  await this.storage.setEntity(chat);
783
732
  }
784
733
  }
785
- }
786
- async processUsers(users) {
734
+ }, _Client_processUsers = async function _Client_processUsers(users) {
787
735
  for (const user of users) {
788
736
  if (user instanceof types.User && user.accessHash) {
789
737
  await this.storage.setEntity(user);
@@ -796,19 +744,7 @@ export class Client extends ClientAbstract {
796
744
  }
797
745
  }
798
746
  }
799
- }
800
- async checkGap(pts, ptsCount, assertNoGap) {
801
- const localState = await this.getLocalState();
802
- if (localState.pts + ptsCount < pts) {
803
- if (assertNoGap) {
804
- UNREACHABLE();
805
- }
806
- else {
807
- await this.recoverUpdateGap("processUpdates");
808
- }
809
- }
810
- }
811
- async checkChannelGap(channelId, pts, ptsCount, assertNoGap) {
747
+ }, _Client_checkChannelGap = async function _Client_checkChannelGap(channelId, pts, ptsCount, assertNoGap) {
812
748
  let localPts = await this.storage.getChannelPts(channelId);
813
749
  if (!localPts) {
814
750
  localPts = pts - ptsCount;
@@ -818,11 +754,10 @@ export class Client extends ClientAbstract {
818
754
  UNREACHABLE();
819
755
  }
820
756
  else {
821
- await this.recoverChannelUpdateGap(channelId, "processUpdates");
757
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverChannelUpdateGap).call(this, channelId, "processUpdates");
822
758
  }
823
759
  }
824
- }
825
- async processUpdates(updates_, assertNoGap = false) {
760
+ }, _Client_processUpdates = async function _Client_processUpdates(updates_, assertNoGap = false) {
826
761
  /// First, individual updates (Update[1]) and updateShort* are extracted from Updates.[2]
827
762
  ///
828
763
  /// If an updatesTooLong[3] was received, an update gap recovery is initiated and no further action will be taken.
@@ -843,7 +778,7 @@ export class Client extends ClientAbstract {
843
778
  updates = [updates_];
844
779
  }
845
780
  else if (updates_ instanceof types.UpdatesTooLong) {
846
- await this.recoverUpdateGap("updatesTooLong");
781
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverUpdateGap).call(this, "updatesTooLong");
847
782
  return;
848
783
  }
849
784
  else if (updates_ instanceof types._TypeUpdate) {
@@ -865,7 +800,7 @@ export class Client extends ClientAbstract {
865
800
  continue;
866
801
  }
867
802
  await this.checkGap(update.pts, update.ptsCount, assertNoGap);
868
- localState ??= await this.getLocalState();
803
+ localState ??= await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
869
804
  originalPts ??= localState.pts;
870
805
  if (localState.pts + update.ptsCount > update.pts) {
871
806
  updates = updates.filter((v) => v != update);
@@ -878,9 +813,9 @@ export class Client extends ClientAbstract {
878
813
  if (update.pts == 0) {
879
814
  continue;
880
815
  }
881
- const ptsCount = "ptsCount" in update ? update.ptsCount : 1;
816
+ const ptsCount = update.ptsCount;
882
817
  const channelId = update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateEditChannelMessage ? update.message.peerId[as](types.PeerChannel).channelId : update.channelId;
883
- await this.checkChannelGap(channelId, update.pts, ptsCount, assertNoGap);
818
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_checkChannelGap).call(this, channelId, update.pts, ptsCount, assertNoGap);
884
819
  let currentPts = channelPtsMap.get(channelId);
885
820
  if (currentPts === undefined) {
886
821
  currentPts = await this.storage.getChannelPts(channelId);
@@ -904,33 +839,33 @@ export class Client extends ClientAbstract {
904
839
  }
905
840
  /// We process the updates when we are sure there is no gap.
906
841
  if (updates_ instanceof types.Updates || updates_ instanceof types.UpdatesCombined) {
907
- await this.processChats(updates_.chats);
908
- await this.processUsers(updates_.users);
909
- await this.setUpdateStateDate(updates_.date);
842
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, updates_.chats);
843
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, updates_.users);
844
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setUpdateStateDate).call(this, updates_.date);
910
845
  }
911
846
  else if (updates_ instanceof types.UpdateShort) {
912
- await this.setUpdateStateDate(updates_.date);
847
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setUpdateStateDate).call(this, updates_.date);
913
848
  }
914
849
  const updatesToHandle = new Array();
915
850
  for (const update of updates) {
916
851
  if (update instanceof types.UpdateShortMessage ||
917
852
  update instanceof types.UpdateShortChatMessage ||
918
853
  update instanceof types.UpdateShortSentMessage) {
919
- await this.setUpdateStateDate(update.date);
854
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setUpdateStateDate).call(this, update.date);
920
855
  }
921
856
  else if (update instanceof types.UpdateChannelTooLong) {
922
857
  if (update.pts != undefined) {
923
858
  await this.storage.setChannelPts(update.channelId, update.pts);
924
859
  }
925
- await this.recoverChannelUpdateGap(update.channelId, "updateChannelTooLong");
860
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverChannelUpdateGap).call(this, update.channelId, "updateChannelTooLong");
926
861
  }
927
862
  else if (update instanceof types.UpdateUserName) {
928
863
  await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v.username));
929
864
  }
930
865
  else if (update instanceof types.UpdatePtsChanged) {
931
- await this.fetchState("updatePtsChanged");
932
- if (this.updateState) {
933
- await this.storage.setState(this.updateState);
866
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "updatePtsChanged");
867
+ if (__classPrivateFieldGet(this, _Client_updateState, "f")) {
868
+ await this.storage.setState(__classPrivateFieldGet(this, _Client_updateState, "f"));
934
869
  }
935
870
  else {
936
871
  UNREACHABLE();
@@ -941,28 +876,26 @@ export class Client extends ClientAbstract {
941
876
  updatesToHandle.push(update);
942
877
  }
943
878
  }
944
- this.handleUpdateQueue.add(async () => {
879
+ __classPrivateFieldGet(this, _Client_handleUpdateQueue, "f").add(async () => {
945
880
  for (const update of updatesToHandle) {
946
- await this.handleUpdate(update);
881
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_handleUpdate).call(this, update);
947
882
  }
948
883
  });
949
- }
950
- async setUpdateStateDate(date) {
951
- const localState = await this.getLocalState();
884
+ }, _Client_setUpdateStateDate = async function _Client_setUpdateStateDate(date) {
885
+ const localState = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
952
886
  localState.date = date;
953
887
  await this.storage.setState(localState);
954
- }
955
- async getLocalState() {
888
+ }, _Client_getLocalState = async function _Client_getLocalState() {
956
889
  let localState = await this.storage.getState();
957
890
  if (!localState) {
958
- if (this.updateState) {
959
- localState = this.updateState;
891
+ if (__classPrivateFieldGet(this, _Client_updateState, "f")) {
892
+ localState = __classPrivateFieldGet(this, _Client_updateState, "f");
960
893
  await this.storage.setState(localState);
961
894
  }
962
895
  else {
963
- await this.fetchState("getLocalState");
964
- if (this.updateState) {
965
- localState = this.updateState;
896
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "getLocalState");
897
+ if (__classPrivateFieldGet(this, _Client_updateState, "f")) {
898
+ localState = __classPrivateFieldGet(this, _Client_updateState, "f");
966
899
  await this.storage.setState(localState);
967
900
  }
968
901
  else {
@@ -971,22 +904,21 @@ export class Client extends ClientAbstract {
971
904
  }
972
905
  }
973
906
  return localState;
974
- }
975
- async recoverUpdateGap(source) {
907
+ }, _Client_recoverUpdateGap = async function _Client_recoverUpdateGap(source) {
976
908
  dGap("recovering from update gap [%s]", source);
977
- await this.propagateConnectionState("updating");
909
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, "updating");
978
910
  try {
979
- let state = await this.getLocalState();
911
+ let state = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
980
912
  while (true) {
981
913
  const difference = await this.invoke(new functions.UpdatesGetDifference({ pts: state.pts, date: state.date, qts: state.qts ?? 0 }));
982
914
  if (difference instanceof types.UpdatesDifference || difference instanceof types.UpdatesDifferenceSlice) {
983
- await this.processChats(difference.chats);
984
- await this.processUsers(difference.users);
915
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, difference.chats);
916
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, difference.users);
985
917
  for (const message of difference.newMessages) {
986
- await this.processUpdates(new types.UpdateNewMessage({ message, pts: 0, ptsCount: 0 }), true);
918
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, new types.UpdateNewMessage({ message, pts: 0, ptsCount: 0 }), true);
987
919
  }
988
920
  for (const update of difference.otherUpdates) {
989
- await this.processUpdates(update, true);
921
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, update, true);
990
922
  }
991
923
  if (difference instanceof types.UpdatesDifference) {
992
924
  await this.storage.setState(difference.state);
@@ -1006,7 +938,7 @@ export class Client extends ClientAbstract {
1006
938
  dGap("received differenceTooLong");
1007
939
  }
1008
940
  else if (difference instanceof types.UpdatesDifferenceEmpty) {
1009
- await this.setUpdateStateDate(difference.date);
941
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setUpdateStateDate).call(this, difference.date);
1010
942
  dGap("there was no update gap");
1011
943
  break;
1012
944
  }
@@ -1018,8 +950,7 @@ export class Client extends ClientAbstract {
1018
950
  finally {
1019
951
  this.stateChangeHandler(this.connected);
1020
952
  }
1021
- }
1022
- async recoverChannelUpdateGap(channelId, source) {
953
+ }, _Client_recoverChannelUpdateGap = async function _Client_recoverChannelUpdateGap(channelId, source) {
1023
954
  dGapC("recovering channel update gap [%o, %s]", channelId, source);
1024
955
  const pts_ = await this.storage.getChannelPts(channelId);
1025
956
  let pts = pts_ == null ? 1 : pts_;
@@ -1032,13 +963,13 @@ export class Client extends ClientAbstract {
1032
963
  limit: await this.storage.getAccountType() == "user" ? CHANNEL_DIFFERENCE_LIMIT_USER : CHANNEL_DIFFERENCE_LIMIT_BOT,
1033
964
  }));
1034
965
  if (difference instanceof types.UpdatesChannelDifference) {
1035
- await this.processChats(difference.chats);
1036
- await this.processUsers(difference.users);
966
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, difference.chats);
967
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, difference.users);
1037
968
  for (const message of difference.newMessages) {
1038
- await this.processUpdates(new types.UpdateNewChannelMessage({ message, pts: 0, ptsCount: 0 }), true);
969
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, new types.UpdateNewChannelMessage({ message, pts: 0, ptsCount: 0 }), true);
1039
970
  }
1040
971
  for (const update of difference.otherUpdates) {
1041
- await this.processUpdates(update, true);
972
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, update, true);
1042
973
  }
1043
974
  await this.storage.setChannelPts(channelId, difference.pts);
1044
975
  dGapC("recovered from update gap [%o, %s]", channelId, source);
@@ -1047,10 +978,10 @@ export class Client extends ClientAbstract {
1047
978
  else if (difference instanceof types.UpdatesChannelDifferenceTooLong) {
1048
979
  // invalidate messages
1049
980
  dGapC("received channelDifferenceTooLong");
1050
- await this.processChats(difference.chats);
1051
- await this.processUsers(difference.users);
981
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, difference.chats);
982
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, difference.users);
1052
983
  for (const message of difference.messages) {
1053
- await this.processUpdates(new types.UpdateNewChannelMessage({ message, pts: 0, ptsCount: 0 }), true);
984
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUpdates).call(this, new types.UpdateNewChannelMessage({ message, pts: 0, ptsCount: 0 }), true);
1054
985
  }
1055
986
  const pts_ = difference.dialog[as](types.Dialog).pts;
1056
987
  if (pts_ != undefined) {
@@ -1066,29 +997,10 @@ export class Client extends ClientAbstract {
1066
997
  break;
1067
998
  }
1068
999
  }
1069
- }
1070
- async getUserAccessHash(userId) {
1071
- const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUser({ userId, accessHash: 0n })] }));
1072
- return users[0][as](types.User).accessHash ?? 0n;
1073
- }
1074
- async getChannelAccessHash(channelId) {
1000
+ }, _Client_getChannelAccessHash = async function _Client_getChannelAccessHash(channelId) {
1075
1001
  const channels = await this.invoke(new functions.ChannelsGetChannels({ id: [new types.InputChannel({ channelId, accessHash: 0n })] }));
1076
1002
  return channels.chats[0][as](types.Channel).accessHash ?? 0n;
1077
- }
1078
- async getInputPeer(id) {
1079
- const inputPeer = await this.getInputPeerInner(id);
1080
- if (inputPeer instanceof types.InputPeerUser || inputPeer instanceof types.InputPeerChannel && inputPeer.accessHash == 0n && await this.storage.getAccountType() == "bot") {
1081
- if ("channelId" in inputPeer) {
1082
- inputPeer.accessHash = await this.getChannelAccessHash(inputPeer.channelId);
1083
- }
1084
- else {
1085
- inputPeer.accessHash = await this.getUserAccessHash(inputPeer.userId);
1086
- await this.storage.setUserAccessHash(inputPeer.userId, inputPeer.accessHash);
1087
- }
1088
- }
1089
- return inputPeer;
1090
- }
1091
- async getInputPeerInner(id) {
1003
+ }, _Client_getInputPeerInner = async function _Client_getInputPeerInner(id) {
1092
1004
  if (typeof id === "string") {
1093
1005
  if (!id.startsWith("@")) {
1094
1006
  throw new Error("Expected username to start with @");
@@ -1112,8 +1024,8 @@ export class Client extends ClientAbstract {
1112
1024
  }
1113
1025
  else {
1114
1026
  const resolved = await this.invoke(new functions.ContactsResolveUsername({ username: id }));
1115
- await this.processChats(resolved.chats);
1116
- await this.processUsers(resolved.users);
1027
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, resolved.chats);
1028
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, resolved.users);
1117
1029
  if (resolved.peer instanceof types.PeerUser) {
1118
1030
  userId = resolved.peer.userId;
1119
1031
  }
@@ -1153,8 +1065,7 @@ export class Client extends ClientAbstract {
1153
1065
  else {
1154
1066
  throw new Error("ID format unknown or not implemented");
1155
1067
  }
1156
- }
1157
- [getEntity](peer) {
1068
+ }, getEntity)](peer) {
1158
1069
  const type = peer instanceof types.PeerUser ? "user" : peer instanceof types.PeerChat ? "chat" : peer instanceof types.PeerChannel ? "channel" : UNREACHABLE();
1159
1070
  const id = peer instanceof types.PeerUser ? peer.userId : peer instanceof types.PeerChat ? peer.chatId : peer instanceof types.PeerChannel ? peer.channelId : UNREACHABLE();
1160
1071
  return this.storage.getEntity(type, id);
@@ -1196,9 +1107,9 @@ export class Client extends ClientAbstract {
1196
1107
  result instanceof types.ChatlistsChatlistUpdates ||
1197
1108
  result instanceof types.MessagesChats ||
1198
1109
  result instanceof types.MessagesChatsSlice) {
1199
- await this.processChats(result.chats);
1110
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, result.chats);
1200
1111
  if ("users" in result) {
1201
- await this.processUsers(result.users);
1112
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, result.users);
1202
1113
  }
1203
1114
  }
1204
1115
  if (result instanceof types.MessagesMessages) {
@@ -1209,33 +1120,6 @@ export class Client extends ClientAbstract {
1209
1120
  }
1210
1121
  }
1211
1122
  }
1212
- async updatesToMessages(chatId, updates) {
1213
- const messages = new Array();
1214
- if (updates instanceof types.Updates) {
1215
- for (const update of updates.updates) {
1216
- if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateEditMessage) {
1217
- messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1218
- }
1219
- else if (update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateEditChannelMessage) {
1220
- messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1221
- }
1222
- }
1223
- }
1224
- else if (updates instanceof types.UpdateShortSentMessage || updates instanceof types.UpdateShortSentMessage) {
1225
- const message = await this.getMessage(chatId, updates.id);
1226
- if (message != null) {
1227
- messages.push(message);
1228
- }
1229
- }
1230
- return messages;
1231
- }
1232
- async resolveSendAs(params) {
1233
- const sendAs = params?.sendAs;
1234
- if (sendAs !== undefined) {
1235
- await this.assertUser("sendAs");
1236
- return sendAs ? await this.getInputPeer(sendAs) : undefined;
1237
- }
1238
- }
1239
1123
  /**
1240
1124
  * Send a text message.
1241
1125
  *
@@ -1245,8 +1129,8 @@ export class Client extends ClientAbstract {
1245
1129
  * @returns The sent text message.
1246
1130
  */
1247
1131
  async sendMessage(chatId, text, params) {
1248
- const [message, entities] = this.parseText(text, params);
1249
- const replyMarkup = await this.constructReplyMarkup(params);
1132
+ const [message, entities] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).call(this, text, params);
1133
+ const replyMarkup = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_constructReplyMarkup).call(this, params);
1250
1134
  const peer = await this.getInputPeer(chatId);
1251
1135
  const randomId = getRandomId();
1252
1136
  const noWebpage = params?.disableWebPagePreview ? true : undefined;
@@ -1254,7 +1138,7 @@ export class Client extends ClientAbstract {
1254
1138
  const noforwards = params?.protectContent ? true : undefined;
1255
1139
  const replyToMsgId = params?.replyToMessageId;
1256
1140
  const topMsgId = params?.messageThreadId;
1257
- const sendAs = await this.resolveSendAs(params);
1141
+ const sendAs = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_resolveSendAs).call(this, params);
1258
1142
  const result = await this.invoke(new functions.MessagesSendMessage({
1259
1143
  peer,
1260
1144
  randomId,
@@ -1267,28 +1151,8 @@ export class Client extends ClientAbstract {
1267
1151
  entities,
1268
1152
  replyMarkup,
1269
1153
  }));
1270
- const message_ = await this.updatesToMessages(chatId, result).then((v) => v[0]);
1271
- return Client.assertMsgHas(message_, "text");
1272
- }
1273
- parseText(text, params) {
1274
- const entities_ = params?.entities ?? [];
1275
- const parseMode = params?.parseMode ?? this.parseMode;
1276
- switch (parseMode) {
1277
- case null:
1278
- break;
1279
- case "HTML": {
1280
- const [newText, entitiesToPush] = parseHtml(text);
1281
- text = newText;
1282
- for (const entity of entitiesToPush) {
1283
- entities_.push(entity);
1284
- }
1285
- break;
1286
- }
1287
- default:
1288
- UNREACHABLE();
1289
- }
1290
- const entities = entities_?.length > 0 ? entities_.map((v) => messageEntityToTlObject(v)) : undefined;
1291
- return [text, entities];
1154
+ const message_ = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1155
+ return __classPrivateFieldGet(Client, _a, "m", _Client_assertMsgHas).call(Client, message_, "text");
1292
1156
  }
1293
1157
  /**
1294
1158
  * Edit a message's text.
@@ -1300,19 +1164,77 @@ export class Client extends ClientAbstract {
1300
1164
  * @returns The edited text message.
1301
1165
  */
1302
1166
  async editMessageText(chatId, messageId, text, params) {
1303
- const [message, entities] = this.parseText(text, params);
1167
+ const [message, entities] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).call(this, text, params);
1304
1168
  const result = await this.invoke(new functions.MessagesEditMessage({
1305
1169
  id: messageId,
1306
1170
  peer: await this.getInputPeer(chatId),
1307
1171
  entities,
1308
1172
  message,
1309
1173
  noWebpage: params?.disableWebPagePreview ? true : undefined,
1310
- replyMarkup: await this.constructReplyMarkup(params),
1174
+ replyMarkup: await __classPrivateFieldGet(this, _Client_instances, "m", _Client_constructReplyMarkup).call(this, params),
1311
1175
  }));
1312
- const message_ = await this.updatesToMessages(chatId, result).then((v) => v[0]);
1313
- return Client.assertMsgHas(message_, "text");
1176
+ const message_ = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1177
+ return __classPrivateFieldGet(Client, _a, "m", _Client_assertMsgHas).call(Client, message_, "text");
1178
+ }
1179
+ /**
1180
+ * Retrieve multiple messages.
1181
+ *
1182
+ * @method
1183
+ * @param chatId The identifier of the chat to retrieve the messages from.
1184
+ * @param messageIds The identifiers of the messages to retrieve.
1185
+ * @example ```ts
1186
+ * const message = await client.getMessages("@MTKruto", [210, 212]);
1187
+ * ```
1188
+ * @returns The retrieved messages.
1189
+ */
1190
+ async getMessages(chatId, messageIds) {
1191
+ return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getMessagesInner).call(this, chatId, messageIds).then((v) => v.map((v) => v.message));
1314
1192
  }
1315
- async getMessagesInner(chatId_, messageIds) {
1193
+ async [(_Client_updatesToMessages = async function _Client_updatesToMessages(chatId, updates) {
1194
+ const messages = new Array();
1195
+ if (updates instanceof types.Updates) {
1196
+ for (const update of updates.updates) {
1197
+ if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateEditMessage) {
1198
+ messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1199
+ }
1200
+ else if (update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateEditChannelMessage) {
1201
+ messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1202
+ }
1203
+ }
1204
+ }
1205
+ else if (updates instanceof types.UpdateShortSentMessage || updates instanceof types.UpdateShortSentMessage) {
1206
+ const message = await this.getMessage(chatId, updates.id);
1207
+ if (message != null) {
1208
+ messages.push(message);
1209
+ }
1210
+ }
1211
+ return messages;
1212
+ }, _Client_resolveSendAs = async function _Client_resolveSendAs(params) {
1213
+ const sendAs = params?.sendAs;
1214
+ if (sendAs !== undefined) {
1215
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertUser).call(this, "sendAs");
1216
+ return sendAs ? await this.getInputPeer(sendAs) : undefined;
1217
+ }
1218
+ }, _Client_parseText = function _Client_parseText(text, params) {
1219
+ const entities_ = params?.entities ?? [];
1220
+ const parseMode = params?.parseMode ?? this.parseMode;
1221
+ switch (parseMode) {
1222
+ case null:
1223
+ break;
1224
+ case "HTML": {
1225
+ const [newText, entitiesToPush] = parseHtml(text);
1226
+ text = newText;
1227
+ for (const entity of entitiesToPush) {
1228
+ entities_.push(entity);
1229
+ }
1230
+ break;
1231
+ }
1232
+ default:
1233
+ UNREACHABLE();
1234
+ }
1235
+ const entities = entities_?.length > 0 ? entities_.map((v) => messageEntityToTlObject(v)) : undefined;
1236
+ return [text, entities];
1237
+ }, _Client_getMessagesInner = async function _Client_getMessagesInner(chatId_, messageIds) {
1316
1238
  const peer = await this.getInputPeer(chatId_);
1317
1239
  let messages_ = new Array();
1318
1240
  const chatId = peerToChatId(peer);
@@ -1348,23 +1270,8 @@ export class Client extends ClientAbstract {
1348
1270
  messages.push({ message, isReplyToMessage });
1349
1271
  }
1350
1272
  return messages;
1351
- }
1352
- /**
1353
- * Retrieve multiple messages.
1354
- *
1355
- * @method
1356
- * @param chatId The identifier of the chat to retrieve the messages from.
1357
- * @param messageIds The identifiers of the messages to retrieve.
1358
- * @example ```ts
1359
- * const message = await client.getMessages("@MTKruto", [210, 212]);
1360
- * ```
1361
- * @returns The retrieved messages.
1362
- */
1363
- async getMessages(chatId, messageIds) {
1364
- return await this.getMessagesInner(chatId, messageIds).then((v) => v.map((v) => v.message));
1365
- }
1366
- async [getMessageWithReply](chatId, messageId) {
1367
- const messages = await this.getMessagesInner(chatId, [messageId]);
1273
+ }, getMessageWithReply)](chatId, messageId) {
1274
+ const messages = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getMessagesInner).call(this, chatId, [messageId]);
1368
1275
  return messages[0]?.message ?? null;
1369
1276
  }
1370
1277
  /**
@@ -1382,7 +1289,44 @@ export class Client extends ClientAbstract {
1382
1289
  const messages = await this.getMessages(chatId, [messageId]);
1383
1290
  return messages[0] ?? null;
1384
1291
  }
1385
- async *downloadInner(location, dcId, params) {
1292
+ /**
1293
+ * Download a file.
1294
+ *
1295
+ * @method
1296
+ * @param fileId The identifier of the file to download.
1297
+ * @example ```ts
1298
+ * for await (const chunk of client.download(fileId, { chunkSize: 256 * 1024 })) {
1299
+ * await outFile.write(chunk);
1300
+ * }
1301
+ * ```
1302
+ * @returns A generator yielding the contents of the file.
1303
+ */
1304
+ async download(fileId, params) {
1305
+ const fileId_ = FileID.decode(fileId);
1306
+ switch (fileId_.fileType) {
1307
+ case FileType.ChatPhoto: {
1308
+ const big = fileId_.params.thumbnailSource == ThumbnailSource.ChatPhotoBig;
1309
+ const peer = await this.getInputPeer(fileId_.params.chatId);
1310
+ const location = new types.InputPeerPhotoFileLocation({ big: big ? true : undefined, peer, photoId: fileId_.params.mediaId });
1311
+ return __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params);
1312
+ }
1313
+ case FileType.Photo: {
1314
+ if (fileId_.params.mediaId == undefined || fileId_.params.accessHash == undefined || fileId_.params.fileReference == undefined || fileId_.params.thumbnailSize == undefined) {
1315
+ UNREACHABLE();
1316
+ }
1317
+ const location = new types.InputPhotoFileLocation({
1318
+ id: fileId_.params.mediaId,
1319
+ accessHash: fileId_.params.accessHash,
1320
+ fileReference: fileId_.params.fileReference,
1321
+ thumbSize: fileId_.params.thumbnailSize,
1322
+ });
1323
+ return __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params);
1324
+ }
1325
+ default:
1326
+ UNREACHABLE();
1327
+ }
1328
+ }
1329
+ async [(_Client_downloadInner = async function* _Client_downloadInner(location, dcId, params) {
1386
1330
  const chunkSize = params?.chunkSize ?? 1024 * 1024;
1387
1331
  if (mod(chunkSize, 1024) != 0) {
1388
1332
  throw new Error("chunkSize must be divisible by 1024");
@@ -1422,45 +1366,7 @@ export class Client extends ClientAbstract {
1422
1366
  UNREACHABLE();
1423
1367
  }
1424
1368
  }
1425
- }
1426
- /**
1427
- * Download a file.
1428
- *
1429
- * @method
1430
- * @param fileId The identifier of the file to download.
1431
- * @example ```ts
1432
- * for await (const chunk of client.download(fileId, { chunkSize: 256 * 1024 })) {
1433
- * await outFile.write(chunk);
1434
- * }
1435
- * ```
1436
- * @returns A generator yielding the contents of the file.
1437
- */
1438
- async download(fileId, params) {
1439
- const fileId_ = FileID.decode(fileId);
1440
- switch (fileId_.fileType) {
1441
- case FileType.ChatPhoto: {
1442
- const big = fileId_.params.thumbnailSource == ThumbnailSource.ChatPhotoBig;
1443
- const peer = await this.getInputPeer(fileId_.params.chatId);
1444
- const location = new types.InputPeerPhotoFileLocation({ big: big ? true : undefined, peer, photoId: fileId_.params.mediaId });
1445
- return this.downloadInner(location, fileId_.dcId, params);
1446
- }
1447
- case FileType.Photo: {
1448
- if (fileId_.params.mediaId == undefined || fileId_.params.accessHash == undefined || fileId_.params.fileReference == undefined || fileId_.params.thumbnailSize == undefined) {
1449
- UNREACHABLE();
1450
- }
1451
- const location = new types.InputPhotoFileLocation({
1452
- id: fileId_.params.mediaId,
1453
- accessHash: fileId_.params.accessHash,
1454
- fileReference: fileId_.params.fileReference,
1455
- thumbSize: fileId_.params.thumbnailSize,
1456
- });
1457
- return this.downloadInner(location, fileId_.dcId, params);
1458
- }
1459
- default:
1460
- UNREACHABLE();
1461
- }
1462
- }
1463
- async [getStickerSetName](inputStickerSet, hash = 0) {
1369
+ }, getStickerSetName)](inputStickerSet, hash = 0) {
1464
1370
  const maybeStickerSetName = await this.storage.getStickerSetName(inputStickerSet.id, inputStickerSet.accessHash);
1465
1371
  if (maybeStickerSetName != null && Date.now() - maybeStickerSetName[1].getTime() < STICKER_SET_NAME_TTL) {
1466
1372
  return maybeStickerSetName[0];
@@ -1494,7 +1400,7 @@ export class Client extends ClientAbstract {
1494
1400
  dropAuthor: params?.dropSenderName || undefined,
1495
1401
  dropMediaCaptions: params?.dropCaption || undefined,
1496
1402
  }));
1497
- return await this.updatesToMessages(to, result);
1403
+ return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, to, result);
1498
1404
  }
1499
1405
  /**
1500
1406
  * Forward a single message.
@@ -1520,102 +1426,6 @@ export class Client extends ClientAbstract {
1520
1426
  }
1521
1427
  return constructUser(users[0][as](types.User));
1522
1428
  }
1523
- // TODO: log errors
1524
- async handleUpdate(update) {
1525
- if (update instanceof types.UpdateShortMessage) {
1526
- update = new types.UpdateNewMessage({
1527
- message: new types.Message({
1528
- out: update.out,
1529
- mentioned: update.mentioned,
1530
- mediaUnread: update.mediaUnread,
1531
- silent: update.silent,
1532
- id: update.id,
1533
- fromId: update.out ? new types.PeerUser({ userId: await this.getSelfId().then(BigInt) }) : new types.PeerUser({ userId: update.userId }),
1534
- peerId: new types.PeerChat({ chatId: update.userId }),
1535
- message: update.message,
1536
- date: update.date,
1537
- fwdFrom: update.fwdFrom,
1538
- viaBotId: update.viaBotId,
1539
- replyTo: update.replyTo,
1540
- entities: update.entities,
1541
- ttlPeriod: update.ttlPeriod,
1542
- }),
1543
- pts: update.pts,
1544
- ptsCount: update.ptsCount,
1545
- });
1546
- }
1547
- else if (update instanceof types.UpdateShortChatMessage) {
1548
- update = new types.UpdateNewMessage({
1549
- message: new types.Message({
1550
- out: update.out,
1551
- mentioned: update.mentioned,
1552
- mediaUnread: update.mediaUnread,
1553
- silent: update.silent,
1554
- id: update.id,
1555
- fromId: new types.PeerUser({ userId: update.fromId }),
1556
- peerId: new types.PeerChat({ chatId: update.chatId }),
1557
- fwdFrom: update.fwdFrom,
1558
- viaBotId: update.viaBotId,
1559
- replyTo: update.replyTo,
1560
- date: update.date,
1561
- message: update.message,
1562
- entities: update.entities,
1563
- ttlPeriod: update.ttlPeriod,
1564
- }),
1565
- pts: update.pts,
1566
- ptsCount: update.ptsCount,
1567
- });
1568
- }
1569
- if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateNewChannelMessage) {
1570
- if (update.message instanceof types.Message || update.message instanceof types.MessageService) {
1571
- await this.storage.setMessage(peerToChatId(update.message.peerId), update.message.id, update.message);
1572
- }
1573
- }
1574
- if (update instanceof types.UpdateNewMessage ||
1575
- update instanceof types.UpdateNewChannelMessage ||
1576
- update instanceof types.UpdateEditMessage ||
1577
- update instanceof types.UpdateEditChannelMessage) {
1578
- const key = update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage ? "message" : "editedMessage";
1579
- const message = await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1580
- await this.handle({ [key]: message }, resolve);
1581
- }
1582
- if (update instanceof types.UpdateDeleteMessages) {
1583
- const deletedMessages = new Array();
1584
- for (const messageId of update.messages) {
1585
- const chatId = await this.storage.getMessageChat(messageId);
1586
- if (chatId) {
1587
- const message = await this.storage.getMessage(chatId, messageId);
1588
- if (message != null) {
1589
- deletedMessages.push(await constructMessage(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1590
- }
1591
- await this.storage.setMessage(chatId, messageId, null);
1592
- }
1593
- }
1594
- if (deletedMessages.length > 0) {
1595
- await this.handle({ deletedMessages: deletedMessages }, resolve);
1596
- }
1597
- }
1598
- else if (update instanceof types.UpdateDeleteChannelMessages) {
1599
- const chatId = getChannelChatId(update.channelId);
1600
- const deletedMessages = new Array();
1601
- for (const messageId of update.messages) {
1602
- const message = await this.storage.getMessage(chatId, messageId);
1603
- if (message) {
1604
- deletedMessages.push(await constructMessage(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1605
- }
1606
- await this.storage.setMessage(chatId, messageId, null);
1607
- }
1608
- if (deletedMessages.length > 0) {
1609
- await this.handle({ deletedMessages: deletedMessages }, resolve);
1610
- }
1611
- }
1612
- if (update instanceof types.UpdateBotCallbackQuery || update instanceof types.UpdateInlineBotCallbackQuery) {
1613
- await this.handle({ callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, resolve);
1614
- }
1615
- else if (update instanceof types.UpdateBotInlineQuery) {
1616
- await this.handle({ inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }, resolve);
1617
- }
1618
- }
1619
1429
  /**
1620
1430
  * Answer a callback query. Bot-only.
1621
1431
  *
@@ -1623,7 +1433,7 @@ export class Client extends ClientAbstract {
1623
1433
  * @param id ID of the callback query to answer.
1624
1434
  */
1625
1435
  async answerCallbackQuery(id, params) {
1626
- await this.assertBot("answerCallbackQuery");
1436
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "answerCallbackQuery");
1627
1437
  await this.invoke(new functions.MessagesSetBotCallbackAnswer({
1628
1438
  queryId: BigInt(id),
1629
1439
  cacheTime: params?.cacheTime ?? 0,
@@ -1631,18 +1441,6 @@ export class Client extends ClientAbstract {
1631
1441
  alert: params?.alert ? true : undefined,
1632
1442
  }));
1633
1443
  }
1634
- async constructReplyMarkup(params) {
1635
- if (params?.replyMarkup) {
1636
- await this.assertBot("replyMarkup");
1637
- return replyMarkupToTlObject(params.replyMarkup, this.usernameResolver.bind(this));
1638
- }
1639
- }
1640
- static assertMsgHas(message, key) {
1641
- if (!(key in message) || message[key] === undefined) {
1642
- UNREACHABLE();
1643
- }
1644
- return message;
1645
- }
1646
1444
  /**
1647
1445
  * Send a poll.
1648
1446
  *
@@ -1660,9 +1458,9 @@ export class Client extends ClientAbstract {
1660
1458
  const replyToMsgId = params?.replyToMessageId;
1661
1459
  const topMsgId = params?.messageThreadId;
1662
1460
  const sendAs = params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined; // TODO: check default sendAs
1663
- const replyMarkup = await this.constructReplyMarkup(params);
1461
+ const replyMarkup = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_constructReplyMarkup).call(this, params);
1664
1462
  const explanation = params?.explanation;
1665
- const parseResult = explanation !== undefined ? this.parseText(explanation, { parseMode: params?.explanationParseMode, entities: params?.explanationEntities }) : undefined;
1463
+ const parseResult = explanation !== undefined ? __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).call(this, explanation, { parseMode: params?.explanationParseMode, entities: params?.explanationEntities }) : undefined;
1666
1464
  const solution = parseResult === undefined ? undefined : parseResult[0];
1667
1465
  const solutionEntities = parseResult === undefined ? undefined : parseResult[1];
1668
1466
  const answers = options.map((v, i) => new types.PollAnswer({ option: new Uint8Array([i]), text: v }));
@@ -1694,8 +1492,8 @@ export class Client extends ClientAbstract {
1694
1492
  media,
1695
1493
  message: "",
1696
1494
  }));
1697
- const message = await this.updatesToMessages(chatId, result).then((v) => v[0]);
1698
- return Client.assertMsgHas(message, "poll");
1495
+ const message = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1496
+ return __classPrivateFieldGet(Client, _a, "m", _Client_assertMsgHas).call(Client, message, "poll");
1699
1497
  }
1700
1498
  /**
1701
1499
  * Send a chat action.
@@ -1775,7 +1573,7 @@ export class Client extends ClientAbstract {
1775
1573
  autoStart: false,
1776
1574
  });
1777
1575
  signal?.addEventListener("abort", () => drop(client.disconnect()));
1778
- client.state.salt = this.state.salt;
1576
+ __classPrivateFieldGet(client, _Client_state, "f").salt = __classPrivateFieldGet(this, _Client_state, "f").salt;
1779
1577
  await client.connect();
1780
1578
  let part = 0;
1781
1579
  const partCount = Math.ceil(contents.length / chunkSize);
@@ -1870,7 +1668,7 @@ export class Client extends ClientAbstract {
1870
1668
  async answerInlineQuery(id, results, params) {
1871
1669
  await this.invoke(new functions.MessagesSetInlineBotResults({
1872
1670
  queryId: BigInt(id),
1873
- results: await Promise.all(results.map((v) => inlineQueryResultToTlObject(v, this.parseText.bind(this), this.usernameResolver.bind(this)))),
1671
+ results: await Promise.all(results.map((v) => inlineQueryResultToTlObject(v, __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).bind(this), __classPrivateFieldGet(this, _Client_usernameResolver, "f").bind(this)))),
1874
1672
  cacheTime: params?.cacheTime ?? 300,
1875
1673
  private: params?.isPersonal ? true : undefined,
1876
1674
  switchWebview: params?.button && params.button.webApp ? new types.InlineBotWebView({ text: params.button.text, url: params.button.webApp.url }) : undefined,
@@ -1880,8 +1678,8 @@ export class Client extends ClientAbstract {
1880
1678
  }));
1881
1679
  }
1882
1680
  use(handler) {
1883
- const handle = this.handle;
1884
- this.handle = async (upd, next) => {
1681
+ const handle = __classPrivateFieldGet(this, _Client_handle, "f");
1682
+ __classPrivateFieldSet(this, _Client_handle, async (upd, next) => {
1885
1683
  let called = false;
1886
1684
  await handle(upd, async () => {
1887
1685
  if (called)
@@ -1889,7 +1687,7 @@ export class Client extends ClientAbstract {
1889
1687
  called = true;
1890
1688
  await handler(upd, next);
1891
1689
  });
1892
- };
1690
+ }, "f");
1893
1691
  }
1894
1692
  branch(predicate, trueHandler, falseHandler) {
1895
1693
  this.use(async (upd, next) => {
@@ -1925,17 +1723,14 @@ export class Client extends ClientAbstract {
1925
1723
  }
1926
1724
  }, handler);
1927
1725
  }
1928
- async setMyInfo(info) {
1929
- await this.invoke(new functions.BotsSetBotInfo({ bot: new types.InputUserSelf(), ...info }));
1930
- }
1931
1726
  /**
1932
1727
  * Set the bot's description in the given language. Bot-only.
1933
1728
  *
1934
1729
  * @method
1935
1730
  */
1936
1731
  async setMyDescription({ description, languageCode }) {
1937
- await this.assertBot("setMyDescription");
1938
- await this.setMyInfo({ description, langCode: languageCode ?? "" });
1732
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "setMyDescription");
1733
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setMyInfo).call(this, { description, langCode: languageCode ?? "" });
1939
1734
  }
1940
1735
  /**
1941
1736
  * Set the bot's name in the given language. Bot-only.
@@ -1943,8 +1738,8 @@ export class Client extends ClientAbstract {
1943
1738
  * @method
1944
1739
  */
1945
1740
  async setMyName({ name, languageCode }) {
1946
- await this.assertBot("setMyName");
1947
- await this.setMyInfo({ name, langCode: languageCode ?? "" });
1741
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "setMyName");
1742
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setMyInfo).call(this, { name, langCode: languageCode ?? "" });
1948
1743
  }
1949
1744
  /**
1950
1745
  * Set the bot's short description in the given language. Bot-only.
@@ -1952,11 +1747,8 @@ export class Client extends ClientAbstract {
1952
1747
  * @method
1953
1748
  */
1954
1749
  async setMyShortDescription({ shortDescription: about, languageCode }) {
1955
- await this.assertBot("setMyShortDescription");
1956
- await this.setMyInfo({ about, langCode: languageCode ?? "" });
1957
- }
1958
- getMyInfo(languageCode) {
1959
- return this.invoke(new functions.BotsGetBotInfo({ bot: new types.InputUserSelf(), langCode: languageCode ?? "" }));
1750
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "setMyShortDescription");
1751
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_setMyInfo).call(this, { about, langCode: languageCode ?? "" });
1960
1752
  }
1961
1753
  /**
1962
1754
  * Get the bot's description in the given language. Bot-only.
@@ -1964,8 +1756,8 @@ export class Client extends ClientAbstract {
1964
1756
  * @method
1965
1757
  */
1966
1758
  async getMyDescription(params) {
1967
- await this.assertBot("getMyDescription");
1968
- return await this.getMyInfo(params?.languageCode).then((v) => v.description);
1759
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "getMyDescription");
1760
+ return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getMyInfo).call(this, params?.languageCode).then((v) => v.description);
1969
1761
  }
1970
1762
  /**
1971
1763
  * Set the bot's name in the given language. Bot-only.
@@ -1973,8 +1765,8 @@ export class Client extends ClientAbstract {
1973
1765
  * @method
1974
1766
  */
1975
1767
  async getMyName(params) {
1976
- await this.assertBot("getMyName");
1977
- return await this.getMyInfo(params?.languageCode).then((v) => v.description);
1768
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "getMyName");
1769
+ return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getMyInfo).call(this, params?.languageCode).then((v) => v.description);
1978
1770
  }
1979
1771
  /**
1980
1772
  * Get the bot's short description in the given language. Bot-only.
@@ -1982,7 +1774,118 @@ export class Client extends ClientAbstract {
1982
1774
  * @method
1983
1775
  */
1984
1776
  async getMyShortDescription(params) {
1985
- await this.assertBot("getMyShortDescription");
1986
- return await this.getMyInfo(params?.languageCode).then((v) => v.about);
1777
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "getMyShortDescription");
1778
+ return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getMyInfo).call(this, params?.languageCode).then((v) => v.about);
1987
1779
  }
1988
1780
  }
1781
+ _a = Client, _Client_handleUpdate =
1782
+ // TODO: log errors
1783
+ async function _Client_handleUpdate(update) {
1784
+ if (update instanceof types.UpdateShortMessage) {
1785
+ update = new types.UpdateNewMessage({
1786
+ message: new types.Message({
1787
+ out: update.out,
1788
+ mentioned: update.mentioned,
1789
+ mediaUnread: update.mediaUnread,
1790
+ silent: update.silent,
1791
+ id: update.id,
1792
+ fromId: update.out ? new types.PeerUser({ userId: await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getSelfId).call(this).then(BigInt) }) : new types.PeerUser({ userId: update.userId }),
1793
+ peerId: new types.PeerChat({ chatId: update.userId }),
1794
+ message: update.message,
1795
+ date: update.date,
1796
+ fwdFrom: update.fwdFrom,
1797
+ viaBotId: update.viaBotId,
1798
+ replyTo: update.replyTo,
1799
+ entities: update.entities,
1800
+ ttlPeriod: update.ttlPeriod,
1801
+ }),
1802
+ pts: update.pts,
1803
+ ptsCount: update.ptsCount,
1804
+ });
1805
+ }
1806
+ else if (update instanceof types.UpdateShortChatMessage) {
1807
+ update = new types.UpdateNewMessage({
1808
+ message: new types.Message({
1809
+ out: update.out,
1810
+ mentioned: update.mentioned,
1811
+ mediaUnread: update.mediaUnread,
1812
+ silent: update.silent,
1813
+ id: update.id,
1814
+ fromId: new types.PeerUser({ userId: update.fromId }),
1815
+ peerId: new types.PeerChat({ chatId: update.chatId }),
1816
+ fwdFrom: update.fwdFrom,
1817
+ viaBotId: update.viaBotId,
1818
+ replyTo: update.replyTo,
1819
+ date: update.date,
1820
+ message: update.message,
1821
+ entities: update.entities,
1822
+ ttlPeriod: update.ttlPeriod,
1823
+ }),
1824
+ pts: update.pts,
1825
+ ptsCount: update.ptsCount,
1826
+ });
1827
+ }
1828
+ if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateNewChannelMessage) {
1829
+ if (update.message instanceof types.Message || update.message instanceof types.MessageService) {
1830
+ await this.storage.setMessage(peerToChatId(update.message.peerId), update.message.id, update.message);
1831
+ }
1832
+ }
1833
+ if (update instanceof types.UpdateNewMessage ||
1834
+ update instanceof types.UpdateNewChannelMessage ||
1835
+ update instanceof types.UpdateEditMessage ||
1836
+ update instanceof types.UpdateEditChannelMessage) {
1837
+ const key = update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage ? "message" : "editedMessage";
1838
+ const message = await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1839
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { [key]: message }, resolve);
1840
+ }
1841
+ if (update instanceof types.UpdateDeleteMessages) {
1842
+ const deletedMessages = new Array();
1843
+ for (const messageId of update.messages) {
1844
+ const chatId = await this.storage.getMessageChat(messageId);
1845
+ if (chatId) {
1846
+ const message = await this.storage.getMessage(chatId, messageId);
1847
+ if (message != null) {
1848
+ deletedMessages.push(await constructMessage(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1849
+ }
1850
+ await this.storage.setMessage(chatId, messageId, null);
1851
+ }
1852
+ }
1853
+ if (deletedMessages.length > 0) {
1854
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { deletedMessages: deletedMessages }, resolve);
1855
+ }
1856
+ }
1857
+ else if (update instanceof types.UpdateDeleteChannelMessages) {
1858
+ const chatId = getChannelChatId(update.channelId);
1859
+ const deletedMessages = new Array();
1860
+ for (const messageId of update.messages) {
1861
+ const message = await this.storage.getMessage(chatId, messageId);
1862
+ if (message) {
1863
+ deletedMessages.push(await constructMessage(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1864
+ }
1865
+ await this.storage.setMessage(chatId, messageId, null);
1866
+ }
1867
+ if (deletedMessages.length > 0) {
1868
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { deletedMessages: deletedMessages }, resolve);
1869
+ }
1870
+ }
1871
+ if (update instanceof types.UpdateBotCallbackQuery || update instanceof types.UpdateInlineBotCallbackQuery) {
1872
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, resolve);
1873
+ }
1874
+ else if (update instanceof types.UpdateBotInlineQuery) {
1875
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }, resolve);
1876
+ }
1877
+ }, _Client_constructReplyMarkup = async function _Client_constructReplyMarkup(params) {
1878
+ if (params?.replyMarkup) {
1879
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "replyMarkup");
1880
+ return replyMarkupToTlObject(params.replyMarkup, __classPrivateFieldGet(this, _Client_usernameResolver, "f").bind(this));
1881
+ }
1882
+ }, _Client_assertMsgHas = function _Client_assertMsgHas(message, key) {
1883
+ if (!(key in message) || message[key] === undefined) {
1884
+ UNREACHABLE();
1885
+ }
1886
+ return message;
1887
+ }, _Client_setMyInfo = async function _Client_setMyInfo(info) {
1888
+ await this.invoke(new functions.BotsSetBotInfo({ bot: new types.InputUserSelf(), ...info }));
1889
+ }, _Client_getMyInfo = function _Client_getMyInfo(languageCode) {
1890
+ return this.invoke(new functions.BotsGetBotInfo({ bot: new types.InputUserSelf(), langCode: languageCode ?? "" }));
1891
+ };