@mtkruto/node 0.1.128 → 0.1.130

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 (43) hide show
  1. package/esm/0_deps.d.ts +1 -0
  2. package/esm/0_deps.js +9 -0
  3. package/esm/4_constants.d.ts +1 -1
  4. package/esm/4_constants.js +1 -1
  5. package/esm/client/5_client.d.ts +17 -3
  6. package/esm/client/5_client.js +310 -79
  7. package/esm/deps/deno.land/std@0.209.0/media_types/extension.d.ts +17 -0
  8. package/esm/deps/deno.land/std@0.209.0/media_types/extension.js +26 -0
  9. package/esm/deps/deno.land/std@0.209.0/media_types/extensions_by_type.d.ts +20 -0
  10. package/esm/deps/deno.land/std@0.209.0/media_types/extensions_by_type.js +31 -0
  11. package/esm/storage/0_storage.d.ts +8 -7
  12. package/esm/storage/0_storage.js +39 -9
  13. package/esm/tl/0_tl_raw_reader.js +1 -1
  14. package/esm/tl/3_utilities.d.ts +1 -0
  15. package/esm/tl/3_utilities.js +11 -0
  16. package/esm/types/1__getters.d.ts +3 -2
  17. package/esm/types/1_chat_p.d.ts +2 -2
  18. package/esm/types/1_chat_p.js +16 -4
  19. package/esm/types/1_sticker.js +5 -4
  20. package/esm/types/4_chat.d.ts +3 -2
  21. package/esm/types/4_chat.js +16 -27
  22. package/package.json +1 -1
  23. package/script/0_deps.d.ts +1 -0
  24. package/script/0_deps.js +11 -1
  25. package/script/4_constants.d.ts +1 -1
  26. package/script/4_constants.js +1 -1
  27. package/script/client/5_client.d.ts +17 -3
  28. package/script/client/5_client.js +307 -76
  29. package/script/deps/deno.land/std@0.209.0/media_types/extension.d.ts +17 -0
  30. package/script/deps/deno.land/std@0.209.0/media_types/extension.js +30 -0
  31. package/script/deps/deno.land/std@0.209.0/media_types/extensions_by_type.d.ts +20 -0
  32. package/script/deps/deno.land/std@0.209.0/media_types/extensions_by_type.js +35 -0
  33. package/script/storage/0_storage.d.ts +8 -7
  34. package/script/storage/0_storage.js +39 -9
  35. package/script/tl/0_tl_raw_reader.js +1 -1
  36. package/script/tl/3_utilities.d.ts +1 -0
  37. package/script/tl/3_utilities.js +13 -1
  38. package/script/types/1__getters.d.ts +3 -2
  39. package/script/types/1_chat_p.d.ts +2 -2
  40. package/script/types/1_chat_p.js +16 -4
  41. package/script/types/1_sticker.js +4 -3
  42. package/script/types/4_chat.d.ts +3 -2
  43. package/script/types/4_chat.js +17 -27
@@ -9,12 +9,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Client_instances, _a, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_updateState, _Client_publicKeys, _Client_autoStart, _Client_ignoreOutgoing, _Client_prefixes, _Client_namespaceProxies, _Client_constructContext, _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_pingLoopAbortSignal, _Client_pingInterval, _Client_pingLoop, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_invoke, _Client_handleInvokeError, _Client_processChats, _Client_processUsers, _Client_handleUpdateQueue, _Client_processUpdatesQueue, _Client_checkGap, _Client_checkChannelGap, _Client_lastUpdates, _Client_processUpdates, _Client_setUpdateStateDate, _Client_setUpdatePts, _Client_getLocalState, _Client_recoverUpdateGap, _Client_recoverChannelUpdateGap, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_updatesToMessages, _Client_resolveSendAs, _Client_parseText, _Client_getMessagesInner, _Client_downloadInner, _Client_lastGetMe, _Client_getMe, _Client_handleUpdate, _Client_usernameResolver, _Client_constructReplyMarkup, _Client_assertMsgHas, _Client_handle, _Client_setMyInfo, _Client_getMyInfo, _Client_resolveFileId, _Client_sendMedia, _Client_sendChatUpdate, _Client_reassignChatLastMessage, _Client_chats, _Client_archivedChats, _Client_chatsLoadedFromStorage, _Client_getChatAnywhere, _Client_getChatList, _Client_loadChatsFromStorage, _Client_getLoadedChats, _Client_pinnedChats, _Client_pinnedArchiveChats, _Client_storageHadPinnedChats, _Client_pinnedChatsLoaded, _Client_loadPinnedChats, _Client_fetchPinnedChats, _Client_getPinnedChats, _Client_updatePinnedChats, _Client_fetchChats;
13
- import { contentType, debug, gunzip, Mutex } from "../0_deps.js";
12
+ var _Client_instances, _a, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_updateState, _Client_publicKeys, _Client_autoStart, _Client_ignoreOutgoing, _Client_prefixes, _Client_namespaceProxies, _Client_constructContext, _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_pingLoopAbortSignal, _Client_pingInterval, _Client_pingLoop, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_invoke, _Client_handleInvokeError, _Client_processChats, _Client_processUsers, _Client_handleUpdateQueue, _Client_processUpdatesQueue, _Client_checkGap, _Client_checkChannelGap, _Client_lastUpdates, _Client_processUpdates, _Client_setUpdateStateDate, _Client_setUpdatePts, _Client_getLocalState, _Client_recoverUpdateGap, _Client_recoverChannelUpdateGap, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_updatesToMessages, _Client_resolveSendAs, _Client_parseText, _Client_getMessagesInner, _Client_downloadInner, _Client_lastGetMe, _Client_getMe, _Client_handleUpdate, _Client_usernameResolver, _Client_constructReplyMarkup, _Client_assertMsgHas, _Client_handle, _Client_setMyInfo, _Client_getMyInfo, _Client_resolveFileId, _Client_sendMedia, _Client_sendChatUpdate, _Client_reassignChatLastMessage, _Client_chats, _Client_archivedChats, _Client_chatsLoadedFromStorage, _Client_tryGetChatId, _Client_getChatAnywhere, _Client_getChatList, _Client_loadChatsFromStorage, _Client_getLoadedChats, _Client_pinnedChats, _Client_pinnedArchiveChats, _Client_storageHadPinnedChats, _Client_pinnedChatsLoaded, _Client_loadPinnedChats, _Client_fetchPinnedChats, _Client_getPinnedChats, _Client_updateOrAddChat, _Client_removeChat, _Client_updatePinnedChats, _Client_fetchChats;
13
+ import { contentType, debug, extension, gunzip, Mutex } from "../0_deps.js";
14
14
  import { bigIntFromBuffer, cleanObject, drop, getRandomBigInt, getRandomId, mod, mustPrompt, mustPromptOneOf, Queue, sha1, UNREACHABLE, ZERO_CHANNEL_ID } from "../1_utilities.js";
