@mtkruto/node 0.0.989 → 0.0.991

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 (51) hide show
  1. package/esm/client/3_client.d.ts +11 -1
  2. package/esm/client/3_client.js +72 -23
  3. package/esm/constants.d.ts +1 -1
  4. package/esm/constants.js +1 -1
  5. package/esm/tl/2_types.d.ts +2055 -1617
  6. package/esm/tl/2_types.js +3447 -3447
  7. package/esm/tl/3_functions.d.ts +154 -154
  8. package/esm/tl/3_functions.js +988 -988
  9. package/esm/types/0_poll_option.d.ts +8 -0
  10. package/esm/types/0_poll_option.js +6 -0
  11. package/esm/types/1_chat.js +3 -4
  12. package/esm/types/1_keyboard_button.js +4 -5
  13. package/esm/types/1_photo.js +0 -2
  14. package/esm/types/1_poll.d.ts +32 -0
  15. package/esm/types/1_poll.js +23 -0
  16. package/esm/types/1_sticker.js +1 -1
  17. package/esm/types/1_user.js +1 -2
  18. package/esm/types/2_game.js +2 -2
  19. package/esm/types/2_inline_keyboard_markup.js +1 -2
  20. package/esm/types/2_reply_keyboard_markup.js +1 -2
  21. package/esm/types/3_message.d.ts +4 -0
  22. package/esm/types/3_message.js +7 -0
  23. package/esm/utilities/0_queue.d.ts +2 -0
  24. package/esm/utilities/0_queue.js +14 -2
  25. package/esm/utilities/1_misc.js +1 -1
  26. package/package.json +1 -1
  27. package/script/client/3_client.d.ts +11 -1
  28. package/script/client/3_client.js +72 -23
  29. package/script/constants.d.ts +1 -1
  30. package/script/constants.js +1 -1
  31. package/script/tl/2_types.d.ts +2055 -1617
  32. package/script/tl/2_types.js +3894 -3894
  33. package/script/tl/3_functions.d.ts +154 -154
  34. package/script/tl/3_functions.js +988 -988
  35. package/script/types/0_poll_option.d.ts +8 -0
  36. package/script/types/0_poll_option.js +10 -0
  37. package/script/types/1_chat.js +3 -4
  38. package/script/types/1_keyboard_button.js +4 -5
  39. package/script/types/1_photo.js +0 -2
  40. package/script/types/1_poll.d.ts +32 -0
  41. package/script/types/1_poll.js +27 -0
  42. package/script/types/1_sticker.js +1 -1
  43. package/script/types/1_user.js +1 -2
  44. package/script/types/2_game.js +2 -2
  45. package/script/types/2_inline_keyboard_markup.js +1 -2
  46. package/script/types/2_reply_keyboard_markup.js +1 -2
  47. package/script/types/3_message.d.ts +4 -0
  48. package/script/types/3_message.js +7 -0
  49. package/script/utilities/0_queue.d.ts +2 -0
  50. package/script/utilities/0_queue.js +14 -2
  51. package/script/utilities/1_misc.js +1 -1
