@mtcute/core 0.20.4 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/client.cjs +4 -0
  2. package/client.js +4 -0
  3. package/highlevel/client.d.cts +17 -10
  4. package/highlevel/client.d.ts +17 -10
  5. package/highlevel/methods/chats/get-peer.cjs +19 -0
  6. package/highlevel/methods/chats/get-peer.d.cts +4 -0
  7. package/highlevel/methods/chats/get-peer.d.ts +4 -0
  8. package/highlevel/methods/chats/get-peer.js +14 -0
  9. package/highlevel/methods/premium/send-star-gift.cjs +1 -1
  10. package/highlevel/methods/premium/send-star-gift.d.cts +2 -2
  11. package/highlevel/methods/premium/send-star-gift.d.ts +2 -2
  12. package/highlevel/methods/premium/send-star-gift.js +1 -1
  13. package/highlevel/methods/premium/transfer-star-gift.cjs +1 -1
  14. package/highlevel/methods/premium/transfer-star-gift.d.cts +2 -2
  15. package/highlevel/methods/premium/transfer-star-gift.d.ts +2 -2
  16. package/highlevel/methods/premium/transfer-star-gift.js +1 -1
  17. package/highlevel/methods/premium/upgrade-star-gift.cjs +1 -1
  18. package/highlevel/methods/premium/upgrade-star-gift.d.cts +2 -2
  19. package/highlevel/methods/premium/upgrade-star-gift.d.ts +2 -2
  20. package/highlevel/methods/premium/upgrade-star-gift.js +1 -1
  21. package/highlevel/methods.d.cts +1 -0
  22. package/highlevel/methods.d.ts +1 -0
  23. package/highlevel/types/misc/takeout-session.cjs +18 -21
  24. package/highlevel/types/misc/takeout-session.d.cts +16 -11
  25. package/highlevel/types/misc/takeout-session.d.ts +16 -11
  26. package/highlevel/types/misc/takeout-session.js +18 -21
  27. package/index.cjs +1 -0
  28. package/index.js +2 -1
  29. package/methods.cjs +2 -0
  30. package/methods.js +2 -0
  31. package/network/network-manager.cjs +1 -1
  32. package/network/network-manager.js +1 -1
  33. package/package.json +1 -1
  34. package/utils/peer-utils.cjs +44 -0
  35. package/utils/peer-utils.d.cts +2 -0
  36. package/utils/peer-utils.d.ts +2 -0
  37. package/utils/peer-utils.js +44 -0
  38. package/utils.cjs +1 -0
  39. package/utils.js +2 -1
package/client.cjs CHANGED
@@ -58,6 +58,7 @@ const getChat = require("./highlevel/methods/chats/get-chat.cjs");
58
58
  const getFullChat = require("./highlevel/methods/chats/get-full-chat.cjs");
59
59
  const getFullUser = require("./highlevel/methods/chats/get-full-user.cjs");
60
60
  const getNearbyChats = require("./highlevel/methods/chats/get-nearby-chats.cjs");
61
+ const getPeer = require("./highlevel/methods/chats/get-peer.cjs");
61
62
  const getSimilarChannels = require("./highlevel/methods/chats/get-similar-channels.cjs");
62
63
  const getUser = require("./highlevel/methods/chats/get-user.cjs");
63
64
  const iterChatEventLog = require("./highlevel/methods/chats/iter-chat-event-log.cjs");