15
15
  import { as, functions, getChannelChatId, Message_, MessageContainer, name, peerToChatId, RPCResult, TLError, TLReader, types } from "../2_tl.js";
16
16
  import { StorageMemory } from "../3_storage.js";
17
- import { botCommandScopeToTlObject, constructCallbackQuery, constructChat, constructChat2, constructChat3, constructChosenInlineResult, constructInlineQuery, constructMessage, constructUser, FileID, FileType, getChatOrder, inlineQueryResultToTlObject, messageEntityToTlObject, replyMarkupToTlObject, ThumbnailSource } from "../3_types.js";
17
+ import { botCommandScopeToTlObject, constructCallbackQuery, constructChat, constructChat2, constructChat3, constructChat4, constructChosenInlineResult, constructDocument, constructInlineQuery, constructMessage, constructUser, FileID, FileType, FileUniqueID, FileUniqueType, getChatOrder, inlineQueryResultToTlObject, messageEntityToTlObject, replyMarkupToTlObject, ThumbnailSource } from "../3_types.js";
18
18
  import { ACK_THRESHOLD, APP_VERSION, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, STICKER_SET_NAME_TTL, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL } from "../4_constants.js";
19
19
  import { AuthKeyUnregistered, FloodWait, Migrate, PasswordHashInvalid, PhoneNumberInvalid, SessionPasswordNeeded, upgradeInstance } from "../4_errors.js";
20
20
  import { parseHtml } from "./0_html.js";
@@ -23,7 +23,7 @@ import { checkPassword } from "./0_password.js";
23
23
  import { getFileContents, getUsername, isChannelPtsUpdate, isHttpUrl, isPtsUpdate, resolve } from "./0_utilities.js";
24
24
  import { ClientAbstract } from "./1_client_abstract.js";
25
25
  import { ClientPlain } from "./2_client_plain.js";
26
- import { getChatListId, } from "./3_types.js";
26
+ import { getChatListId } from "./3_types.js";
27
27
  import { Composer, concat, flatten, skip } from "./4_composer.js";
28
28
  const d = debug("Client");
29
29
  const dGap = debug("Client/recoverUpdateGap");
@@ -517,7 +517,7 @@ export class Client extends ClientAbstract {
517
517
  throw new Error(`${source}: not a bot client`);
518
518
  }
