@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
@@ -0,0 +1,8 @@
1
+ import * as types from "../tl/2_types.js";
2
+ export interface PollOption {
3
+ /** Option text, 1-100 characters */
4
+ text: string;
5
+ /** Number of users that voted for this option */
6
+ voterCount: number;
7
+ }
8
+ export declare function constructPollOption(option: types.PollAnswer, results: Array<types.TypePollAnswerVoters>): PollOption;
@@ -0,0 +1,6 @@
1
+ export function constructPollOption(option, results) {
2
+ return {
3
+ text: option.text,
4
+ voterCount: results.find((v) => v.option.every((v, i) => option.option[i] == v))?.voters ?? 0,
5
+ };
6
+ }
@@ -1,7 +1,6 @@
1
1
  import { ZERO_CHANNEL_ID } from "../constants.js";
2
2
  import { UNREACHABLE } from "../utilities/0_control.js";
3
3
  import { cleanObject } from "../utilities/0_object.js";
4
- import { as } from "../tl/1_tl_object.js";
5
4
  import * as types from "../tl/2_types.js";
6
5
  import { getIdColor } from "./!0_id_color.js";
7
6
  import { constructChatPhoto } from "./0_chat_photo.js";
@@ -29,7 +28,7 @@ export function constructChat(chat) {
29
28
  };
30
29
  if (chat_.isBot) {
31
30
  chat_.isRestricted = chat.restricted || false;
32
- chat_.restrictionReason = chat.restrictionReason?.map((v) => v[as](types.RestrictionReason));
31
+ chat_.restrictionReason = chat.restrictionReason;
33
32
  }
34
33
  if (chat.photo instanceof types.UserProfilePhoto) {
35
34
  chat_.photo = constructChatPhoto(chat.photo, chat_.id, chat.accessHash ?? 0n);
@@ -81,9 +80,9 @@ export function constructChat(chat) {
81
80
  };
82
81
  }
83
82
  chat_.username = chat.username;
84
- chat_.also = chat.usernames?.map((v) => v[as](types.Username)).map((v) => v.username);
83
+ chat_.also = chat.usernames?.map((v) => v.username);
85
84
  if (chat_.isRestricted) {
86
- chat_.restrictionReason = chat.restrictionReason?.map((v) => v[as](types.RestrictionReason));
85
+ chat_.restrictionReason = chat.restrictionReason;
87
86
  }
88
87
  if (chat.photo instanceof types.ChatPhoto) {
89
88
  chat_.photo = constructChatPhoto(chat.photo, chat_.id, chat.accessHash ?? 0n);
@@ -1,5 +1,4 @@
1
1
  import { UNREACHABLE } from "../utilities/0_control.js";
2
- import { as } from "../tl/1_tl_object.js";
3
2
  import * as types from "../tl/2_types.js";
4
3
  import { chatAdministratorRightsToTlObject, constructChatAdministratorRights } from "./0_chat_administrator_rights.js";
5
4
  export function constructKeyboardButton(button_) {
@@ -30,10 +29,10 @@ export function constructKeyboardButton(button_) {
30
29
  },
31
30
  };
32
31
  if (button_.peerType.botAdminRights) {
33
- button.requestChat.botAdministratorRights = constructChatAdministratorRights(button_.peerType.botAdminRights[as](types.ChatAdminRights));
32
+ button.requestChat.botAdministratorRights = constructChatAdministratorRights(button_.peerType.botAdminRights);
34
33
  }
35
34
  if (button_.peerType.userAdminRights) {
36
- button.requestChat.userAdministratorRights = constructChatAdministratorRights(button_.peerType.userAdminRights[as](types.ChatAdminRights));
35
+ button.requestChat.userAdministratorRights = constructChatAdministratorRights(button_.peerType.userAdminRights);
37
36
  }
38
37
  return button;
39
38
  }
@@ -48,10 +47,10 @@ export function constructKeyboardButton(button_) {
48
47
  },
49
48
  };
50
49
  if (button_.peerType.botAdminRights) {
51
- button.requestChat.botAdministratorRights = constructChatAdministratorRights(button_.peerType.botAdminRights[as](types.ChatAdminRights));
50
+ button.requestChat.botAdministratorRights = constructChatAdministratorRights(button_.peerType.botAdminRights);
52
51
  }