@@ -589,6 +590,9 @@ TelegramClient.prototype.getFullUser = function(...args) {
589
590
  TelegramClient.prototype.getNearbyChats = function(...args) {
590
591
  return getNearbyChats.getNearbyChats(this._client, ...args);
591
592
  };
593
+ TelegramClient.prototype.getPeer = function(...args) {
594
+ return getPeer.getPeer(this._client, ...args);
595
+ };
592
596
  TelegramClient.prototype.getSimilarChannels = function(...args) {
593
597
  return getSimilarChannels.getSimilarChannels(this._client, ...args);
594
598
  };
package/client.js CHANGED
@@ -51,6 +51,7 @@ import { getChat } from "./highlevel/methods/chats/get-chat.js";
51
51
  import { getFullChat } from "./highlevel/methods/chats/get-full-chat.js";
52
52
  import { getFullUser } from "./highlevel/methods/chats/get-full-user.js";
53
53
  import { getNearbyChats } from "./highlevel/methods/chats/get-nearby-chats.js";
54
+ import { getPeer } from "./highlevel/methods/chats/get-peer.js";
54
55
  import { getSimilarChannels } from "./highlevel/methods/chats/get-similar-channels.js";
55
56
  import { getUser } from "./highlevel/methods/chats/get-user.js";
56
57
  import { iterChatEventLog } from "./highlevel/methods/chats/iter-chat-event-log.js";
@@ -582,6 +583,9 @@ TelegramClient.prototype.getFullUser = function(...args) {
582
583
  TelegramClient.prototype.getNearbyChats = function(...args) {
583
584
  return getNearbyChats(this._client, ...args);
584
585
  };
586
+ TelegramClient.prototype.getPeer = function(...args) {
587
+ return getPeer(this._client, ...args);
588
+ };
585
589
  TelegramClient.prototype.getSimilarChannels = function(...args) {
586
590
  return getSimilarChannels(this._client, ...args);
587
591
  };
@@ -23,7 +23,7 @@ import { QuoteParamsFrom } from './methods/messages/send-quote.js';
23
23
  import { CanApplyBoostResult } from './methods/premium/can-apply-boost.js';
24
24
  import { CanSendStoryResult } from './methods/stories/can-send-story.js';
25
25
  import { ITelegramStorageProvider } from './storage/provider.js';
26
- import { AllStories, ArrayPaginated, ArrayWithTotal, Boost, BoostSlot, BoostStats, BotChatJoinRequestUpdate, BotCommands, BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, BusinessCallbackQuery, BusinessChatLink, BusinessConnection, BusinessMessage, BusinessWorkHoursDay, CallbackQuery, Chat, ChatEvent, ChatInviteLink, ChatInviteLinkMember, ChatJoinRequestUpdate, ChatlistPreview, ChatMember, ChatMemberUpdate, ChatPreview, ChosenInlineResult, CollectibleInfo, DeleteBusinessMessageUpdate, DeleteMessageUpdate, DeleteStoryUpdate, Dialog, FactCheck, FileDownloadLocation, FileDownloadParameters, ForumTopic, FullChat, FullUser, GameHighScore, HistoryReadUpdate, InlineCallbackQuery, InlineQuery, InputChatEventFilters, InputDialogFolder, InputFileLike, InputInlineResult, InputMediaLike, InputMediaSticker, InputMessageId, InputPeerLike, InputPrivacyRule, InputReaction, InputStarGift, InputStickerSet, InputStickerSetItem, InputText, InputWebview, MaybeDynamic, Message, MessageEffect, MessageMedia, MessageReactions, ParametersSkip2, ParsedUpdate, PeerReaction, PeerStories, Photo, Poll, PollUpdate, PollVoteUpdate, PreCheckoutQuery, RawDocument, ReplyMarkup, SavedStarGift, SentCode, StarGift, StarGiftUnique, StarsStatus, StarsTransaction, Sticker, StickerSet, StickerType, StoriesStealthMode, Story, StoryInteractions, StoryUpdate, StoryViewer, StoryViewersList, TakeoutSession, TextWithEntities, TypingStatus, UploadedFile, UploadFileLike, User, UserStarGift, UserStatusUpdate, UserTypingUpdate, WebviewResult } from './types/index.js';
26
+ import { AllStories, ArrayPaginated, ArrayWithTotal, Boost, BoostSlot, BoostStats, BotChatJoinRequestUpdate, BotCommands, BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, BusinessCallbackQuery, BusinessChatLink, BusinessConnection, BusinessMessage, BusinessWorkHoursDay, CallbackQuery, Chat, ChatEvent, ChatInviteLink, ChatInviteLinkMember, ChatJoinRequestUpdate, ChatlistPreview, ChatMember, ChatMemberUpdate, ChatPreview, ChosenInlineResult, CollectibleInfo, DeleteBusinessMessageUpdate, DeleteMessageUpdate, DeleteStoryUpdate, Dialog, FactCheck, FileDownloadLocation, FileDownloadParameters, ForumTopic, FullChat, FullUser, GameHighScore, HistoryReadUpdate, InlineCallbackQuery, InlineQuery, InputChatEventFilters, InputDialogFolder, InputFileLike, InputInlineResult, InputMediaLike, InputMediaSticker, InputMessageId, InputPeerLike, InputPrivacyRule, InputReaction, InputStarGift, InputStickerSet, InputStickerSetItem, InputText, InputWebview, MaybeDynamic, Message, MessageEffect, MessageMedia, MessageReactions, ParametersSkip2, ParsedUpdate, Peer, PeerReaction, PeerStories, Photo, Poll, PollUpdate, PollVoteUpdate, PreCheckoutQuery, RawDocument, ReplyMarkup, SavedStarGift, SentCode, StarGift, StarGiftUnique, StarsStatus, StarsTransaction, Sticker, StickerSet, StickerType, StoriesStealthMode, Story, StoryInteractions, StoryUpdate, StoryViewer, StoryViewersList, TakeoutSession, TextWithEntities, TypingStatus, UploadedFile, UploadFileLike, User, UserStarGift, UserStatusUpdate, UserTypingUpdate, WebviewResult } from './types/index.js';
27
27
  import { ParsedUpdateHandlerParams } from './updates/parsed.js';
28
28
  import { RawUpdateInfo } from './updates/types.js';
29
29
  import { InputStringSessionData } from './utils/string-session.js';
@@ -1298,6 +1298,8 @@ export interface TelegramClient extends ITelegramClient {
1298
1298
  * @param longitude Longitude of the location
1299
1299
  */
1300
1300
  getNearbyChats(latitude: number, longitude: number): Promise<Chat[]>;
1301
+ /** Shorthand for {@link getChat} and {@link getUser} depending on the type of the peer */
1302
+ getPeer(peer: InputPeerLike): Promise<Peer>;
1301
1303
  /**
1302
1304
  * Get channels that are similar to a given channel
1303
1305
  *
@@ -3942,7 +3944,11 @@ export interface TelegramClient extends ITelegramClient {
3942
3944
  getMyBoostSlots(): Promise<BoostSlot[]>;
3943
3945
  /** Get one or more saved star gifts by their IDs */
3944
3946
  getSavedStarGiftsById(gifts: MaybeArray<InputStarGift>): Promise<SavedStarGift[]>;
3945
- /** Get a list of saved star gifts of a user */
3947
+ /**
3948
+ * Get a list of saved star gifts of a user/channel
3949
+ *
3950
+ * Note that filters currently only work for channels
3951
+ */
3946
3952
  getSavedStarGifts(params: {
3947
3953
  /** Peer to get saved gifts from */
3948
3954
  owner: InputPeerLike;
@@ -3978,9 +3984,10 @@ export interface TelegramClient extends ITelegramClient {
3978
3984
  /**
3979
3985
  * Get a list of gifts sent to a user.
3980
3986
  *
3981
- * **Available**: 👤 users only
3987
+ * **Available**: both users and bots
3982
3988
  *
3983
3989
  * @param userId User whose gifts to fetch
3990
+ * @deprecated Use {@link getSavedStarGifts} instead
3984
3991
  * @returns Gifts sent to the user
3985
3992
  */
3986
3993
  getStarGifts(userId: InputPeerLike, params?: {
@@ -3994,7 +4001,7 @@ export interface TelegramClient extends ITelegramClient {
3994
4001
  * @default 100
3995
4002
  */
3996
4003
  limit?: number;
3997
- }): Promise<ArrayPaginated<UserStarGift, string>>;
4004
+ }): Promise<ArrayPaginated<SavedStarGift, string>>;
3998
4005
  /**
3999
4006
  * Get Telegram Stars transactions for a given peer.
4000
4007
  *
@@ -4141,7 +4148,7 @@ export interface TelegramClient extends ITelegramClient {
4141
4148
  *
4142
4149
  * **Available**: 👤 users only
4143
4150
  *
4144
- * @returns Service message about the sent gift
4151
+ * @returns Service message about the sent gift, if one was generated.
4145
4152
  */
4146
4153
  sendStarGift(params: {
4147
4154
  /** ID of the peer to send the gift to */
@@ -4163,7 +4170,7 @@ export interface TelegramClient extends ITelegramClient {
4163
4170
  * to the client's update handler.
4164
4171
  */
4165
4172
  shouldDispatch?: true;
4166
- }): Promise<Message>;
4173
+ }): Promise<Message | null>;
4167
4174
  /**
4168
4175
  * Set current user's business introduction.
4169
4176
  *
@@ -4212,7 +4219,7 @@ export interface TelegramClient extends ITelegramClient {
4212
4219
  *
4213
4220
  * **Available**: 👤 users only
4214
4221
  *
4215
- * @returns Service message about the transferred gift
4222
+ * @returns Service message about the transferred gift, if one was generated.
4216
4223
  */
4217
4224
  transferStarGift(params: {
4218
4225
  /** Star gift to transfer */
@@ -4224,7 +4231,7 @@ export interface TelegramClient extends ITelegramClient {
4224
4231
  * to the client's update handler.
4225
4232
  */
4226
4233
  shouldDispatch?: true;
4227
- }): Promise<Message>;
4234
+ }): Promise<Message | null>;
4228
4235
  /**
4229
4236
  * Upgrades a star gift to a unique gift.
4230
4237
  *
@@ -4235,7 +4242,7 @@ export interface TelegramClient extends ITelegramClient {
4235
4242
  *
4236
4243
  * **Available**: 👤 users only
4237
4244
  *
4238
- * @returns Service message about the upgraded gift
4245
+ * @returns Service message about the upgraded gift, if one was generated.
4239
4246
  */
4240
4247
  upgradeStarGift(params: {
4241
4248
  gift: InputStarGift;
@@ -4249,7 +4256,7 @@ export interface TelegramClient extends ITelegramClient {
4249
4256
  * to the client's update handler.
4250
4257
  */
4251
4258
  shouldDispatch?: true;
4252
- }): Promise<Message>;
4259
+ }): Promise<Message | null>;
4253
4260
  /**
4254
4261
  * Add a sticker to a sticker set.
4255
4262
  *
@@ -23,7 +23,7 @@ import { QuoteParamsFrom } from './methods/messages/send-quote.js';
23
23
  import { CanApplyBoostResult } from './methods/premium/can-apply-boost.js';
24
24
  import { CanSendStoryResult } from './methods/stories/can-send-story.js';
25
25
  import { ITelegramStorageProvider } from './storage/provider.js';
26
- import { AllStories, ArrayPaginated, ArrayWithTotal, Boost, BoostSlot, BoostStats, BotChatJoinRequestUpdate, BotCommands, BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, BusinessCallbackQuery, BusinessChatLink, BusinessConnection, BusinessMessage, BusinessWorkHoursDay, CallbackQuery, Chat, ChatEvent, ChatInviteLink, ChatInviteLinkMember, ChatJoinRequestUpdate, ChatlistPreview, ChatMember, ChatMemberUpdate, ChatPreview, ChosenInlineResult, CollectibleInfo, DeleteBusinessMessageUpdate, DeleteMessageUpdate, DeleteStoryUpdate, Dialog, FactCheck, FileDownloadLocation, FileDownloadParameters, ForumTopic, FullChat, FullUser, GameHighScore, HistoryReadUpdate, InlineCallbackQuery, InlineQuery, InputChatEventFilters, InputDialogFolder, InputFileLike, InputInlineResult, InputMediaLike, InputMediaSticker, InputMessageId, InputPeerLike, InputPrivacyRule, InputReaction, InputStarGift, InputStickerSet, InputStickerSetItem, InputText, InputWebview, MaybeDynamic, Message, MessageEffect, MessageMedia, MessageReactions, ParametersSkip2, ParsedUpdate, PeerReaction, PeerStories, Photo, Poll, PollUpdate, PollVoteUpdate, PreCheckoutQuery, RawDocument, ReplyMarkup, SavedStarGift, SentCode, StarGift, StarGiftUnique, StarsStatus, StarsTransaction, Sticker, StickerSet, StickerType, StoriesStealthMode, Story, StoryInteractions, StoryUpdate, StoryViewer, StoryViewersList, TakeoutSession, TextWithEntities, TypingStatus, UploadedFile, UploadFileLike, User, UserStarGift, UserStatusUpdate, UserTypingUpdate, WebviewResult } from './types/index.js';
26
+ import { AllStories, ArrayPaginated, ArrayWithTotal, Boost, BoostSlot, BoostStats, BotChatJoinRequestUpdate, BotCommands, BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, BusinessCallbackQuery, BusinessChatLink, BusinessConnection, BusinessMessage, BusinessWorkHoursDay, CallbackQuery, Chat, ChatEvent, ChatInviteLink, ChatInviteLinkMember, ChatJoinRequestUpdate, ChatlistPreview, ChatMember, ChatMemberUpdate, ChatPreview, ChosenInlineResult, CollectibleInfo, DeleteBusinessMessageUpdate, DeleteMessageUpdate, DeleteStoryUpdate, Dialog, FactCheck, FileDownloadLocation, FileDownloadParameters, ForumTopic, FullChat, FullUser, GameHighScore, HistoryReadUpdate, InlineCallbackQuery, InlineQuery, InputChatEventFilters, InputDialogFolder, InputFileLike, InputInlineResult, InputMediaLike, InputMediaSticker, InputMessageId, InputPeerLike, InputPrivacyRule, InputReaction, InputStarGift, InputStickerSet, InputStickerSetItem, InputText, InputWebview, MaybeDynamic, Message, MessageEffect, MessageMedia, MessageReactions, ParametersSkip2, ParsedUpdate, Peer, PeerReaction, PeerStories, Photo, Poll, PollUpdate, PollVoteUpdate, PreCheckoutQuery, RawDocument, ReplyMarkup, SavedStarGift, SentCode, StarGift, StarGiftUnique, StarsStatus, StarsTransaction, Sticker, StickerSet, StickerType, StoriesStealthMode, Story, StoryInteractions, StoryUpdate, StoryViewer, StoryViewersList, TakeoutSession, TextWithEntities, TypingStatus, UploadedFile, UploadFileLike, User, UserStarGift, UserStatusUpdate, UserTypingUpdate, WebviewResult } from './types/index.js';
27
27
  import { ParsedUpdateHandlerParams } from './updates/parsed.js';
28
28
  import { RawUpdateInfo } from './updates/types.js';
29
29
  import { InputStringSessionData } from './utils/string-session.js';
@@ -1298,6 +1298,8 @@ export interface TelegramClient extends ITelegramClient {
1298
1298
  * @param longitude Longitude of the location
1299
1299
  */
1300
1300
  getNearbyChats(latitude: number, longitude: number): Promise<Chat[]>;
1301
+ /** Shorthand for {@link getChat} and {@link getUser} depending on the type of the peer */
1302
+ getPeer(peer: InputPeerLike): Promise<Peer>;
1301
1303
  /**
1302
1304
  * Get channels that are similar to a given channel
1303
1305
  *
@@ -3942,7 +3944,11 @@ export interface TelegramClient extends ITelegramClient {
3942
3944
  getMyBoostSlots(): Promise<BoostSlot[]>;
3943
3945
  /** Get one or more saved star gifts by their IDs */
3944
3946
  getSavedStarGiftsById(gifts: MaybeArray<InputStarGift>): Promise<SavedStarGift[]>;
3945
- /** Get a list of saved star gifts of a user */
3947
+ /**
3948
+ * Get a list of saved star gifts of a user/channel
3949
+ *
3950
+ * Note that filters currently only work for channels
3951
+ */
3946
3952
  getSavedStarGifts(params: {
3947
3953
  /** Peer to get saved gifts from */
3948
3954
  owner: InputPeerLike;
@@ -3978,9 +3984,10 @@ export interface TelegramClient extends ITelegramClient {
3978
3984
  /**
3979
3985
  * Get a list of gifts sent to a user.
3980
3986
  *
3981
- * **Available**: 👤 users only
3987
+ * **Available**: both users and bots
3982
3988
  *
3983
3989
  * @param userId User whose gifts to fetch
3990
+ * @deprecated Use {@link getSavedStarGifts} instead
3984
3991
  * @returns Gifts sent to the user
3985
3992
  */
3986
3993
  getStarGifts(userId: InputPeerLike, params?: {
@@ -3994,7 +4001,7 @@ export interface TelegramClient extends ITelegramClient {
3994
4001
  * @default 100
3995
4002
  */
3996
4003
  limit?: number;
3997
- }): Promise<ArrayPaginated<UserStarGift, string>>;
4004
+ }): Promise<ArrayPaginated<SavedStarGift, string>>;
3998
4005
  /**
3999
4006
  * Get Telegram Stars transactions for a given peer.
4000
4007
  *
@@ -4141,7 +4148,7 @@ export interface TelegramClient extends ITelegramClient {
4141
4148
  *
4142
4149
  * **Available**: 👤 users only
4143
4150
  *
4144
- * @returns Service message about the sent gift
4151
+ * @returns Service message about the sent gift, if one was generated.
4145
4152
  */
4146
4153
  sendStarGift(params: {
4147
4154
  /** ID of the peer to send the gift to */
@@ -4163,7 +4170,7 @@ export interface TelegramClient extends ITelegramClient {
4163
4170
  * to the client's update handler.
4164
4171
  */
4165
4172
  shouldDispatch?: true;
4166
- }): Promise<Message>;
4173
+ }): Promise<Message | null>;
4167
4174
  /**
4168
4175
  * Set current user's business introduction.
4169
4176
  *
@@ -4212,7 +4219,7 @@ export interface TelegramClient extends ITelegramClient {
4212
4219
  *
4213
4220
  * **Available**: 👤 users only
4214
4221
  *
4215
- * @returns Service message about the transferred gift
4222
+ * @returns Service message about the transferred gift, if one was generated.
4216
4223
  */
4217
4224
  transferStarGift(params: {
4218
4225
  /** Star gift to transfer */
@@ -4224,7 +4231,7 @@ export interface TelegramClient extends ITelegramClient {
4224
4231
  * to the client's update handler.
4225
4232
  */
4226
4233
  shouldDispatch?: true;
4227
- }): Promise<Message>;
4234
+ }): Promise<Message | null>;
4228
4235
  /**
4229
4236
  * Upgrades a star gift to a unique gift.
4230
4237
  *
@@ -4235,7 +4242,7 @@ export interface TelegramClient extends ITelegramClient {
4235
4242
  *
4236
4243
  * **Available**: 👤 users only
4237
4244
  *
4238
- * @returns Service message about the upgraded gift
4245
+ * @returns Service message about the upgraded gift, if one was generated.
4239
4246
  */
4240
4247
  upgradeStarGift(params: {
4241
4248
  gift: InputStarGift;
@@ -4249,7 +4256,7 @@ export interface TelegramClient extends ITelegramClient {
4249
4256
  * to the client's update handler.
4250
4257
  */
4251
4258
  shouldDispatch?: true;
4252
- }): Promise<Message>;
4259
+ }): Promise<Message | null>;
4253
4260
  /**
4254
4261
  * Add a sticker to a sticker set.
4255
4262
  *
@@ -0,0 +1,19 @@
1
+ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
2
+ globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
3
+ console.warn("[@mtcute/core] CommonJS support is deprecated and will be removed in 0.25.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
4
+ console.warn("[@mtcute/core] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
5
+ }
6
+ "use strict";
7
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ const peerUtils = require("../../utils/peer-utils.cjs");
9
+ const resolvePeer = require("../users/resolve-peer.cjs");
10
+ const getChat = require("./get-chat.cjs");
11
+ const getUser = require("./get-user.cjs");
12
+ async function getPeer(client, peer) {
13
+ const resolved = await resolvePeer.resolvePeer(client, peer);
14
+ if (peerUtils.isInputPeerUser(resolved)) {
15
+ return getUser.getUser(client, resolved);
16
+ }
17
+ return getChat.getChat(client, resolved);
18
+ }
19
+ exports.getPeer = getPeer;
@@ -0,0 +1,4 @@
1
+ import { ITelegramClient } from '../../client.types.js';
2
+ import { InputPeerLike, Peer } from '../../types/index.js';
3
+ /** Shorthand for {@link getChat} and {@link getUser} depending on the type of the peer */
4
+ export declare function getPeer(client: ITelegramClient, peer: InputPeerLike): Promise<Peer>;
@@ -0,0 +1,4 @@
1
+ import { ITelegramClient } from '../../client.types.js';
2
+ import { InputPeerLike, Peer } from '../../types/index.js';
3
+ /** Shorthand for {@link getChat} and {@link getUser} depending on the type of the peer */
4
+ export declare function getPeer(client: ITelegramClient, peer: InputPeerLike): Promise<Peer>;
@@ -0,0 +1,14 @@
1
+ import { isInputPeerUser } from "../../utils/peer-utils.js";
2
+ import { resolvePeer } from "../users/resolve-peer.js";
3
+ import { getChat } from "./get-chat.js";
4
+ import { getUser } from "./get-user.js";
5
+ async function getPeer(client, peer) {
6
+ const resolved = await resolvePeer(client, peer);
7
+ if (isInputPeerUser(resolved)) {
8
+ return getUser(client, resolved);
9
+ }
10
+ return getChat(client, resolved);
11
+ }
12
+ export {
13
+ getPeer
14
+ };
@@ -37,6 +37,6 @@ async function sendStarGift(client, params) {
37
37
  formId: form.formId
38
38
  });