519
519
  }, _Client_fetchState = async function _Client_fetchState(source) {
520
- const state = await this.invoke(new functions.updates.getState());
520
+ const state = await this.api.updates.getState();
521
521
  __classPrivateFieldSet(this, _Client_updateState, state, "f");
522
522
  d("state fetched [%s]", source);
523
523
  }, handleMigrationError)](err) {
@@ -583,7 +583,7 @@ export class Client extends ClientAbstract {
583
583
  if (typeof params === "string") {
584
584
  while (true) {
585
585
  try {
586
- const auth = await this.invoke(new functions.auth.importBotAuthorization({ api_id: this.apiId, api_hash: this.apiHash, bot_auth_token: params, flags: 0 }));
586
+ const auth = await this.api.auth.importBotAuthorization({ api_id: this.apiId, api_hash: this.apiHash, bot_auth_token: params, flags: 0 });
587
587
  __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.auth.Authorization).user.id), "f");
588
588
  await this.storage.setAccountType("bot");
589
589
  break;
@@ -605,7 +605,7 @@ export class Client extends ClientAbstract {
605
605
  return;
606
606
  }
607
607
  if (params instanceof types.auth.ExportedAuthorization) {
608
- await this.invoke(new functions.auth.importAuthorization({ id: params.id, bytes: params.bytes }));
608
+ await this.api.auth.importAuthorization({ id: params.id, bytes: params.bytes });
609
609
  dAuth("authorization imported");
610
610
  return;
611
611
  }
@@ -616,12 +616,12 @@ export class Client extends ClientAbstract {
616
616
  while (true) {
617
617
  try {
618
618
  phone = typeof params.phone === "string" ? params.phone : await params.phone();
619
- const sendCode = () => this.invoke(new functions.auth.sendCode({
619
+ const sendCode = () => this.api.auth.sendCode({
620
620
  phone_number: phone,
621
621
  api_id: this.apiId,
622
622
  api_hash: this.apiHash,
623
623
  settings: new types.CodeSettings(),
624
- })).then((v) => v[as](types.auth.SentCode));
624
+ }).then((v) => v[as](types.auth.SentCode));
625
625
  try {
626
626
  sentCode = await sendCode();
627
627
  }
@@ -651,11 +651,11 @@ export class Client extends ClientAbstract {
651
651
  code: while (true) {
652
652
  const code = typeof params.code === "string" ? params.code : await params.code();
653
653
  try {
654
- const auth = await this.invoke(new functions.auth.signIn({
654
+ const auth = await this.api.auth.signIn({
655
655
  phone_number: phone,
656
656
  phone_code: code,
657
657
  phone_code_hash: sentCode.phone_code_hash,
658
- }));
658
+ });
659
659
  __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.auth.Authorization).user.id), "f");
660
660
  await this.storage.setAccountType("user");
661
661
  dAuth("authorized as user");
@@ -677,14 +677,14 @@ export class Client extends ClientAbstract {
677
677
  throw err;
678
678
  }
679
679
  password: while (true) {
680
- const ap = await this.invoke(new functions.account.getPassword());
680
+ const ap = await this.api.account.getPassword();
681
681
  if (!(ap.current_algo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
682
682
  throw new Error(`Handling ${ap.current_algo?.[name]} not implemented`);
683
683
  }
684
684
  try {
685
685
  const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
686
686
  const input = await checkPassword(password, ap);
687
- const auth = await this.invoke(new functions.auth.checkPassword({ password: input }));
687
+ const auth = await this.api.auth.checkPassword({ password: input });
688
688
  __classPrivateFieldSet(this, _Client_selfId, Number(auth[as](types.auth.Authorization).user.id), "f");
689
689
  await this.storage.setAccountType("user");
690
690
  dAuth("authorized as user");
@@ -732,7 +732,7 @@ export class Client extends ClientAbstract {
732
732
  return this.invoke(function_, true);
733
733
  }
734
734
  async getUserAccessHash(userId) {
735
- const users = await this.invoke(new functions.users.getUsers({ id: [new types.InputUser({ user_id: userId, access_hash: 0n })] }));
735
+ const users = await this.api.users.getUsers({ id: [new types.InputUser({ user_id: userId, access_hash: 0n })] });
736
736
  return users[0]?.[as](types.User).access_hash ?? 0n;
737
737
  }
738
738
  async getInputPeer(id) {
@@ -750,7 +750,7 @@ export class Client extends ClientAbstract {
750
750
  }
751
751
  [(_Client_initConnection = async function _Client_initConnection() {
752
752
  if (!__classPrivateFieldGet(this, _Client_connectionInited, "f")) {
753
- await this.invoke(new functions.initConnection({
753
+ await this.api.initConnection({
754
754
  api_id: this.apiId,
755
755
  app_version: this.appVersion,
756
756
  device_model: this.deviceModel,
@@ -762,7 +762,7 @@ export class Client extends ClientAbstract {
762
762
  }),
763
763
  system_lang_code: this.systemLangCode,
764
764
  system_version: this.systemVersion,
765
- }));
765
+ });
766
766
  __classPrivateFieldSet(this, _Client_connectionInited, true, "f");
767
767
  d("connection inited");
768
768
  }
@@ -905,7 +905,7 @@ export class Client extends ClientAbstract {
905
905
  reject(__classPrivateFieldGet(this, _Client_pingLoopAbortSignal, "f")?.signal.reason);
906
906
  };
907
907
  });
908
- await this.invoke(new functions.ping_delay_disconnect({ ping_id: getRandomId(), disconnect_delay: __classPrivateFieldGet(this, _Client_pingInterval, "f") / 1000 + 15 }));
908
+ await this.api.ping_delay_disconnect({ ping_id: getRandomId(), disconnect_delay: __classPrivateFieldGet(this, _Client_pingInterval, "f") / 1000 + 15 });
909
909
  if (Date.now() - __classPrivateFieldGet(this, _Client_lastUpdates, "f").getTime() >= 15 * 60 * 1000) {
910
910
  drop(__classPrivateFieldGet(this, _Client_instances, "m", _Client_recoverUpdateGap).call(this, "lastUpdates"));
911
911
  }
@@ -915,20 +915,20 @@ export class Client extends ClientAbstract {
915
915
  }
916
916
  }
917
917
  }, _Client_invoke = async function _Client_invoke(function_, noWait) {
918
- if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
919
- if (__classPrivateFieldGet(this, _Client_autoStart, "f") && !__classPrivateFieldGet(this, _Client_autoStarted, "f")) {
920
- await this.start();
921
- }
922
- else {
923
- throw new ConnectionError("Not connected");
924
- }
925
- }
926
- if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
927
- UNREACHABLE();
928
- }
929
918
  let n = 1;
930
919
  while (true) {
931
920
  try {
921
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
922
+ if (__classPrivateFieldGet(this, _Client_autoStart, "f") && !__classPrivateFieldGet(this, _Client_autoStarted, "f")) {
923
+ await this.start();
924
+ }
925
+ else {
926
+ throw new ConnectionError("Not connected");
927
+ }
928
+ }
929
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
930
+ UNREACHABLE();
931
+ }
932
932
  let seqNo = __classPrivateFieldGet(this, _Client_state, "f").seqNo * 2;
933
933
  if (!(function_ instanceof functions.ping) && !(function_ instanceof types.Msgs_ack)) {
934
934
  seqNo++;
@@ -996,6 +996,9 @@ export class Client extends ClientAbstract {
996
996
  else if (chat instanceof types.Chat) {
997
997
  await this.storage.setEntity(chat);
998
998
  }
999
+ else if (chat instanceof types.ChannelForbidden || chat instanceof types.ChatForbidden) {
1000
+ await this.storage.removeEntity(chat);
1001
+ }
999
1002
  }
1000
1003
  }, _Client_processUsers = async function _Client_processUsers(users) {
1001
1004
  for (const user of users) {
@@ -1152,6 +1155,14 @@ export class Client extends ClientAbstract {
1152
1155
  }
1153
1156
  else if (update instanceof types.UpdateUserName) {
1154
1157
  await this.storage.updateUsernames("user", update.user_id, update.usernames.map((v) => v.username));
1158
+ const peer = new types.PeerUser(update);
1159
+ const entity = await this[getEntity](peer);
1160
+ if (entity != null) {
1161
+ entity.usernames = update.usernames;
1162
+ entity.first_name = update.first_name;
1163
+ entity.last_name = update.last_name;
1164
+ await this.storage.setEntity(entity);
1165
+ }
1155
1166
  }
1156
1167
  else if (update instanceof types.UpdatePtsChanged) {
1157
1168
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchState).call(this, "updatePtsChanged");
@@ -1178,7 +1189,7 @@ export class Client extends ClientAbstract {
1178
1189
  }
1179
1190
  }
1180
1191
  /// If there were any Update, they will be passed to the update handling queue.
1181
- if (update instanceof types._Update) {
1192
+ if (update instanceof types._Update || update instanceof types.UpdateShortMessage || update instanceof types.UpdateShortChatMessage || update instanceof types.UpdateShortSentMessage) {
1182
1193
  updatesToHandle.push(update);
1183
1194
  }
1184
1195
  }
@@ -1220,7 +1231,7 @@ export class Client extends ClientAbstract {
1220
1231
  try {
1221
1232
  let state = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
1222
1233
  while (true) {
1223
- const difference = await this.invoke(new functions.updates.getDifference({ pts: state.pts, date: state.date, qts: state.qts ?? 0 }));
1234
+ const difference = await this.api.updates.getDifference({ pts: state.pts, date: state.date, qts: state.qts ?? 0 });
1224
1235
  if (difference instanceof types.updates.Difference || difference instanceof types.updates.DifferenceSlice) {
1225
1236
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, difference.chats);
1226
1237
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, difference.users);
@@ -1269,12 +1280,12 @@ export class Client extends ClientAbstract {
1269
1280
  let pts = pts_ == null ? 1 : pts_;
1270
1281
  while (true) {
1271
1282
  const { access_hash } = await this.getInputPeer(ZERO_CHANNEL_ID + -Number(channelId)).then((v) => v[as](types.InputPeerChannel));
1272
- const difference = await this.invoke(new functions.updates.getChannelDifference({
1283
+ const difference = await this.api.updates.getChannelDifference({
1273
1284
  pts,
1274
1285
  channel: new types.InputChannel({ channel_id: channelId, access_hash }),
1275
1286
  filter: new types.ChannelMessagesFilterEmpty(),
1276
1287
  limit: await this.storage.getAccountType() == "user" ? CHANNEL_DIFFERENCE_LIMIT_USER : CHANNEL_DIFFERENCE_LIMIT_BOT,
1277
- }));
1288
+ });
1278
1289
  if (difference instanceof types.updates.ChannelDifference) {
1279
1290
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, difference.chats);
1280
1291
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, difference.users);
@@ -1311,7 +1322,7 @@ export class Client extends ClientAbstract {
1311
1322
  }
1312
1323
  }
1313
1324
  }, _Client_getChannelAccessHash = async function _Client_getChannelAccessHash(channelId) {
1314
- const channels = await this.invoke(new functions.channels.getChannels({ id: [new types.InputChannel({ channel_id: channelId, access_hash: 0n })] }));
1325
+ const channels = await this.api.channels.getChannels({ id: [new types.InputChannel({ channel_id: channelId, access_hash: 0n })] });
1315
1326
  return channels.chats[0][as](types.Channel).access_hash ?? 0n;
1316
1327
  }, _Client_getInputPeerInner = async function _Client_getInputPeerInner(id) {
1317
1328
  if (typeof id === "string") {
@@ -1329,7 +1340,7 @@ export class Client extends ClientAbstract {
1329
1340
  }
1330
1341
  }
1331
1342
  else {
1332
- const resolved = await this.invoke(new functions.contacts.resolveUsername({ username: id }));
1343
+ const resolved = await this.api.contacts.resolveUsername({ username: id });
1333
1344
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processChats).call(this, resolved.chats);
1334
1345
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_processUsers).call(this, resolved.users);
1335
1346
  if (resolved.peer instanceof types.PeerUser) {
@@ -1451,7 +1462,7 @@ export class Client extends ClientAbstract {
1451
1462
  const replyToMsgId = params?.replyToMessageId;
1452
1463
  const topMsgId = params?.messageThreadId;
1453
1464
  const sendAs = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_resolveSendAs).call(this, params);
1454
- const result = await this.invoke(new functions.messages.sendMessage({
1465
+ const result = await this.api.messages.sendMessage({
1455
1466
  peer,
1456
1467
  random_id: randomId,
1457
1468
  message,
@@ -1462,7 +1473,7 @@ export class Client extends ClientAbstract {
1462
1473
  send_as: sendAs,
1463
1474
  entities,
1464
1475
  reply_markup: replyMarkup,
1465
- }));
1476
+ });
1466
1477
  const message_ = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1467
1478
  return __classPrivateFieldGet(_a, _a, "m", _Client_assertMsgHas).call(_a, message_, "text");
1468
1479
  }
@@ -1477,14 +1488,14 @@ export class Client extends ClientAbstract {
1477
1488
  */
1478
1489
  async editMessageText(chatId, messageId, text, params) {
1479
1490
  const [message, entities] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).call(this, text, params);
1480
- const result = await this.invoke(new functions.messages.editMessage({
1491
+ const result = await this.api.messages.editMessage({
1481
1492
  id: messageId,
1482
1493
  peer: await this.getInputPeer(chatId),
1483
1494
  entities,
1484
1495
  message,
1485
1496
  no_webpage: params?.disableWebPagePreview ? true : undefined,
1486
1497
  reply_markup: await __classPrivateFieldGet(this, _Client_instances, "m", _Client_constructReplyMarkup).call(this, params),
1487
- }));
1498
+ });
1488
1499
  const message_ = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1489
1500
  return __classPrivateFieldGet(_a, _a, "m", _Client_assertMsgHas).call(_a, message_, "text");
1490
1501
  }
@@ -1567,15 +1578,15 @@ export class Client extends ClientAbstract {
1567
1578
  }
1568
1579
  if (shouldFetch) {
1569
1580
  if (peer instanceof types.InputPeerChannel) {
1570
- messages_ = await this.invoke(new functions.channels.getMessages({
1581
+ messages_ = await this.api.channels.getMessages({
1571
1582
  channel: new types.InputChannel(peer),
1572
1583
  id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1573
- })).then((v) => v[as](types.messages.ChannelMessages).messages);
1584
+ }).then((v) => v[as](types.messages.ChannelMessages).messages);
1574
1585
  }
1575
1586
  else {
1576
- messages_ = await this.invoke(new functions.messages.getMessages({
1587
+ messages_ = await this.api.messages.getMessages({
1577
1588
  id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1578
- })).then((v) => v[as](types.messages.Messages).messages);
1589
+ }).then((v) => v[as](types.messages.Messages).messages);
1579
1590
  }
1580
1591
  }
1581
1592
  const messages = new Array();
@@ -1619,14 +1630,17 @@ export class Client extends ClientAbstract {
1619
1630
  * ```
1620
1631
  * @returns A generator yielding the contents of the file.
1621
1632
  */
1622
- async download(fileId, params) {
1633
+ async *download(fileId, params) {
1623
1634
  const fileId_ = FileID.decode(fileId);
1624
1635
  switch (fileId_.fileType) {
1625
1636
  case FileType.ChatPhoto: {
1626
1637
  const big = fileId_.params.thumbnailSource == ThumbnailSource.ChatPhotoBig;
1627
1638
  const peer = await this.getInputPeer(fileId_.params.chatId);
1628
1639
  const location = new types.InputPeerPhotoFileLocation({ big: big ? true : undefined, peer, photo_id: fileId_.params.mediaId });
1629
- return __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params);
1640
+ for await (const chunk of __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params)) {
1641
+ yield chunk;
1642
+ }
1643
+ break;
1630
1644
  }
1631
1645
  case FileType.Photo: {
1632
1646
  if (fileId_.params.mediaId == undefined || fileId_.params.accessHash == undefined || fileId_.params.fileReference == undefined || fileId_.params.thumbnailSize == undefined) {
@@ -1638,7 +1652,26 @@ export class Client extends ClientAbstract {
1638
1652
  file_reference: fileId_.params.fileReference,
1639
1653
  thumb_size: fileId_.params.thumbnailSize,
1640
1654
  });
1641
- return __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params);
1655
+ for await (const chunk of __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params)) {
1656
+ yield chunk;
1657
+ }
1658
+ break;
1659
+ }
1660
+ case FileType.Document:
1661
+ case FileType.Sticker: {
1662
+ if (fileId_.params.mediaId == undefined || fileId_.params.accessHash == undefined || fileId_.params.fileReference == undefined || fileId_.params.thumbnailSize == undefined) {
1663
+ UNREACHABLE();
1664
+ }
1665
+ const location = new types.InputDocumentFileLocation({
1666
+ id: fileId_.params.mediaId,
1667
+ access_hash: fileId_.params.accessHash,
1668
+ file_reference: fileId_.params.fileReference,
1669
+ thumb_size: fileId_.params.thumbnailSize,
1670
+ });
1671
+ for await (const chunk of __classPrivateFieldGet(this, _Client_instances, "m", _Client_downloadInner).call(this, location, fileId_.dcId, params)) {
1672
+ yield chunk;
1673
+ }
1674
+ break;
1642
1675
  }
1643
1676
  default:
1644
1677
  UNREACHABLE();
@@ -1679,7 +1712,7 @@ export class Client extends ClientAbstract {
1679
1712
  client.invoke.use(async (ctx, next) => {
1680
1713
  if (ctx.error instanceof AuthKeyUnregistered) {
1681
1714
  try {
1682
- const exportedAuth = await this.invoke(new functions.auth.exportAuthorization({ dc_id: dcId }));
1715
+ const exportedAuth = await this.api.auth.exportAuthorization({ dc_id: dcId });
1683
1716
  await client.authorize(exportedAuth);
1684
1717
  return true;
1685
1718
  }
@@ -1722,7 +1755,7 @@ export class Client extends ClientAbstract {
1722
1755
  return maybeStickerSetName[0];
1723
1756
  }
1724
1757
  else {
1725
- const stickerSet = await this.invoke(new functions.messages.getStickerSet({ stickerset: inputStickerSet, hash }));
1758
+ const stickerSet = await this.api.messages.getStickerSet({ stickerset: inputStickerSet, hash });
1726
1759
  const name = stickerSet[as](types.messages.StickerSet).set.short_name;
1727
1760
  await this.storage.updateStickerSetName(inputStickerSet.id, inputStickerSet.access_hash, name);
1728
1761
  return name;
@@ -1738,7 +1771,7 @@ export class Client extends ClientAbstract {
1738
1771
  * @returns The forwarded messages.
1739
1772
  */
1740
1773
  async forwardMessages(from, to, messageIds, params) {
1741
- const result = await this.invoke(new functions.messages.forwardMessages({
1774
+ const result = await this.api.messages.forwardMessages({
1742
1775
  from_peer: await this.getInputPeer(from),
1743
1776
  to_peer: await this.getInputPeer(to),
1744
1777
  id: messageIds,
@@ -1749,7 +1782,7 @@ export class Client extends ClientAbstract {
1749
1782
  send_as: params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined,
1750
1783
  drop_author: params?.dropSenderName || undefined,
1751
1784
  drop_media_captions: params?.dropCaption || undefined,
1752
- }));
1785
+ });
1753
1786
  return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, to, result);
1754
1787
  }
1755
1788
  /**
@@ -1770,7 +1803,7 @@ export class Client extends ClientAbstract {
1770
1803
  * @method
1771
1804
  */
1772
1805
  async getMe() {
1773
- const users = await this.invoke(new functions.users.getUsers({ id: [new types.InputUserSelf()] }));
1806
+ const users = await this.api.users.getUsers({ id: [new types.InputUserSelf()] });
1774
1807
  if (users.length < 1) {
1775
1808
  UNREACHABLE();
1776
1809
  }
@@ -1786,12 +1819,12 @@ export class Client extends ClientAbstract {
1786
1819
  */
1787
1820
  async answerCallbackQuery(id, params) {
1788
1821
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "answerCallbackQuery");
1789
- await this.invoke(new functions.messages.setBotCallbackAnswer({
1822
+ await this.api.messages.setBotCallbackAnswer({
1790
1823
  query_id: BigInt(id),
1791
1824
  cache_time: params?.cacheTime ?? 0,
1792
1825
  message: params?.text,
1793
1826
  alert: params?.alert ? true : undefined,
1794
- }));
1827
+ });
1795
1828
  }
1796
1829
  /**
1797
1830
  * Send a poll.
@@ -1833,7 +1866,7 @@ export class Client extends ClientAbstract {
1833
1866
  solution,
1834
1867
  solution_entities: solutionEntities,
1835
1868
  });
1836
- const result = await this.invoke(new functions.messages.sendMedia({
1869
+ const result = await this.api.messages.sendMedia({
1837
1870
  peer,
1838
1871
  random_id: randomId,
1839
1872
  silent,
@@ -1843,7 +1876,7 @@ export class Client extends ClientAbstract {
1843
1876
  send_as: sendAs,
1844
1877
  media,
1845
1878
  message: "",
1846
- }));
1879
+ });
1847
1880
  const message = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
1848
1881
  return __classPrivateFieldGet(_a, _a, "m", _Client_assertMsgHas).call(_a, message, "poll");
1849
1882
  }
@@ -1894,7 +1927,7 @@ export class Client extends ClientAbstract {
1894
1927
  default:
1895
1928
  throw new Error("Invalid chat action: " + action);
1896
1929
  }
1897
- await this.invoke(new functions.messages.setTyping({ peer: await this.getInputPeer(chatId), action: action_, top_msg_id: params?.messageThreadId }));
1930
+ await this.api.messages.setTyping({ peer: await this.getInputPeer(chatId), action: action_, top_msg_id: params?.messageThreadId });
1898
1931
  }
1899
1932
  /**
1900
1933
  * Upload a file.
@@ -1994,20 +2027,20 @@ export class Client extends ClientAbstract {
1994
2027
  * @param commands The commands to set.
1995
2028
  */
1996
2029
  async setMyCommands(commands, params) {
1997
- await this.invoke(new functions.bots.setBotCommands({
2030
+ await this.api.bots.setBotCommands({
1998
2031
  commands: commands.map((v) => new types.BotCommand(v)),
1999
2032
  lang_code: params?.languageCode ?? "",
2000
2033
  scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" }, this.getInputPeer.bind(this)),
2001
- }));
2034
+ });
2002
2035
  }
2003
2036
  /**
2004
2037
  * Get the bot's commands in the given scope and/or language. Bot-only.
2005
2038
  */
2006
2039
  async getMyCommands(params) {
2007
- const commands_ = await this.invoke(new functions.bots.getBotCommands({
2040
+ const commands_ = await this.api.bots.getBotCommands({
2008
2041
  lang_code: params?.languageCode ?? "",
2009
2042
  scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" }, this.getInputPeer.bind(this)),
2010
- }));
2043
+ });
2011
2044
  return commands_.map((v) => ({ command: v.command, description: v.description }));
2012
2045
  }
2013
2046
  /**
@@ -2018,7 +2051,7 @@ export class Client extends ClientAbstract {
2018
2051
  * @param results The results to answer with.
2019
2052
  */
2020
2053
  async answerInlineQuery(id, results, params) {
2021
- await this.invoke(new functions.messages.setInlineBotResults({
2054
+ await this.api.messages.setInlineBotResults({
2022
2055
  query_id: BigInt(id),
2023
2056
  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)))),
2024
2057
  cache_time: params?.cacheTime ?? 300,
@@ -2027,7 +2060,7 @@ export class Client extends ClientAbstract {
2027
2060
  switch_pm: params?.button && params.button.startParameter ? new types.InlineBotSwitchPM({ text: params.button.text, start_param: params.button.startParameter }) : undefined,
2028
2061
  gallery: params?.isGallery ? true : undefined,
2029
2062
  next_offset: params?.nextOffset,
2030
- }));
2063
+ });
2031
2064
  }
2032
2065
  use(...middleware) {
2033
2066
  const composer = new Composer(...middleware);
@@ -2179,10 +2212,10 @@ export class Client extends ClientAbstract {
2179
2212
  async deleteMessages(chatId, messageIds, params) {
2180
2213
  const peer = await this.getInputPeer(chatId);
2181
2214
  if (peer instanceof types.InputPeerChannel) {
2182
- await this.invoke(new functions.channels.deleteMessages({ channel: new types.InputChannel(peer), id: messageIds }));
2215
+ await this.api.channels.deleteMessages({ channel: new types.InputChannel(peer), id: messageIds });
2183
2216
  }
2184
2217
  else {
2185
- await this.invoke(new functions.messages.deleteMessages({ id: messageIds, revoke: params?.onlyForMe ? undefined : true }));
2218
+ await this.api.messages.deleteMessages({ id: messageIds, revoke: params?.onlyForMe ? undefined : true });
2186
2219
  }
2187
2220
  }
2188
2221
  /**
@@ -2289,7 +2322,7 @@ export class Client extends ClientAbstract {
2289
2322
  return { messages, cdn };
2290
2323
  }
2291
2324
  /**
2292
- * Get chats.
2325
+ * Get chats from a chat list.
2293
2326
  *
2294
2327
  * @method
2295
2328
  */
@@ -2321,6 +2354,76 @@ export class Client extends ClientAbstract {
2321
2354
  chats = chats.slice(0, limit);
2322
2355
  return chats;
2323
2356
  }
2357
+ /**
2358
+ * Get a chat.
2359
+ *
2360
+ * @method
2361
+ */
2362
+ async getChat(chatId) {
2363
+ if (await this.storage.getAccountType() == "user") {
2364
+ let maybeChatId = null;
2365
+ if (typeof chatId === "number") {
2366
+ maybeChatId = chatId;
2367
+ }
2368
+ else if (typeof chatId === "string") {
2369
+ maybeChatId = __classPrivateFieldGet(this, _Client_instances, "m", _Client_tryGetChatId).call(this, getUsername(chatId));
2370
+ }
2371
+ else {
2372
+ UNREACHABLE();
2373
+ }
2374
+ if (maybeChatId != null) {
2375
+ const [chat] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatAnywhere).call(this, maybeChatId);
2376
+ if (chat !== undefined) {
2377
+ return chat;
2378
+ }
2379
+ }
2380
+ }
2381
+ let inputPeer = null;
2382
+ if (typeof chatId === "number") {
2383
+ const chat = await constructChat3(chatId, -1, undefined, this[getEntity].bind(this));
2384
+ if (chat != null) {
2385
+ return chat;
2386
+ }
2387
+ }
2388
+ else {
2389
+ inputPeer = await this.getInputPeer(chatId);
2390
+ const chatId_ = peerToChatId(inputPeer);
2391
+ const chat = await constructChat3(chatId_, -1, undefined, this[getEntity].bind(this));
2392
+ if (chat != null) {
2393
+ return chat;
2394
+ }
2395
+ }
2396
+ if (inputPeer == null) {
2397
+ inputPeer = await this.getInputPeer(chatId);
2398
+ }
2399
+ if (inputPeer instanceof types.InputPeerChat) {
2400
+ const chats = await this.api.messages.getChats({ id: [inputPeer.chat_id] }).then((v) => v[as](types.messages.Chats));
2401
+ const chat = chats.chats[0];
2402
+ if (chat instanceof types.ChatEmpty) {
2403
+ UNREACHABLE();
2404
+ }
2405
+ return constructChat2(chat, -1, undefined);
2406
+ }
2407
+ else if (inputPeer instanceof types.InputPeerChannel) {
2408
+ const channels = await this.api.channels.getChannels({ id: [new types.InputChannel(inputPeer)] });
2409
+ const channel = channels.chats[0];
2410
+ if (channel instanceof types.ChatEmpty) {
2411
+ UNREACHABLE();
2412
+ }
2413
+ return constructChat2(channel, -1, undefined);
2414
+ }
2415
+ else if (inputPeer instanceof types.InputPeerUser) {
2416
+ const users = await this.api.users.getUsers({ id: [new types.InputUser(inputPeer)] });
2417
+ const user = users[0];
2418
+ if (user instanceof types.UserEmpty) {
2419
+ UNREACHABLE();
2420
+ }
2421
+ return constructChat2(user, -1, undefined);
2422
+ }
2423
+ else {
2424
+ UNREACHABLE();
2425
+ }
2426
+ }
2324
2427
  /**
2325
2428
  * Get chat history.
2326
2429
  *
@@ -2347,7 +2450,9 @@ export class Client extends ClientAbstract {
2347
2450
  }
2348
2451
  if (messages.length < limit) {
2349
2452
  d("have only %d messages but need %d more", messages.length, limit - messages.length);
2350
- offsetId = messages[messages.length - 1].id; // TODO: track id of oldest message and don't send requests for it
2453
+ if (messages.length > 0) {
2454
+ offsetId = messages[messages.length - 1].id; // TODO: track id of oldest message and don't send requests for it
2455
+ }
2351
2456
  const result = await this.api.messages.getHistory({
2352
2457
  peer: peer,
2353
2458
  offset_id: offsetId,
@@ -2368,6 +2473,53 @@ export class Client extends ClientAbstract {
2368
2473
  }
2369
2474
  return messages;
2370
2475
  }
2476
+ /**
2477
+ * Get custom emoji documents for download.
2478
+ *
2479
+ * @param id Identifier of one or more of custom emojis.
2480
+ * @method
2481
+ */
2482
+ async getCustomEmojiDocuments(id) {
2483
+ id = Array.isArray(id) ? id : [id];
2484
+ if (!id.length) {
2485
+ throw new Error("No custom emoji ID provided");
2486
+ }
2487
+ const documents = new Array();
2488
+ let shouldFetch = false;
2489
+ for (const [i, id_] of id.entries()) {
2490
+ const maybeDocument = await this.storage.getCustomEmojiDocument(BigInt(id_));
2491
+ if (maybeDocument != null && Date.now() - maybeDocument[1].getTime() <= 30 * 60 * 1000) {
2492
+ const document_ = maybeDocument[0];
2493
+ const fileUniqueId = new FileUniqueID(FileUniqueType.Document, { mediaId: document_.id }).encode();
2494
+ const fileId = new FileID(null, null, FileType.Document, document_.dc_id, {
2495
+ mediaId: document_.id,
2496
+ accessHash: document_.access_hash,
2497
+ fileReference: document_.file_reference,
2498
+ }).encode();
2499
+ const document = constructDocument(document_, new types.DocumentAttributeFilename({ file_name: `${id[i] ?? "customEmoji"}.${extension(document_.mime_type)}` }), fileId, fileUniqueId);
2500
+ documents.push(document);
2501
+ }
2502
+ else {
2503
+ shouldFetch = true;
2504
+ break;
2505
+ }
2506
+ }
2507
+ if (!shouldFetch) {
2508
+ return documents;
2509
+ }
2510
+ const documents_ = await this.api.messages.getCustomEmojiDocuments({ document_id: id.map(BigInt) }).then((v) => v.map((v) => v[as](types.Document)));
2511
+ for (const [i, document_] of documents_.entries()) {
2512
+ const fileUniqueId = new FileUniqueID(FileUniqueType.Document, { mediaId: document_.id }).encode();
2513
+ const fileId = new FileID(null, null, FileType.Document, document_.dc_id, {
2514
+ mediaId: document_.id,
2515
+ accessHash: document_.access_hash,
2516
+ fileReference: document_.file_reference,
2517
+ }).encode();
2518
+ const document = constructDocument(document_, new types.DocumentAttributeFilename({ file_name: `${id[i] ?? "customEmoji"}.${extension(document_.mime_type)}` }), fileId, fileUniqueId);
2519
+ documents.push(document);
2520
+ }
2521
+ return documents;
2522
+ }
2371
2523
  }
2372
2524
  _a = Client, _Client_getMe = async function _Client_getMe() {
2373
2525
  if (__classPrivateFieldGet(this, _Client_lastGetMe, "f") != null) {
@@ -2390,7 +2542,7 @@ async function _Client_handleUpdate(update) {
2390
2542
  silent: update.silent,
2391
2543
  id: update.id,
2392
2544
  from_id: update.out ? new types.PeerUser({ user_id: await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getSelfId).call(this).then(BigInt) }) : new types.PeerUser({ user_id: update.user_id }),
2393
- peer_id: new types.PeerChat({ chat_id: update.user_id }),
2545
+ peer_id: new types.PeerUser({ user_id: update.user_id }),
2394
2546
  message: update.message,
2395
2547
  date: update.date,
2396
2548
  fwd_from: update.fwd_from,
@@ -2493,6 +2645,39 @@ async function _Client_handleUpdate(update) {
2493
2645
  if (update instanceof types.UpdatePinnedDialogs) {
2494
2646
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatePinnedChats).call(this, update);
2495
2647
  }
2648
+ if (update instanceof types.UpdateChannel) {
2649
+ const peer = new types.PeerChannel(update);
2650
+ const channel = await this[getEntity](peer);
2651
+ if (channel != null && "left" in channel && channel.left) {
2652
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_removeChat).call(this, peerToChatId(peer));
2653
+ }
2654
+ else if (channel instanceof types.ChannelForbidden) {
2655
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_removeChat).call(this, peerToChatId(peer));
2656
+ }
2657
+ else if (channel instanceof types.Channel) {
2658
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updateOrAddChat).call(this, peerToChatId(peer));
2659
+ }
2660
+ }
2661
+ else if (update instanceof types.UpdateChat) { // TODO: handle deactivated (migration)
2662
+ const peer = new types.PeerChat(update);
2663
+ const chat = await this[getEntity](peer);
2664
+ if (chat != null && "left" in chat && chat.left) {
2665
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_removeChat).call(this, peerToChatId(peer));
2666
+ }
2667
+ else if (chat instanceof types.ChatForbidden) {
2668
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_removeChat).call(this, peerToChatId(peer));
2669
+ }
2670
+ else if (chat instanceof types.Chat) {
2671
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updateOrAddChat).call(this, peerToChatId(peer));
2672
+ }
2673
+ }
2674
+ else if (update instanceof types.UpdateUser || update instanceof types.UpdateUserName) {
2675
+ const peer = new types.PeerUser(update);
2676
+ const chat = await this[getEntity](peer);
2677
+ if (chat != null) {
2678
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updateOrAddChat).call(this, peerToChatId(peer));
2679
+ }
2680
+ }
2496
2681
  }, _Client_constructReplyMarkup = async function _Client_constructReplyMarkup(params) {
2497
2682
  if (params?.replyMarkup) {
2498
2683
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertBot).call(this, "replyMarkup");
@@ -2506,9 +2691,9 @@ async function _Client_handleUpdate(update) {
2506
2691
  }, _Client_setMyInfo =
2507
2692
  //#endregion
2508
2693
  async function _Client_setMyInfo(info) {
2509
- await this.invoke(new functions.bots.setBotInfo({ bot: new types.InputUserSelf(), ...info }));
2694
+ await this.api.bots.setBotInfo({ bot: new types.InputUserSelf(), ...info });
2510
2695
  }, _Client_getMyInfo = function _Client_getMyInfo(languageCode) {
2511
- return this.invoke(new functions.bots.getBotInfo({ bot: new types.InputUserSelf(), lang_code: languageCode ?? "" }));
2696
+ return this.api.bots.getBotInfo({ bot: new types.InputUserSelf(), lang_code: languageCode ?? "" });
2512
2697
  }, _Client_resolveFileId = function _Client_resolveFileId(maybeFileId, expectedFileType) {
2513
2698
  let fileId = null;
2514
2699
  try {
@@ -2544,7 +2729,7 @@ async function _Client_setMyInfo(info) {
2544
2729
  const parseResult = caption_ !== undefined ? __classPrivateFieldGet(this, _Client_instances, "m", _Client_parseText).call(this, caption_, { parseMode: params?.parseMode, entities: params?.captionEntities }) : undefined;
2545
2730
  const caption = parseResult === undefined ? undefined : parseResult[0];
2546
2731
  const captionEntities = parseResult === undefined ? undefined : parseResult[1];
2547
- const result = await this.invoke(new functions.messages.sendMedia({
2732
+ const result = await this.api.messages.sendMedia({
2548
2733
  peer,
2549
2734
  random_id: randomId,
2550
2735
  silent,
@@ -2555,7 +2740,7 @@ async function _Client_setMyInfo(info) {
2555
2740
  media,
2556
2741
  message: caption ?? "",
2557
2742
  entities: captionEntities,
2558
- }));
2743
+ });
2559
2744
  return await __classPrivateFieldGet(this, _Client_instances, "m", _Client_updatesToMessages).call(this, chatId, result).then((v) => v[0]);
2560
2745
  }, _Client_sendChatUpdate = async function _Client_sendChatUpdate(chatId, added) {
2561
2746
  try {
@@ -2564,12 +2749,12 @@ async function _Client_setMyInfo(info) {
2564
2749
  catch {
2565
2750
  return;
2566
2751
  }
2567
- const chat = __classPrivateFieldGet(this, _Client_chats, "f").get(chatId);
2752
+ const [chat] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatAnywhere).call(this, chatId);
2568
2753
  const update = chat === undefined ? { deletedChat: { chatId } } : added ? { newChat: chat } : { editedChat: chat };
2569
2754
  __classPrivateFieldGet(this, _Client_handleUpdateQueue, "f").add(async () => {
2570
2755
  await __classPrivateFieldGet(this, _Client_handle, "f").call(this, await __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, update), resolve);
2571
2756
  });
2572
- }, _Client_reassignChatLastMessage = async function _Client_reassignChatLastMessage(chatId, add = false) {
2757
+ }, _Client_reassignChatLastMessage = async function _Client_reassignChatLastMessage(chatId, add = false, sendUpdate = true) {
2573
2758
  try {
2574
2759
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_assertUser).call(this, "");
2575
2760
  }
@@ -2590,14 +2775,16 @@ async function _Client_setMyInfo(info) {
2590
2775
  }
2591
2776
  else {
2592
2777
  const pinnedChats = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getPinnedChats).call(this, listId);
2593
- const chat = await constructChat2(chatId, pinnedChats.indexOf(chatId), message, this[getEntity].bind(this));
2778
+ const chat = await constructChat3(chatId, pinnedChats.indexOf(chatId), message, this[getEntity].bind(this));
2594
2779
  if (chat == null) {
2595
2780
  UNREACHABLE();
2596
2781
  }
2597
2782
  __classPrivateFieldGet(this, _Client_chats, "f").set(chatId, chat);
2598
2783
  await this.storage.setChat(listId, chatId, chat.pinned, chat.lastMessage?.id ?? 0, chat.lastMessage?.date ?? new Date(0));
2599
2784
  }
2600
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, !chat);
2785
+ if (sendUpdate) {
2786
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, !chat);
2787
+ }
2601
2788
  return;
2602
2789
  }
2603
2790
  const message = await this.getHistory(chatId, { limit: 1 }).then((v) => v[0]);
@@ -2609,20 +2796,41 @@ async function _Client_setMyInfo(info) {
2609
2796
  }
2610
2797
  else {
2611
2798
  const pinnedChats = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getPinnedChats).call(this, listId);
2612
- const chat = await constructChat2(chatId, pinnedChats.indexOf(chatId), message, this[getEntity].bind(this));
2799
+ const chat = await constructChat3(chatId, pinnedChats.indexOf(chatId), message, this[getEntity].bind(this));
2613
2800
  if (chat == null) {
2614
2801
  UNREACHABLE();
2615
2802
  }
2616
2803
  __classPrivateFieldGet(this, _Client_chats, "f").set(chatId, chat);
2617
2804
  }
2618
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, !chat);
2805
+ if (sendUpdate) {
2806
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, !chat);
2807
+ }
2619
2808
  return;
2620
2809
  }
2621
2810
  if (chat) {
2622
2811
  chat.order = getChatOrder(undefined, chat.pinned);
2623
2812
  chat.lastMessage = undefined;
2624
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, false);
2813
+ if (sendUpdate) {
2814
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, false);
2815
+ }
2816
+ }
2817
+ }, _Client_tryGetChatId = function _Client_tryGetChatId(username) {
2818
+ username = username.toLowerCase();
2819
+ for (const chat of __classPrivateFieldGet(this, _Client_chats, "f").values()) {
2820
+ if ("username" in chat) {
2821
+ if (chat.username === username || chat.also?.some((v) => v.toLowerCase() === username)) {
2822
+ return chat.id;
2823
+ }
2824
+ }
2825
+ }
2826
+ for (const chat of __classPrivateFieldGet(this, _Client_archivedChats, "f").values()) {
2827
+ if ("username" in chat) {
2828
+ if (chat.username === username || chat.also?.some((v) => v.toLowerCase() === username)) {
2829
+ return chat.id;
2830
+ }
2831
+ }
2625
2832
  }
2833
+ return null;
2626
2834
  }, _Client_getChatAnywhere = function _Client_getChatAnywhere(chatId) {
2627
2835
  let chat = __classPrivateFieldGet(this, _Client_chats, "f").get(chatId);
2628
2836
  if (chat) {
@@ -2646,14 +2854,14 @@ async function _Client_setMyInfo(info) {
2646
2854
  const chats = await this.storage.getChats(0);
2647
2855
  const archivedChats = await this.storage.getChats(1);
2648
2856
  for (const { chatId, pinned, topMessageId } of chats) {
2649
- const chat = await constructChat3(chatId, pinned, topMessageId, this[getEntity].bind(this), this.getMessage.bind(this));
2857
+ const chat = await constructChat4(chatId, pinned, topMessageId, this[getEntity].bind(this), this.getMessage.bind(this));
2650
2858
  if (chat == null) {
2651
2859
  continue;
2652
2860
  }
2653
2861
  __classPrivateFieldGet(this, _Client_chats, "f").set(chat.id, chat);
2654
2862
  }
2655
2863
  for (const { chatId, pinned, topMessageId } of archivedChats) {
2656
- const chat = await constructChat3(chatId, pinned, topMessageId, this[getEntity].bind(this), this.getMessage.bind(this));
2864
+ const chat = await constructChat4(chatId, pinned, topMessageId, this[getEntity].bind(this), this.getMessage.bind(this));
2657
2865
  if (chat == null) {
2658
2866
  continue;
2659
2867
  }
@@ -2714,6 +2922,29 @@ async function _Client_setMyInfo(info) {
2714
2922
  default:
2715
2923
  UNREACHABLE();
2716
2924
  }
2925
+ }, _Client_updateOrAddChat = async function _Client_updateOrAddChat(chatId) {
2926
+ const [chat, listId] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatAnywhere).call(this, chatId);
2927
+ if (chat !== undefined) {
2928
+ const newChat = await constructChat3(chatId, chat.pinned, chat.lastMessage, this[getEntity].bind(this));
2929
+ if (newChat != null) {
2930
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatList).call(this, listId).set(chatId, newChat);
2931
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, false);
2932
+ }
2933
+ }
2934
+ else {
2935
+ const chat = await constructChat4(chatId, -1, -1, this[getEntity].bind(this), this.getMessage.bind(this));
2936
+ if (chat != null) {
2937
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatList).call(this, 0).set(chatId, chat);
2938
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_reassignChatLastMessage).call(this, chatId, false, false);
2939
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, true);
2940
+ }
2941
+ }
2942
+ }, _Client_removeChat = async function _Client_removeChat(chatId) {
2943
+ const [chat, listId] = __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatAnywhere).call(this, chatId);
2944
+ if (chat !== undefined) {
2945
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_getChatList).call(this, listId).delete(chatId);
2946
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_sendChatUpdate).call(this, chatId, false);
2947
+ }
2717
2948
  }, _Client_updatePinnedChats = async function _Client_updatePinnedChats(update) {
2718
2949
  const listId = update.folder_id ?? 0;
2719
2950
  await __classPrivateFieldGet(this, _Client_instances, "m", _Client_fetchPinnedChats).call(this, update.folder_id);