@@ -194,6 +194,8 @@ export declare class Client extends ClientAbstract {
194
194
  private connectionInited;
195
195
  disconnect(): Promise<void>;
196
196
  private initConnection;
197
+ private lastPropagatedAuthorizationState;
198
+ private propagateAuthorizationState;
197
199
  /**
198
200
  * Calls [initConnection](1) and authorizes the client with one of the following:
199
201
  *
@@ -243,7 +245,10 @@ export declare class Client extends ClientAbstract {
243
245
  private getLocalState;
244
246
  private recoverUpdateGap;
245
247
  private recoverChannelUpdateGap;
248
+ private getUserAccessHash;
249
+ private getChannelAccessHash;
246
250
  getInputPeer(id: ChatID): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
251
+ private getInputPeerInner;
247
252
  [getEntity](peer: types.PeerUser): Promise<types.User | null>;
248
253
  [getEntity](peer: types.PeerChat): Promise<types.Chat | null>;
249
254
  [getEntity](peer: types.PeerChannel): Promise<types.Channel | null>;
@@ -309,16 +314,21 @@ export declare class Client extends ClientAbstract {
309
314
  private handleUpdate;
310
315
  handler: Handler;
311
316
  use(middleware: Handler): void;
312
- on<U extends keyof Update, K extends keyof Update[U]>(filter: Update[U] extends string ? U : Update[U] extends Array<any> ? U : U | [U, ...K[]], handler: Handler<Pick<{
317
+ on<U extends keyof Update, K extends keyof Update[U]>(filter: U extends FilterableUpdates ? U | [U, K, ...K[]] : U, handler: Handler<Pick<{
313
318
  [P in U]: With<Update[U], K>;
314
319
  }, U>>): void;
315
320
  }
316
321
  type With<T, K extends keyof T> = T & Required<Pick<T, K>>;
317
322
  export type ConnectionState = "not-connected" | "updating" | "ready";
323
+ export type AuthorizationState = {
324
+ authorized: boolean;
325
+ };
326
+ type FilterableUpdates = "message" | "editedMessage";
318
327
  export interface Update {
319
328
  message: Message;
320
329
  editedMessage: Message;
321
330
  connectionState: ConnectionState;
331
+ authorizationState: AuthorizationState;
322
332
  deletedMessages: [Message, ...Message[]];
323
333
  }
324
334
  export interface Handler<U extends Partial<Update> = Partial<Update>> {
@@ -204,6 +204,12 @@ export class Client extends ClientAbstract {
204
204
  writable: true,
205
205
  value: false
206
206
  });
207
+ Object.defineProperty(this, "lastPropagatedAuthorizationState", {
208
+ enumerable: true,
209
+ configurable: true,
210
+ writable: true,
211
+ value: null
212
+ });
207
213
  Object.defineProperty(this, "autoStarted", {
208
214
  enumerable: true,
209
215
  configurable: true,
@@ -220,13 +226,13 @@ export class Client extends ClientAbstract {
220
226
  enumerable: true,
221
227
  configurable: true,
222
228
  writable: true,
223
- value: new Queue()
229
+ value: new Queue("handleUpdate")
224
230
  });
225
231
  Object.defineProperty(this, "processUpdatesQueue", {
226
232
  enumerable: true,
227
233
  configurable: true,
228
234
  writable: true,
229
- value: new Queue()
235
+ value: new Queue("processUpdates")
230
236
  });
231
237
  Object.defineProperty(this, "handler", {
232
238
  enumerable: true,
@@ -278,11 +284,11 @@ export class Client extends ClientAbstract {
278
284
  * Before establishing the connection, the session is saved.
279
285
  */
280
286
  async connect() {
287
+ if (this.connected) {
288
+ return;
289
+ }
281
290
  const release = await this.connectMutex.acquire();
282
291
  try {
283
- if (this.connected) {
284
- return;
285
- }
286
292
  if (!this.storageInited) {
287
293
  await this.storage.init();
288
294
  this.storageInited = true;
@@ -362,6 +368,12 @@ export class Client extends ClientAbstract {
362
368
  d("connection inited");
363
369
  }
364
370
  }
371
+ async propagateAuthorizationState(authorized) {
372
+ if (this.lastPropagatedAuthorizationState != authorized) {
373
+ await this.handler({ authorizationState: { authorized } }, resolve);
374
+ this.lastPropagatedAuthorizationState = authorized;
375
+ }
376
+ }
365
377
  /**
366
378
  * Calls [initConnection](1) and authorizes the client with one of the following:
367
379
  *
@@ -399,6 +411,7 @@ export class Client extends ClientAbstract {
399
411
  await this.initConnection();
400
412
  try {
401
413
  await this.fetchState("authorize");
414
+ await this.propagateAuthorizationState(true);
402
415
  d("already authorized");
403
416
  return;
404
417
  }
@@ -432,6 +445,7 @@ export class Client extends ClientAbstract {
432
445
  }
433
446
  }
434
447
  dAuth("authorized as bot");
448
+ await this.propagateAuthorizationState(true);
435
449
  await this.fetchState("authorize");
436
450
  return;
437
451
  }
@@ -493,6 +507,7 @@ export class Client extends ClientAbstract {
493
507
  }));
494
508
  await this.storage.setAccountType("user");
495
509
  dAuth("authorized as user");
510
+ await this.propagateAuthorizationState(true);
496
511
  await this.fetchState("authorize");
497
512
  return;
498
513
  }
@@ -520,6 +535,7 @@ export class Client extends ClientAbstract {
520
535
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
521
536
  await this.storage.setAccountType("user");
522
537
  dAuth("authorized as user");
538
+ await this.propagateAuthorizationState(true);
523
539
  await this.fetchState("authorize");
524
540
  break;
525
541
  }
@@ -542,6 +558,7 @@ export class Client extends ClientAbstract {
542
558
  try {
543
559
  await this.fetchState("authorize");
544
560
  d("already authorized");
561
+ await this.propagateAuthorizationState(true);
545
562
  return;
546
563
  }
547
564
  catch (err) {
@@ -578,7 +595,7 @@ export class Client extends ClientAbstract {
578
595
  body = new TLReader(gunzip(body.packedData)).readObject();
579
596
  }
580
597
  dRecv("received %s", body.constructor.name);
581
- if (body instanceof types.TypeUpdates || body instanceof types.TypeUpdate) {
598
+ if (body instanceof types._TypeUpdates || body instanceof types._TypeUpdate) {
582
599
  this.processUpdatesQueue.add(() => this.processUpdates(body));
583
600
  }
584
601
  else if (message.body instanceof RPCResult) {
@@ -605,7 +622,7 @@ export class Client extends ClientAbstract {
605
622
  this.promises.delete(messageId);
606
623
  }
607
624
  };
608
- if (result instanceof types.TypeUpdates || result instanceof types.TypeUpdate) {
625
+ if (result instanceof types._TypeUpdates || result instanceof types._TypeUpdate) {
609
626
  this.processUpdatesQueue.add(async () => {
610
627
  await this.processUpdates(result);
611
628
  resolvePromise();
@@ -684,9 +701,18 @@ export class Client extends ClientAbstract {
684
701
  if (noWait) {
685
702
  return;
686
703
  }
687
- const result = await new Promise((resolve, reject) => {
688
- this.promises.set(message.id, { resolve, reject });
689
- });
704
+ let result;
705
+ try {
706
+ result = await new Promise((resolve, reject) => {
707
+ this.promises.set(message.id, { resolve, reject });
708
+ });
709
+ }
710
+ catch (err) {
711
+ if (err instanceof types.RPCError && err.errorMessage == "AUTH_KEY_UNREGISTERED") {
712
+ await this.propagateAuthorizationState(false);
713
+ }
714
+ throw err;
715
+ }
690
716
  if (result instanceof types.BadServerSalt) {
691
717
  return await this.invoke(function_);
692
718
  }
@@ -709,7 +735,7 @@ export class Client extends ClientAbstract {
709
735
  await this.storage.updateUsernames("channel", chat.id, [chat.username]);
710
736
  }
711
737
  if (chat.usernames) {
712
- await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
738
+ await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v.username));
713
739
  }
714
740
  }
715
741
  else if (chat instanceof types.Chat) {
@@ -726,7 +752,7 @@ export class Client extends ClientAbstract {
726
752
  await this.storage.updateUsernames("user", user.id, [user.username]);
727
753
  }
728
754
  if (user.usernames) {
729
- await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
755
+ await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v.username));
730
756
  }
731
757
  }
732
758
  }
@@ -780,7 +806,7 @@ export class Client extends ClientAbstract {
780
806
  await this.recoverUpdateGap("updatesTooLong");
781
807
  return;
782
808
  }
783
- else if (updates_ instanceof types.TypeUpdate) {
809
+ else if (updates_ instanceof types._TypeUpdate) {
784
810
  updates = [updates_];
785
811
  }
786
812
  else {
@@ -859,7 +885,7 @@ export class Client extends ClientAbstract {
859
885
  await this.recoverChannelUpdateGap(update.channelId, "updateChannelTooLong");
860
886
  }
861
887
  else if (update instanceof types.UpdateUserName) {
862
- await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
888
+ await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v.username));
863
889
  }
864
890
  else if (update instanceof types.UpdatePtsChanged) {
865
891
  await this.fetchState("updatePtsChanged");
@@ -871,7 +897,7 @@ export class Client extends ClientAbstract {
871
897
  }
872
898
  }
873
899
  /// If there were any Update, they will be passed to the update handling queue.
874
- if (update instanceof types.TypeUpdate) {
900
+ if (update instanceof types._TypeUpdate) {
875
901
  updatesToHandle.push(update);
876
902
  }
877
903
  }
@@ -923,12 +949,12 @@ export class Client extends ClientAbstract {
923
949
  await this.processUpdates(update, true);
924
950
  }
925
951
  if (difference instanceof types.UpdatesDifference) {
926
- await this.storage.setState(difference.state[as](types.UpdatesState));
952
+ await this.storage.setState(difference.state);
927
953
  dGap("recovered from update gap");
928
954
  break;
929
955
  }
930
956
  else if (difference instanceof types.UpdatesDifferenceSlice) {
931
- state = difference.intermediateState[as](types.UpdatesState);
957
+ state = difference.intermediateState;
932
958
  }
933
959
  else {
934
960
  UNREACHABLE();
@@ -1002,7 +1028,28 @@ export class Client extends ClientAbstract {
1002
1028
  }
1003
1029
  }
1004
1030
  }
1031
+ async getUserAccessHash(userId) {
1032
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUser({ userId, accessHash: 0n })] }));
1033
+ return users[0][as](types.User).accessHash ?? 0n;
1034
+ }
1035
+ async getChannelAccessHash(channelId) {
1036
+ const channels = await this.invoke(new functions.ChannelsGetChannels({ id: [new types.InputChannel({ channelId, accessHash: 0n })] }));
1037
+ return channels.chats[0][as](types.Channel).accessHash ?? 0n;
1038
+ }
1005
1039
  async getInputPeer(id) {
1040
+ const inputPeer = await this.getInputPeerInner(id);
1041
+ if (inputPeer instanceof types.InputPeerUser || inputPeer instanceof types.InputPeerChannel && inputPeer.accessHash == 0n && await this.storage.getAccountType() == "bot") {
1042
+ if ("channelId" in inputPeer) {
1043
+ inputPeer.accessHash = await this.getChannelAccessHash(inputPeer.channelId);
1044
+ }
1045
+ else {
1046
+ inputPeer.accessHash = await this.getUserAccessHash(inputPeer.userId);
1047
+ await this.storage.setUserAccessHash(inputPeer.userId, inputPeer.accessHash);
1048
+ }
1049
+ }
1050
+ return inputPeer;
1051
+ }
1052
+ async getInputPeerInner(id) {
1006
1053
  if (typeof id === "string") {
1007
1054
  if (!id.startsWith("@")) {
1008
1055
  throw new Error("Expected username to start with @");
@@ -1107,9 +1154,13 @@ export class Client extends ClientAbstract {
1107
1154
  result instanceof types.ChatlistsExportedInvites ||
1108
1155
  result instanceof types.ChatlistsChatlistInviteAlready ||
1109
1156
  result instanceof types.ChatlistsChatlistInvite ||
1110
- result instanceof types.ChatlistsChatlistUpdates) {
1157
+ result instanceof types.ChatlistsChatlistUpdates ||
1158
+ result instanceof types.MessagesChats ||
1159
+ result instanceof types.MessagesChatsSlice) {
1111
1160
  await this.processChats(result.chats);
1112
- await this.processUsers(result.users);
1161
+ if ("users" in result) {
1162
+ await this.processUsers(result.users);
1163
+ }
1113
1164
  }
1114
1165
  if (result instanceof types.MessagesMessages) {
1115
1166
  for (const message of result.messages) {
@@ -1381,7 +1432,7 @@ export class Client extends ClientAbstract {
1381
1432
  }
1382
1433
  else {
1383
1434
  const stickerSet = await this.invoke(new functions.MessagesGetStickerSet({ stickerset: inputStickerSet, hash }));
1384
- const name = stickerSet[as](types.MessagesStickerSet).set[as](types.StickerSet).shortName;
1435
+ const name = stickerSet[as](types.MessagesStickerSet).set.shortName;
1385
1436
  await this.storage.updateStickerSetName(inputStickerSet.id, inputStickerSet.accessHash, name);
1386
1437
  return name;
1387
1438
  }
@@ -1486,9 +1537,7 @@ export class Client extends ClientAbstract {
1486
1537
  });
1487
1538
  };
1488
1539
  }
1489
- on(
1490
- // deno-lint-ignore no-explicit-any
1491
- filter, handler) {
1540
+ on(filter, handler) {
1492
1541
  const type = typeof filter === "string" ? filter : filter[0];
1493
1542
  const keys = Array.isArray(filter) ? filter.slice(1) : [];
1494
1543
  this.use((update, next) => {
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 161;
8
- export declare const APP_VERSION = "MTKruto 0.0.989";
8
+ export declare const APP_VERSION = "MTKruto 0.0.991";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
package/esm/constants.js CHANGED
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
54
54
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
55
55
  export const INITIAL_DC = "2-test";
56
56
  export const LAYER = 161;
57
- export const APP_VERSION = "MTKruto 0.0.989";
57
+ export const APP_VERSION = "MTKruto 0.0.991";
58
58
  // @ts-ignore: lib
59
59
  export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
60
60
  export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];