39
39
  typeAssertions.assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
40
- return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch);
40
+ return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
41
41
  }
42
42
  exports.sendStarGift = sendStarGift;
@@ -12,7 +12,7 @@ import { InputText } from '../../types/misc/entities.js';
12
12
  * > For GUI clients, you should refer to the method's source code and
13
13
  * > present the payment form to the user.
14
14
  *
15
- * @returns Service message about the sent gift
15
+ * @returns Service message about the sent gift, if one was generated.
16
16
  */
17
17
  export declare function sendStarGift(client: ITelegramClient, params: {
18
18
  /** ID of the peer to send the gift to */
@@ -34,4 +34,4 @@ export declare function sendStarGift(client: ITelegramClient, params: {
34
34
  * to the client's update handler.
35
35
  */
36
36
  shouldDispatch?: true;
37
- }): Promise<Message>;
37
+ }): Promise<Message | null>;
@@ -12,7 +12,7 @@ import { InputText } from '../../types/misc/entities.js';
12
12
  * > For GUI clients, you should refer to the method's source code and
13
13
  * > present the payment form to the user.
14
14
  *
15
- * @returns Service message about the sent gift
15
+ * @returns Service message about the sent gift, if one was generated.
16
16
  */
17
17
  export declare function sendStarGift(client: ITelegramClient, params: {
18
18
  /** ID of the peer to send the gift to */
@@ -34,4 +34,4 @@ export declare function sendStarGift(client: ITelegramClient, params: {
34
34
  * to the client's update handler.
35
35
  */
36
36
  shouldDispatch?: true;
37
- }): Promise<Message>;
37
+ }): Promise<Message | null>;
@@ -30,7 +30,7 @@ async function sendStarGift(client, params) {
30
30
  formId: form.formId
31
31
  });