53
52
  if (button_.peerType.userAdminRights) {
54
- button.requestChat.userAdministratorRights = constructChatAdministratorRights(button_.peerType.userAdminRights[as](types.ChatAdminRights));
53
+ button.requestChat.userAdministratorRights = constructChatAdministratorRights(button_.peerType.userAdminRights);
55
54
  }
56
55
  return button;
57
56
  }
@@ -1,4 +1,3 @@
1
- import { as } from "../tl/1_tl_object.js";
2
1
  import * as types from "../tl/2_types.js";
3
2
  import { FileID, FileType, FileUniqueID, FileUniqueType, ThumbnailSource } from "./!0_file_id.js";
4
3
  import { constructThumbnail } from "./0_thumbnail.js";
@@ -13,7 +12,6 @@ export function constructPhoto(photo) {
13
12
  }
14
13
  })
15
14
  .filter((v) => v instanceof types.PhotoSize)
16
- .map((v) => v[as](types.PhotoSize))
17
15
  .sort((a, b) => a.size - b.size);
18
16
  const largest = sizes.slice(-1)[0];
19
17
  const { dcId, id: mediaId, accessHash, fileReference } = photo;
@@ -0,0 +1,32 @@
1
+ import * as types from "../tl/2_types.js";
2
+ import { MessageEntity } from "./0_message_entity.js";
3
+ import { PollOption } from "./0_poll_option.js";
4
+ export interface Poll {
5
+ /** Unique poll identifier */
6
+ id: string;
7
+ /** Poll question, 1-300 characters */
8
+ question: string;
9
+ /** List of poll options */
10
+ options: PollOption[];
11
+ /** Total number of users that voted in the poll */
12
+ totalVoterCount: number;
13
+ /** True, if the poll is closed */
14
+ isClosed: boolean;
15
+ /** True, if the poll is anonymous */
16
+ isAnonymous: boolean;
17
+ /** Poll type, currently can be “regular” or “quiz” */
18
+ type: "regular" | "quiz";
19
+ /** True, if the poll allows multiple answers */
20
+ allowMultipleAnswers?: boolean;
21
+ /** Optional. 0-based identifier of the correct answer option. Available only for polls in the quiz mode, which are closed, or was sent (not forwarded) by the bot or to the private chat with the bot. */
22
+ correctOptionId?: number;
23
+ /** Optional. Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style poll, 0-200 characters */
24
+ explanation?: string;
25
+ /** Optional. Special entities like usernames, URLs, bot commands, etc. that appear in the explanation */
26
+ explanationEntities?: MessageEntity[];
27
+ /** Optional. Amount of time in seconds the poll will be active after creation */
28
+ openPeriod?: number;
29
+ /** Optional. Point in time (Unix timestamp) when the poll will be automatically closed */
30
+ closeDate?: Date;
31
+ }
32
+ export declare function constructPoll(media_: types.MessageMediaPoll): Poll;
@@ -0,0 +1,23 @@
1
+ import { cleanObject } from "../utilities/0_object.js";
2
+ import { constructMessageEntity } from "./0_message_entity.js";
3
+ import { constructPollOption } from "./0_poll_option.js";
4
+ export function constructPoll(media_) {
5
+ const poll = media_.poll;
6
+ const correctOption = media_.results.results?.find((v) => v.correct)?.option;
7
+ const correctOptionId = correctOption !== undefined ? poll.answers.findIndex((v) => v.option.every((v, i) => correctOption[i] == v)) : undefined;
8
+ return cleanObject({
9
+ id: String(poll.id),
10
+ question: poll.question,
11
+ options: poll.answers.map((v) => constructPollOption(v, media_.results.results ?? [])),
12
+ totalVoterCount: media_.results.totalVoters ?? 0,
13
+ isClosed: poll.closed || false,
14
+ isAnonymous: !poll.publicVoters,
15
+ type: poll.quiz ? "quiz" : "regular",
16
+ allowMultipleAnswers: poll.quiz ? undefined : poll.multipleChoice || false,
17
+ correctOptionId,
18
+ explanation: media_.results.solution,
19
+ explanationEntities: media_.results.solutionEntities?.map(constructMessageEntity).filter((v) => v != null),
20
+ openPeriod: poll.closePeriod,
21
+ closeDate: poll.closeDate ? new Date(poll.closeDate * 1000) : undefined,
22
+ });
23
+ }
@@ -20,7 +20,7 @@ export async function constructSticker(document, fileId, fileUniqueId, getSticke
20
20
  emoji: stickerAttribute.alt || undefined,
21
21
  setName,
22
22
  premiumAnimation: undefined,
23
- maskPosition: stickerAttribute.maskCoords ? constructMaskPosition(stickerAttribute.maskCoords[as](types.MaskCoords)) : undefined,
23
+ maskPosition: stickerAttribute.maskCoords ? constructMaskPosition(stickerAttribute.maskCoords) : undefined,
24
24
  customEmojiId: undefined,
25
25
  needsRepainting: undefined,
26
26
  fileSize: Number(document.size),
@@ -1,5 +1,4 @@
1
1
  import { cleanObject } from "../utilities/0_object.js";
2
- import { as } from "../tl/1_tl_object.js";
3
2
  import * as types from "../tl/2_types.js";
4
3
  import { getIdColor } from "./!0_id_color.js";
5
4
  import { constructChatPhoto } from "./0_chat_photo.js";
@@ -12,7 +11,7 @@ export function constructUser(user_) {
12
11
  firstName: user_.firstName || "",
13
12
  lastName: user_.lastName,
14
13
  username: user_.username,
15
- also: user_.usernames?.map((v) => v[as](types.Username)).map((v) => v.username),
14
+ also: user_.usernames?.map((v) => v.username),
16
15
  languageCode: user_.langCode,
17
16
  isScam: user_.scam || false,
18
17
  isFake: user_.fake || false,
@@ -5,11 +5,11 @@ import { FileID, FileType, FileUniqueID, FileUniqueType } from "./!0_file_id.js"
5
5
  import { constructAnimation } from "./1_animation.js";
6
6
  import { constructPhoto } from "./1_photo.js";
7
7
  export function constructGame(media_) {
8
- const game_ = media_.game[as](types.Game);
8
+ const game_ = media_.game;
9
9
  const document_ = game_.document ? game_.document[as](types.Document) : undefined;
10
10
  return cleanObject({
11
11
  title: game_.title,
12
- description: game_.description,
12
+ description: media_.game.description,
13
13
  photo: constructPhoto(game_.photo[as](types.Photo)),
14
14
  animation: document_
15
15
  ? constructAnimation(document_, document_.attributes.find((v) => v instanceof types.DocumentAttributeVideo), document_.attributes.find((v) => v instanceof types.DocumentAttributeFilename), new FileID(null, null, FileType.Animation, document_.dcId, {
@@ -1,9 +1,8 @@
1
- import { as } from "../tl/1_tl_object.js";
2
1
  import * as types from "../tl/2_types.js";
3
2
  import { constructInlineKeyboardButton, inlineKeyboardButtonToTlObject } from "./1_inline_keyboard_button.js";
4
3
  export function constructInlineKeyboardMarkup(keyboard_) {
5
4
  const rows = new Array();
6
- for (const row_ of keyboard_.rows.map((v) => v[as](types.KeyboardButtonRow))) {
5
+ for (const row_ of keyboard_.rows) {
7
6
  const row = new Array();
8
7
  for (const button_ of row_.buttons) {
9
8
  row.push(constructInlineKeyboardButton(button_));
@@ -1,9 +1,8 @@
1
- import { as } from "../tl/1_tl_object.js";
2
1
  import * as types from "../tl/2_types.js";
3
2
  import { constructKeyboardButton, keyboardButtonToTlObject } from "./1_keyboard_button.js";
4
3
  export function constructReplyKeyboardMarkup(keyboard_) {
5
4
  const rows = new Array();
6
- for (const row_ of keyboard_.rows.map((v) => v[as](types.KeyboardButtonRow))) {
5
+ for (const row_ of keyboard_.rows) {
7
6
  const row = new Array();
8
7
  for (const button_ of row_.buttons) {
9
8
  row.push(constructKeyboardButton(button_));
@@ -18,6 +18,7 @@ import { Voice } from "./0_voice.js";
18
18
  import { Dice } from "./0_dice.js";
19
19
  import { Contact } from "./0_contact.js";
20
20
  import { Game } from "./2_game.js";
21
+ import { Poll } from "./1_poll.js";
21
22
  import { Venue } from "./0_venue.js";
22
23
  import { Location } from "./0_location.js";
23
24
  /** This object represents a message. */
@@ -36,6 +37,8 @@ export interface Message {
36
37
  date?: Date;
37
38
  /** Conversation the message belongs to */
38
39
  chat: Chat;
40
+ /** A link to the message */
41
+ link?: string;
39
42
  /** For forwarded messages, sender of the original message */
40
43
  forwardFrom?: User;
41
44
  /** For messages forwarded from channels or from anonymous administrators, information about the original sender chat */
@@ -100,6 +103,7 @@ export interface Message {
100
103
  contact?: Contact;
101
104
  /** Message is a game, information about the game. */
102
105
  game?: Game;
106
+ poll?: Poll;
103
107
  /** Message is a venue, information about the venue. For backward compatibility, when this field is set, the location field will also be set */
104
108
  venue?: Venue;
105
109
  /** Message is a shared location, information about the location */
@@ -22,6 +22,7 @@ import { constructDice } from "./0_dice.js";
22
22
  import { FileID, FileType, FileUniqueID, FileUniqueType } from "./!0_file_id.js";
23
23
  import { constructContact } from "./0_contact.js";
24
24
  import { constructGame } from "./2_game.js";
25
+ import { constructPoll } from "./1_poll.js";
25
26
  import { constructVenue } from "./0_venue.js";
26
27
  import { constructLocation } from "./0_location.js";
27
28
  import { ZERO_CHANNEL_ID } from "../constants.js";
@@ -184,6 +185,7 @@ export async function constructMessage(message_, getEntity, getMessage, getStick
184
185
  if (!(message_ instanceof types.Message) && !(message_ instanceof types.MessageService)) {
185
186
  UNREACHABLE();
186
187
  }
188
+ let link;
187
189
  let chat_ = null;
188
190
  if (message_.peerId instanceof types.PeerUser) {
189
191
  const entity = await getEntity(message_.peerId);
@@ -204,6 +206,7 @@ export async function constructMessage(message_, getEntity, getMessage, getStick
204
206
  }
205
207
  }
206
208
  else if (message_.peerId instanceof types.PeerChannel) {
209
+ link = `https://t.me/c/${message_.peerId.channelId}/${message_.id}`;
207
210
  const entity = await getEntity(message_.peerId);
208
211
  if (entity) {
209
212
  chat_ = constructChat(entity);
@@ -222,6 +225,7 @@ export async function constructMessage(message_, getEntity, getMessage, getStick
222
225
  out: message_.out ?? false,
223
226
  id: message_.id,
224
227
  chat: chat_,
228
+ link,
225
229
  date: new Date(message_.date * 1000),
226
230
  views: message_.views,
227
231
  isTopicMessage: false,
@@ -376,6 +380,9 @@ export async function constructMessage(message_, getEntity, getMessage, getStick
376
380
  else if (message_.media instanceof types.MessageMediaGame) {
377
381
  message.game = constructGame(message_.media);
378
382
  }
383
+ else if (message_.media instanceof types.MessageMediaPoll) {
384
+ message.poll = constructPoll(message_.media);
385
+ }
379
386
  else if (message_.media instanceof types.MessageMediaVenue) {
380
387
  message.venue = constructVenue(message_.media);
381
388
  }
@@ -1,5 +1,7 @@
1
1
  export declare class Queue {
2
+ private d;
2
3
  private functions;
4
+ constructor(name: string);
3
5
  add(fn: () => Promise<void>): void;
4
6
  private busy;
5
7
  private check;
@@ -1,5 +1,12 @@
1
+ import { debug } from "../deps.js";
1
2
  export class Queue {
2
- constructor() {
3
+ constructor(name) {
4
+ Object.defineProperty(this, "d", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
3
10
  Object.defineProperty(this, "functions", {
4
11
  enumerable: true,
5
12
  configurable: true,
@@ -12,6 +19,7 @@ export class Queue {
12
19
  writable: true,
13
20
  value: false
14
21
  });
22
+ this.d = debug(`q/${name}`);
15
23
  }
16
24
  add(fn) {
17
25
  this.functions.push(fn);
@@ -26,7 +34,11 @@ export class Queue {
26
34
  }
27
35
  const fn = this.functions.shift();
28
36
  if (fn !== undefined) {
29
- fn().finally(() => {
37
+ fn()
38
+ .catch((err) => {
39
+ this.d("%o", err);
40
+ })
41
+ .finally(() => {
30
42
  this.busy = false;
31
43
  this.check();
32
44
  });
@@ -1,6 +1,6 @@
1
1
  import { UNREACHABLE } from "./0_control.js";
2
2
  export function drop(promise) {
3
- promise.then().catch();
3
+ promise.catch(() => { });
4
4
  }
5
5
  export function mustPrompt(message) {
6
6
  const result = prompt(message);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/node",
5
- "version": "0.0.989",
5
+ "version": "0.0.991",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -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>> {
@@ -230,6 +230,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
230
230
  writable: true,
231
231
  value: false
232
232
  });
233
+ Object.defineProperty(this, "lastPropagatedAuthorizationState", {
234
+ enumerable: true,
235
+ configurable: true,
236
+ writable: true,
237
+ value: null
238
+ });
233
239
  Object.defineProperty(this, "autoStarted", {
234
240
  enumerable: true,
235
241
  configurable: true,
@@ -246,13 +252,13 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
246
252
  enumerable: true,
247
253
  configurable: true,
248
254
  writable: true,
249
- value: new _0_queue_js_1.Queue()
255
+ value: new _0_queue_js_1.Queue("handleUpdate")
250
256
  });
251
257
  Object.defineProperty(this, "processUpdatesQueue", {
252
258
  enumerable: true,
253
259
  configurable: true,
254
260
  writable: true,
255
- value: new _0_queue_js_1.Queue()
261
+ value: new _0_queue_js_1.Queue("processUpdates")
256
262
  });
257
263
  Object.defineProperty(this, "handler", {
258
264
  enumerable: true,
@@ -304,11 +310,11 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
304
310
  * Before establishing the connection, the session is saved.
305
311
  */
306
312
  async connect() {
313
+ if (this.connected) {
314
+ return;
315
+ }
307
316
  const release = await this.connectMutex.acquire();
308
317
  try {
309
- if (this.connected) {
310
- return;
311
- }
312
318
  if (!this.storageInited) {
313
319
  await this.storage.init();
314
320
  this.storageInited = true;
@@ -388,6 +394,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
388
394
  d("connection inited");
389
395
  }
390
396
  }
397
+ async propagateAuthorizationState(authorized) {
398
+ if (this.lastPropagatedAuthorizationState != authorized) {
399
+ await this.handler({ authorizationState: { authorized } }, resolve);
400
+ this.lastPropagatedAuthorizationState = authorized;
401
+ }
402
+ }
391
403
  /**
392
404
  * Calls [initConnection](1) and authorizes the client with one of the following:
393
405
  *
@@ -425,6 +437,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
425
437
  await this.initConnection();
426
438
  try {
427
439
  await this.fetchState("authorize");
440
+ await this.propagateAuthorizationState(true);
428
441
  d("already authorized");
429
442
  return;
430
443
  }
@@ -458,6 +471,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
458
471
  }
459
472
  }
460
473
  dAuth("authorized as bot");
474
+ await this.propagateAuthorizationState(true);
461
475
  await this.fetchState("authorize");
462
476
  return;
463
477
  }
@@ -519,6 +533,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
519
533
  }));
520
534
  await this.storage.setAccountType("user");
521
535
  dAuth("authorized as user");
536
+ await this.propagateAuthorizationState(true);
522
537
  await this.fetchState("authorize");
523
538
  return;
524
539
  }
@@ -546,6 +561,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
546
561
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
547
562
  await this.storage.setAccountType("user");
548
563
  dAuth("authorized as user");
564
+ await this.propagateAuthorizationState(true);
549
565
  await this.fetchState("authorize");
550
566
  break;
551
567
  }
@@ -568,6 +584,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
568
584
  try {
569
585
  await this.fetchState("authorize");
570
586
  d("already authorized");
587
+ await this.propagateAuthorizationState(true);
571
588
  return;
572
589
  }
573
590
  catch (err) {
@@ -604,7 +621,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
604
621
  body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
605
622
  }
606
623
  dRecv("received %s", body.constructor.name);
607
- if (body instanceof types.TypeUpdates || body instanceof types.TypeUpdate) {
624
+ if (body instanceof types._TypeUpdates || body instanceof types._TypeUpdate) {
608
625
  this.processUpdatesQueue.add(() => this.processUpdates(body));
609
626
  }
610
627
  else if (message.body instanceof _5_rpc_result_js_1.RPCResult) {
@@ -631,7 +648,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
631
648
  this.promises.delete(messageId);
632
649
  }
633
650
  };
634
- if (result instanceof types.TypeUpdates || result instanceof types.TypeUpdate) {
651
+ if (result instanceof types._TypeUpdates || result instanceof types._TypeUpdate) {
635
652
  this.processUpdatesQueue.add(async () => {
636
653
  await this.processUpdates(result);
637
654
  resolvePromise();
@@ -710,9 +727,18 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
710
727
  if (noWait) {
711
728
  return;
712
729
  }
713
- const result = await new Promise((resolve, reject) => {
714
- this.promises.set(message.id, { resolve, reject });
715
- });
730
+ let result;
731
+ try {
732
+ result = await new Promise((resolve, reject) => {
733
+ this.promises.set(message.id, { resolve, reject });
734
+ });
735
+ }
736
+ catch (err) {
737
+ if (err instanceof types.RPCError && err.errorMessage == "AUTH_KEY_UNREGISTERED") {
738
+ await this.propagateAuthorizationState(false);
739
+ }
740
+ throw err;
741
+ }
716
742
  if (result instanceof types.BadServerSalt) {
717
743
  return await this.invoke(function_);
718
744
  }
@@ -735,7 +761,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
735
761
  await this.storage.updateUsernames("channel", chat.id, [chat.username]);
736
762
  }
737
763
  if (chat.usernames) {
738
- await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
764
+ await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v.username));
739
765
  }
740
766
  }
741
767
  else if (chat instanceof types.Chat) {
@@ -752,7 +778,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
752
778
  await this.storage.updateUsernames("user", user.id, [user.username]);
753
779
  }
754
780
  if (user.usernames) {
755
- await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
781
+ await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v.username));
756
782
  }
757
783
  }
758
784
  }
@@ -806,7 +832,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
806
832
  await this.recoverUpdateGap("updatesTooLong");
807
833
  return;
808
834
  }
809
- else if (updates_ instanceof types.TypeUpdate) {
835
+ else if (updates_ instanceof types._TypeUpdate) {
810
836
  updates = [updates_];
811
837
  }
812
838
  else {
@@ -885,7 +911,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
885
911
  await this.recoverChannelUpdateGap(update.channelId, "updateChannelTooLong");
886
912
  }
887
913
  else if (update instanceof types.UpdateUserName) {
888
- await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
914
+ await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v.username));
889
915
  }
890
916
  else if (update instanceof types.UpdatePtsChanged) {
891
917
  await this.fetchState("updatePtsChanged");
@@ -897,7 +923,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
897
923
  }
898
924
  }
899
925
  /// If there were any Update, they will be passed to the update handling queue.
900
- if (update instanceof types.TypeUpdate) {
926
+ if (update instanceof types._TypeUpdate) {
901
927
  updatesToHandle.push(update);
902
928
  }
903
929
  }
@@ -949,12 +975,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
949
975
  await this.processUpdates(update, true);
950
976
  }
951
977
  if (difference instanceof types.UpdatesDifference) {
952
- await this.storage.setState(difference.state[_1_tl_object_js_1.as](types.UpdatesState));
978
+ await this.storage.setState(difference.state);
953
979
  dGap("recovered from update gap");
954
980
  break;
955
981
  }
956
982
  else if (difference instanceof types.UpdatesDifferenceSlice) {
957
- state = difference.intermediateState[_1_tl_object_js_1.as](types.UpdatesState);
983
+ state = difference.intermediateState;
958
984
  }
959
985
  else {
960
986
  (0, _0_control_js_1.UNREACHABLE)();
@@ -1028,7 +1054,28 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1028
1054
  }
1029
1055
  }
1030
1056
  }
1057
+ async getUserAccessHash(userId) {
1058
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUser({ userId, accessHash: 0n })] }));
1059
+ return users[0][_1_tl_object_js_1.as](types.User).accessHash ?? 0n;
1060
+ }
1061
+ async getChannelAccessHash(channelId) {
1062
+ const channels = await this.invoke(new functions.ChannelsGetChannels({ id: [new types.InputChannel({ channelId, accessHash: 0n })] }));
1063
+ return channels.chats[0][_1_tl_object_js_1.as](types.Channel).accessHash ?? 0n;
1064
+ }
1031
1065
  async getInputPeer(id) {
1066
+ const inputPeer = await this.getInputPeerInner(id);
1067
+ if (inputPeer instanceof types.InputPeerUser || inputPeer instanceof types.InputPeerChannel && inputPeer.accessHash == 0n && await this.storage.getAccountType() == "bot") {
1068
+ if ("channelId" in inputPeer) {
1069
+ inputPeer.accessHash = await this.getChannelAccessHash(inputPeer.channelId);
1070
+ }
1071
+ else {
1072
+ inputPeer.accessHash = await this.getUserAccessHash(inputPeer.userId);
1073
+ await this.storage.setUserAccessHash(inputPeer.userId, inputPeer.accessHash);
1074
+ }
1075
+ }
1076
+ return inputPeer;
1077
+ }
1078
+ async getInputPeerInner(id) {
1032
1079
  if (typeof id === "string") {
1033
1080
  if (!id.startsWith("@")) {
1034
1081
  throw new Error("Expected username to start with @");
@@ -1133,9 +1180,13 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1133
1180
  result instanceof types.ChatlistsExportedInvites ||
1134
1181
  result instanceof types.ChatlistsChatlistInviteAlready ||
1135
1182
  result instanceof types.ChatlistsChatlistInvite ||
1136
- result instanceof types.ChatlistsChatlistUpdates) {
1183
+ result instanceof types.ChatlistsChatlistUpdates ||
1184
+ result instanceof types.MessagesChats ||
1185
+ result instanceof types.MessagesChatsSlice) {
1137
1186
  await this.processChats(result.chats);
1138
- await this.processUsers(result.users);
1187
+ if ("users" in result) {
1188
+ await this.processUsers(result.users);
1189
+ }
1139
1190
  }
1140
1191
  if (result instanceof types.MessagesMessages) {
1141
1192
  for (const message of result.messages) {
@@ -1407,7 +1458,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1407
1458
  }
1408
1459
  else {
1409
1460
  const stickerSet = await this.invoke(new functions.MessagesGetStickerSet({ stickerset: inputStickerSet, hash }));
1410
- const name = stickerSet[_1_tl_object_js_1.as](types.MessagesStickerSet).set[_1_tl_object_js_1.as](types.StickerSet).shortName;
1461
+ const name = stickerSet[_1_tl_object_js_1.as](types.MessagesStickerSet).set.shortName;
1411
1462
  await this.storage.updateStickerSetName(inputStickerSet.id, inputStickerSet.accessHash, name);
1412
1463
  return name;
1413
1464
  }
@@ -1512,9 +1563,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1512
1563
  });
1513
1564
  };
1514
1565
  }
1515
- on(
1516
- // deno-lint-ignore no-explicit-any
1517
- filter, handler) {
1566
+ on(filter, handler) {
1518
1567
  const type = typeof filter === "string" ? filter : filter[0];
1519
1568
  const keys = Array.isArray(filter) ? filter.slice(1) : [];
1520
1569
  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 = "";