32
32
  assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
33
- return _findMessageInUpdate(client, res.updates, false, !shouldDispatch);
33
+ return _findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
34
34
  }
35
35
  export {
36
36
  sendStarGift
@@ -26,6 +26,6 @@ async function transferStarGift(client, params) {
26
26
  formId: form.formId
27
27
  });
28
28
  typeAssertions.assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
29
- return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch);
29
+ return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
30
30
  }
31
31
  exports.transferStarGift = transferStarGift;
@@ -9,7 +9,7 @@ import { Message } from '../../types/messages/message.js';
9
9
  * > For GUI clients, you should refer to the method's source code and
10
10
  * > present the payment form to the user.
11
11
  *
12
- * @returns Service message about the transferred gift
12
+ * @returns Service message about the transferred gift, if one was generated.
13
13
  */
14
14
  export declare function transferStarGift(client: ITelegramClient, params: {
15
15
  /** Star gift to transfer */
@@ -21,4 +21,4 @@ export declare function transferStarGift(client: ITelegramClient, params: {
21
21
  * to the client's update handler.
22
22
  */
23
23
  shouldDispatch?: true;
24
- }): Promise<Message>;
24
+ }): Promise<Message | null>;
@@ -9,7 +9,7 @@ import { Message } from '../../types/messages/message.js';
9
9
  * > For GUI clients, you should refer to the method's source code and
10
10
  * > present the payment form to the user.
11
11
  *
12
- * @returns Service message about the transferred gift
12
+ * @returns Service message about the transferred gift, if one was generated.
13
13
  */
14
14
  export declare function transferStarGift(client: ITelegramClient, params: {
15
15
  /** Star gift to transfer */
@@ -21,4 +21,4 @@ export declare function transferStarGift(client: ITelegramClient, params: {
21
21
  * to the client's update handler.
22
22
  */
23
23
  shouldDispatch?: true;
24
- }): Promise<Message>;
24
+ }): Promise<Message | null>;
@@ -19,7 +19,7 @@ async function transferStarGift(client, params) {
19
19
  formId: form.formId
20
20
  });
21
21
  assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
22
- return _findMessageInUpdate(client, res.updates, false, !shouldDispatch);
22
+ return _findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
23
23
  }
24
24
  export {
25
25
  transferStarGift
@@ -25,6 +25,6 @@ async function upgradeStarGift(client, params) {
25
25
  formId: form.formId
26
26
  });
27
27
  typeAssertions.assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
28
- return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch);
28
+ return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
29
29
  }
30
30
  exports.upgradeStarGift = upgradeStarGift;
@@ -9,7 +9,7 @@ import { Message } from '../../types/messages/message.js';
9
9
  * > For GUI clients, you should refer to the method's source code and
10
10
  * > present the payment form to the user.
11
11
  *
12
- * @returns Service message about the upgraded gift
12
+ * @returns Service message about the upgraded gift, if one was generated.
13
13
  */
14
14
  export declare function upgradeStarGift(client: ITelegramClient, params: {
15
15
  gift: InputStarGift;
@@ -23,4 +23,4 @@ export declare function upgradeStarGift(client: ITelegramClient, params: {
23
23
  * to the client's update handler.
24
24
  */
25
25
  shouldDispatch?: true;
26
- }): Promise<Message>;
26
+ }): Promise<Message | null>;
@@ -9,7 +9,7 @@ import { Message } from '../../types/messages/message.js';
9
9
  * > For GUI clients, you should refer to the method's source code and
10
10
  * > present the payment form to the user.
11
11
  *
12
- * @returns Service message about the upgraded gift
12
+ * @returns Service message about the upgraded gift, if one was generated.
13
13
  */
14
14
  export declare function upgradeStarGift(client: ITelegramClient, params: {
15
15
  gift: InputStarGift;
@@ -23,4 +23,4 @@ export declare function upgradeStarGift(client: ITelegramClient, params: {
23
23
  * to the client's update handler.
24
24
  */
25
25
  shouldDispatch?: true;
26
- }): Promise<Message>;
26
+ }): Promise<Message | null>;
@@ -18,7 +18,7 @@ async function upgradeStarGift(client, params) {
18
18
  formId: form.formId
19
19
  });
20
20
  assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
21
- return _findMessageInUpdate(client, res.updates, false, !shouldDispatch);
21
+ return _findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
22
22
  }
23
23
  export {
24
24
  upgradeStarGift
@@ -54,6 +54,7 @@ export { getChat } from './methods/chats/get-chat.js';
54
54
  export { getFullChat } from './methods/chats/get-full-chat.js';
55
55
  export { getFullUser } from './methods/chats/get-full-user.js';
56
56
  export { getNearbyChats } from './methods/chats/get-nearby-chats.js';
57
+ export { getPeer } from './methods/chats/get-peer.js';
57
58
  export { getSimilarChannels } from './methods/chats/get-similar-channels.js';
58
59
  export { getUser } from './methods/chats/get-user.js';
59
60
  export { iterChatEventLog } from './methods/chats/iter-chat-event-log.js';
@@ -54,6 +54,7 @@ export { getChat } from './methods/chats/get-chat.js';
54
54
  export { getFullChat } from './methods/chats/get-full-chat.js';
55
55
  export { getFullUser } from './methods/chats/get-full-user.js';
56
56
  export { getNearbyChats } from './methods/chats/get-nearby-chats.js';
57
+ export { getPeer } from './methods/chats/get-peer.js';
57
58
  export { getSimilarChannels } from './methods/chats/get-similar-channels.js';
58
59
  export { getUser } from './methods/chats/get-user.js';
59
60
  export { iterChatEventLog } from './methods/chats/iter-chat-event-log.js';
@@ -26,14 +26,19 @@ class TakeoutSession {
26
26
  * @param params Additional call parameters
27
27
  */
28
28
  async call(message, params) {
29
- return this.client.call(
30
- {
31
- _: "invokeWithTakeout",
32
- takeoutId: this.id,
33
- query: message
34
- },
35
- params
36
- );
29
+ let query = {
30
+ _: "invokeWithTakeout",
31
+ takeoutId: this.id,
32
+ query: message
33
+ };
34
+ if (params?.withMessageRange) {
35
+ query = {
36
+ _: "invokeWithMessagesRange",
37
+ range: params.withMessageRange,
38
+ query
39
+ };
40
+ }
41
+ return this.client.call(query, params);
37
42
  }
38
43
  /**
39
44
  * Create a proxy over {@link TelegramClient}
@@ -42,20 +47,12 @@ class TakeoutSession {
42
47
  * You can optionally provide a function to check if some
43
48
  * RPC method should be called via a takeout session or directly,
44
49
  * otherwise all methods are called through the takeout session.
45
- *
46
- * > **Note**: This will return a `Proxy` object that
47
- * > overrides `call` method. Using this method requires
48
- * > that your target environment supports `Proxy` and `Reflect` APIs
49
- *
50
- * @param predicate
51
- * Function that given the RPC call should determine whether
52
- * that call should be called via takeout session or not.
53
- * Returning `true` will use takeout session, `false` will not.
54
50
  */
55
- createProxy(predicate) {
56
- const boundCall = predicate ? (obj, params) => {
57
- if (predicate(obj)) {
58
- return this.call(obj, params);
51
+ createProxy(params) {
52
+ const boundCall = params ? (obj, theirParams) => {
53
+ const mergedParams = theirParams ? { ...params, ...theirParams } : params;
54
+ if (!params.predicate || params.predicate?.(obj)) {
55
+ return this.call(obj, mergedParams);
59
56
  }
60
57
  return this.client.call(obj, params);
61
58
  } : this.call.bind(this);
@@ -21,7 +21,10 @@ export declare class TakeoutSession {
21
21
  * @param message RPC method to call
22
22
  * @param params Additional call parameters
23
23
  */
24
- call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
24
+ call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions & {
25
+ /** If passed, the request will be wrapped in an `invokeWithMessagesRange` */
26
+ withMessageRange?: tl.TypeMessageRange;
27
+ }): Promise<tl.RpcCallReturn[T['_']]>;
25
28
  /**
26
29
  * Create a proxy over {@link TelegramClient}
27
30
  * that will use this takeout session to call methods.
@@ -29,17 +32,19 @@ export declare class TakeoutSession {
29
32
  * You can optionally provide a function to check if some
30
33
  * RPC method should be called via a takeout session or directly,
31
34
  * otherwise all methods are called through the takeout session.
32
- *
33
- * > **Note**: This will return a `Proxy` object that
34
- * > overrides `call` method. Using this method requires
35
- * > that your target environment supports `Proxy` and `Reflect` APIs
36
- *
37
- * @param predicate
38
- * Function that given the RPC call should determine whether
39
- * that call should be called via takeout session or not.
40
- * Returning `true` will use takeout session, `false` will not.
41
35
  */
42
- createProxy(predicate?: (obj: tl.TlObject) => boolean): ITelegramClient;
36
+ createProxy(params?: RpcCallOptions & {
37
+ /**
38
+ * Function that given the RPC call should determine whether
39
+ * that call should be called via takeout session (and use the rest of the params passed here) or not.
40
+ * Returning `true` will use takeout session, `false` will call the method directly.
41
+ *
42
+ * @default `undefined`, i.e. all calls are sent via takeout session
43
+ */
44
+ predicate?: (obj: tl.TlObject) => boolean;
45
+ /** If passed, the request will be wrapped in an `invokeWithMessagesRange` */
46
+ withMessageRange?: tl.TypeMessageRange;
47
+ }): ITelegramClient;
43
48
  /**
44
49
  * Finish account takeout session
45
50
  *
@@ -21,7 +21,10 @@ export declare class TakeoutSession {
21
21
  * @param message RPC method to call
22
22
  * @param params Additional call parameters
23
23
  */
24
- call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
24
+ call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions & {
25
+ /** If passed, the request will be wrapped in an `invokeWithMessagesRange` */
26
+ withMessageRange?: tl.TypeMessageRange;
27
+ }): Promise<tl.RpcCallReturn[T['_']]>;
25
28
  /**
26
29
  * Create a proxy over {@link TelegramClient}
27
30
  * that will use this takeout session to call methods.
@@ -29,17 +32,19 @@ export declare class TakeoutSession {
29
32
  * You can optionally provide a function to check if some
30
33
  * RPC method should be called via a takeout session or directly,
31
34
  * otherwise all methods are called through the takeout session.
32
- *
33
- * > **Note**: This will return a `Proxy` object that
34
- * > overrides `call` method. Using this method requires
35
- * > that your target environment supports `Proxy` and `Reflect` APIs
36
- *
37
- * @param predicate
38
- * Function that given the RPC call should determine whether
39
- * that call should be called via takeout session or not.
40
- * Returning `true` will use takeout session, `false` will not.
41
35
  */
42
- createProxy(predicate?: (obj: tl.TlObject) => boolean): ITelegramClient;
36
+ createProxy(params?: RpcCallOptions & {
37
+ /**
38
+ * Function that given the RPC call should determine whether
39
+ * that call should be called via takeout session (and use the rest of the params passed here) or not.
40
+ * Returning `true` will use takeout session, `false` will call the method directly.
41
+ *
42
+ * @default `undefined`, i.e. all calls are sent via takeout session
43
+ */
44
+ predicate?: (obj: tl.TlObject) => boolean;
45
+ /** If passed, the request will be wrapped in an `invokeWithMessagesRange` */
46
+ withMessageRange?: tl.TypeMessageRange;
47
+ }): ITelegramClient;
43
48
  /**
44
49
  * Finish account takeout session
45
50
  *
@@ -19,14 +19,19 @@ class TakeoutSession {
19
19
  * @param params Additional call parameters
20
20
  */
21
21
  async call(message, params) {
22
- return this.client.call(
23
- {
24
- _: "invokeWithTakeout",
25
- takeoutId: this.id,
26
- query: message
27
- },
28
- params
29
- );
22
+ let query = {
23
+ _: "invokeWithTakeout",
24
+ takeoutId: this.id,
25
+ query: message
26
+ };
27
+ if (params?.withMessageRange) {
28
+ query = {
29
+ _: "invokeWithMessagesRange",
30
+ range: params.withMessageRange,
31
+ query
32
+ };
33
+ }
34
+ return this.client.call(query, params);
30
35
  }
31
36
  /**
32
37
  * Create a proxy over {@link TelegramClient}
@@ -35,20 +40,12 @@ class TakeoutSession {
35
40
  * You can optionally provide a function to check if some
36
41
  * RPC method should be called via a takeout session or directly,
37
42
  * otherwise all methods are called through the takeout session.
38
- *
39
- * > **Note**: This will return a `Proxy` object that
40
- * > overrides `call` method. Using this method requires
41
- * > that your target environment supports `Proxy` and `Reflect` APIs
42
- *
43
- * @param predicate
44
- * Function that given the RPC call should determine whether
45
- * that call should be called via takeout session or not.
46
- * Returning `true` will use takeout session, `false` will not.
47
43
  */
48
- createProxy(predicate) {
49
- const boundCall = predicate ? (obj, params) => {
50
- if (predicate(obj)) {
51
- return this.call(obj, params);
44
+ createProxy(params) {
45
+ const boundCall = params ? (obj, theirParams) => {
46
+ const mergedParams = theirParams ? { ...params, ...theirParams } : params;
47
+ if (!params.predicate || params.predicate?.(obj)) {
48
+ return this.call(obj, mergedParams);
52
49
  }
53
50
  return this.client.call(obj, params);
54
51
  } : this.call.bind(this);
package/index.cjs CHANGED
@@ -152,6 +152,7 @@ const utils = require("./types/utils.cjs");
152
152
  exports.Long = Long;
153
153
  exports.getAllPeersFrom = peerUtils.getAllPeersFrom;
154
154
  exports.getBarePeerId = peerUtils.getBarePeerId;
155
+ exports.getBasicPeerType = peerUtils.getBasicPeerType;
155
156
  exports.getMarkedPeerId = peerUtils.getMarkedPeerId;
156
157
  exports.parseMarkedPeerId = peerUtils.parseMarkedPeerId;
157
158
  exports.toggleChannelIdMark = peerUtils.toggleChannelIdMark;
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { default as default2 } from "long";
2
- import { getAllPeersFrom, getBarePeerId, getMarkedPeerId, parseMarkedPeerId, toggleChannelIdMark } from "./utils/peer-utils.js";
2
+ import { getAllPeersFrom, getBarePeerId, getBasicPeerType, getMarkedPeerId, parseMarkedPeerId, toggleChannelIdMark } from "./utils/peer-utils.js";
3
3
  import { mtp, tl } from "@mtcute/tl";
4
4
  import { TelegramStorageManager } from "./highlevel/storage/storage.js";
5
5
  import { SentCode } from "./highlevel/types/auth/sent-code.js";
@@ -308,6 +308,7 @@ export {
308
308
  businessWorkHoursDaysToRaw,
309
309
  getAllPeersFrom,
310
310
  getBarePeerId,
311
+ getBasicPeerType,
311
312
  getMarkedPeerId,
312
313
  inputTextToTl,
313
314
  isUploadedFile,
package/methods.cjs CHANGED
@@ -56,6 +56,7 @@ const getChat = require("./highlevel/methods/chats/get-chat.cjs");
56
56
  const getFullChat = require("./highlevel/methods/chats/get-full-chat.cjs");
57
57
  const getFullUser = require("./highlevel/methods/chats/get-full-user.cjs");
58
58
  const getNearbyChats = require("./highlevel/methods/chats/get-nearby-chats.cjs");
59
+ const getPeer = require("./highlevel/methods/chats/get-peer.cjs");
59
60
  const getSimilarChannels = require("./highlevel/methods/chats/get-similar-channels.cjs");
60
61
  const getUser = require("./highlevel/methods/chats/get-user.cjs");
61
62
  const iterChatEventLog = require("./highlevel/methods/chats/iter-chat-event-log.cjs");
@@ -317,6 +318,7 @@ exports.getChat = getChat.getChat;
317
318
  exports.getFullChat = getFullChat.getFullChat;
318
319
  exports.getFullUser = getFullUser.getFullUser;
319
320
  exports.getNearbyChats = getNearbyChats.getNearbyChats;
321
+ exports.getPeer = getPeer.getPeer;
320
322
  exports.getSimilarChannels = getSimilarChannels.getSimilarChannels;
321
323
  exports.getUser = getUser.getUser;
322
324
  exports.iterChatEventLog = iterChatEventLog.iterChatEventLog;
package/methods.js CHANGED
@@ -49,6 +49,7 @@ import { getChat } from "./highlevel/methods/chats/get-chat.js";
49
49
  import { getFullChat } from "./highlevel/methods/chats/get-full-chat.js";
50
50
  import { getFullUser } from "./highlevel/methods/chats/get-full-user.js";
51
51
  import { getNearbyChats } from "./highlevel/methods/chats/get-nearby-chats.js";
52
+ import { getPeer } from "./highlevel/methods/chats/get-peer.js";
52
53
  import { getSimilarChannels } from "./highlevel/methods/chats/get-similar-channels.js";
53
54
  import { getUser } from "./highlevel/methods/chats/get-user.js";
54
55
  import { iterChatEventLog } from "./highlevel/methods/chats/iter-chat-event-log.js";
@@ -379,6 +380,7 @@ export {
379
380
  getMyUsername,
380
381
  getNearbyChats,
381
382
  getPasswordHint,
383
+ getPeer,
382
384
  getPeerDialogs,
383
385
  getPeerStories,
384
386
  getPrimaryInviteLink,
@@ -220,7 +220,7 @@ class NetworkManager {
220
220
  _: "initConnection",
221
221
  deviceModel,
222
222
  systemVersion: "1.0",
223
- appVersion: "0.20.4",
223
+ appVersion: "0.21.0",
224
224
  systemLangCode: "en",
225
225
  langPack: "",
226
226
  // "langPacks are for official apps only"
@@ -213,7 +213,7 @@ class NetworkManager {
213
213
  _: "initConnection",
214
214
  deviceModel,
215
215
  systemVersion: "1.0",
216
- appVersion: "0.20.4",
216
+ appVersion: "0.21.0",
217
217
  systemLangCode: "en",
218
218
  langPack: "",
219
219
  // "langPacks are for official apps only"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mtcute/core",
3
3
  "type": "module",
4
- "version": "0.20.4",
4
+ "version": "0.21.0",
5
5
  "description": "Type-safe library for MTProto (Telegram API)",
6
6
  "license": "MIT",
7
7
  "scripts": {},
@@ -6,6 +6,7 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
6
6
  "use strict";
7
7
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
8
  const errors = require("../types/errors.cjs");
9
+ const utils = require("../types/utils.cjs");
9
10
  const ZERO_CHANNEL_ID = -1e12;
10
11
  const ZERO_SECRET_CHAT_ID = -2e12;
11
12
  const MAX_SECRET_CHAT_ID = -1997852516353;
@@ -136,8 +137,51 @@ function* getAllPeersFrom(obj) {
136
137
  }
137
138
  }
138
139
  }
140
+ function getBasicPeerType(id) {
141
+ if (typeof id === "number") {
142
+ if (id < 0) {
143
+ if (MIN_MARKED_CHAT_ID <= id) {
144
+ return "chat";
145
+ }
146
+ if (MIN_MARKED_CHANNEL_ID <= id && id !== ZERO_CHANNEL_ID) {
147
+ return "channel";
148
+ }
149
+ } else if (id > 0 && id <= MAX_USER_ID) {
150
+ return "user";
151
+ }
152
+ throw new Error(`Invalid marked peer id: ${id}`);
153
+ }
154
+ switch (id._) {
155
+ case "peerUser":
156
+ case "inputUser":
157
+ case "inputUserSelf":
158
+ case "inputPeerSelf":
159
+ case "inputUserFromMessage":
160
+ case "inputPeerUser":
161
+ case "inputPeerUserFromMessage":
162
+ case "inputUserEmpty":
163
+ case "mtcute.dummyInputPeerMinUser":
164
+ return "user";
165
+ case "peerChat":
166
+ case "inputPeerChat":
167
+ return "chat";
168
+ case "peerChannel":
169
+ case "inputChannel":
170
+ case "inputChannelEmpty":
171
+ case "mtcute.dummyInputPeerMinChannel":
172
+ case "inputChannelFromMessage":
173
+ case "inputPeerChannel":
174
+ case "inputPeerChannelFromMessage":
175
+ return "channel";
176
+ case "inputPeerEmpty":
177
+ throw new Error("Empty peer");
178
+ default:
179
+ utils.assertNever();
180
+ }
181
+ }
139
182
  exports.getAllPeersFrom = getAllPeersFrom;
140
183
  exports.getBarePeerId = getBarePeerId;
184
+ exports.getBasicPeerType = getBasicPeerType;
141
185
  exports.getMarkedPeerId = getMarkedPeerId;
142
186
  exports.parseMarkedPeerId = parseMarkedPeerId;
143
187
  exports.toggleChannelIdMark = toggleChannelIdMark;
@@ -27,3 +27,5 @@ export declare function parseMarkedPeerId(id: number): [BasicPeerType, number];
27
27
  * Only checks `.user`, `.chat`, `.channel`, `.users` and `.chats` properties
28
28
  */
29
29
  export declare function getAllPeersFrom(obj: tl.TlObject | tl.TlObject[]): Iterable<tl.TypeUser | tl.TypeChat>;
30
+ /** Get the {@link BasicPeerType} of a peer, either by its marked ID or by its TL object */
31
+ export declare function getBasicPeerType(id: number | tl.TypePeer | tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel): BasicPeerType;
@@ -27,3 +27,5 @@ export declare function parseMarkedPeerId(id: number): [BasicPeerType, number];
27
27
  * Only checks `.user`, `.chat`, `.channel`, `.users` and `.chats` properties
28
28
  */
29
29
  export declare function getAllPeersFrom(obj: tl.TlObject | tl.TlObject[]): Iterable<tl.TypeUser | tl.TypeChat>;
30
+ /** Get the {@link BasicPeerType} of a peer, either by its marked ID or by its TL object */
31
+ export declare function getBasicPeerType(id: number | tl.TypePeer | tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel): BasicPeerType;
@@ -1,4 +1,5 @@
1
1
  import { MtUnsupportedError, MtArgumentError } from "../types/errors.js";
2
+ import { assertNever } from "../types/utils.js";
2
3
  const ZERO_CHANNEL_ID = -1e12;
3
4
  const ZERO_SECRET_CHAT_ID = -2e12;
4
5
  const MAX_SECRET_CHAT_ID = -1997852516353;
@@ -129,9 +130,52 @@ function* getAllPeersFrom(obj) {
129
130
  }
130
131
  }
131
132
  }
133
+ function getBasicPeerType(id) {
134
+ if (typeof id === "number") {
135
+ if (id < 0) {
136
+ if (MIN_MARKED_CHAT_ID <= id) {
137
+ return "chat";
138
+ }
139
+ if (MIN_MARKED_CHANNEL_ID <= id && id !== ZERO_CHANNEL_ID) {
140
+ return "channel";
141
+ }
142
+ } else if (id > 0 && id <= MAX_USER_ID) {
143
+ return "user";
144
+ }
145
+ throw new Error(`Invalid marked peer id: ${id}`);
146
+ }
147
+ switch (id._) {
148
+ case "peerUser":
149
+ case "inputUser":
150
+ case "inputUserSelf":
151
+ case "inputPeerSelf":
152
+ case "inputUserFromMessage":
153
+ case "inputPeerUser":
154
+ case "inputPeerUserFromMessage":
155
+ case "inputUserEmpty":
156
+ case "mtcute.dummyInputPeerMinUser":
157
+ return "user";
158
+ case "peerChat":
159
+ case "inputPeerChat":
160
+ return "chat";
161
+ case "peerChannel":
162
+ case "inputChannel":
163
+ case "inputChannelEmpty":
164
+ case "mtcute.dummyInputPeerMinChannel":
165
+ case "inputChannelFromMessage":
166
+ case "inputPeerChannel":
167
+ case "inputPeerChannelFromMessage":
168
+ return "channel";
169
+ case "inputPeerEmpty":
170
+ throw new Error("Empty peer");
171
+ default:
172
+ assertNever();
173
+ }
174
+ }
132
175
  export {
133
176
  getAllPeersFrom,
134
177
  getBarePeerId,
178
+ getBasicPeerType,
135
179
  getMarkedPeerId,
136
180
  parseMarkedPeerId,
137
181
  toggleChannelIdMark
package/utils.cjs CHANGED
@@ -71,6 +71,7 @@ exports.getRandomInt = miscUtils$1.getRandomInt;
71
71
  exports.sleepWithAbort = miscUtils$1.sleepWithAbort;
72
72
  exports.getAllPeersFrom = peerUtils$1.getAllPeersFrom;
73
73
  exports.getBarePeerId = peerUtils$1.getBarePeerId;
74
+ exports.getBasicPeerType = peerUtils$1.getBasicPeerType;
74
75
  exports.getMarkedPeerId = peerUtils$1.getMarkedPeerId;
75
76
  exports.parseMarkedPeerId = peerUtils$1.parseMarkedPeerId;
76
77
  exports.toggleChannelIdMark = peerUtils$1.toggleChannelIdMark;
package/utils.js CHANGED
@@ -10,7 +10,7 @@ import { SortedLinkedList } from "./utils/linked-list.js";
10
10
  import { LogManager, Logger } from "./utils/logger.js";
11
11
  import { LongMap, LongSet, compareLongs, longFromBuffer, longFromFastString, longToFastString, randomLong, removeFromLongArray } from "./utils/long-utils.js";
12
12
  import { getRandomInt, sleepWithAbort } from "./utils/misc-utils.js";
13
- import { getAllPeersFrom, getBarePeerId, getMarkedPeerId, parseMarkedPeerId, toggleChannelIdMark } from "./utils/peer-utils.js";
13
+ import { getAllPeersFrom, getBarePeerId, getBasicPeerType, getMarkedPeerId, parseMarkedPeerId, toggleChannelIdMark } from "./utils/peer-utils.js";
14
14
  import { SortedArray } from "./utils/sorted-array.js";
15
15
  import { jsonToTlJson, tlJsonToJson } from "./utils/tl-json.js";
16
16
  import { assertTrue, assertTypeIs, assertTypeIsNot, hasValueAtKey, isPresent, isTlRpcError, mtpAssertTypeIs } from "./utils/type-assertions.js";
@@ -80,6 +80,7 @@ export {
80
80
  generateKeyAndIvFromNonce,
81
81
  getAllPeersFrom,
82
82
  getBarePeerId,
83
+ getBasicPeerType,
83
84
  getMarkedPeerId,
84
85
  getRandomInt,
85
86
  hasValueAtKey,