@mtcute/core 0.23.2 → 0.24.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 (131) hide show
  1. package/client.cjs +21 -2
  2. package/client.js +21 -2
  3. package/highlevel/base.cjs +15 -3
  4. package/highlevel/base.d.cts +4 -1
  5. package/highlevel/base.d.ts +4 -1
  6. package/highlevel/base.js +15 -3
  7. package/highlevel/client.d.cts +56 -7
  8. package/highlevel/client.d.ts +56 -7
  9. package/highlevel/client.types.d.cts +2 -1
  10. package/highlevel/client.types.d.ts +2 -1
  11. package/highlevel/methods/chats/get-chat-event-log.cjs +5 -3
  12. package/highlevel/methods/chats/get-chat-event-log.js +3 -1
  13. package/highlevel/methods/files/download-iterable.cjs +1 -1
  14. package/highlevel/methods/files/download-iterable.js +1 -1
  15. package/highlevel/methods/premium/_normalize-input-star-gift.cjs +6 -0
  16. package/highlevel/methods/premium/_normalize-input-star-gift.js +6 -0
  17. package/highlevel/methods/premium/buy-resale-gift.cjs +30 -0
  18. package/highlevel/methods/premium/buy-resale-gift.d.cts +13 -0
  19. package/highlevel/methods/premium/buy-resale-gift.d.ts +13 -0
  20. package/highlevel/methods/premium/buy-resale-gift.js +25 -0
  21. package/highlevel/methods/premium/get-resale-star-gifts.cjs +107 -0
  22. package/highlevel/methods/premium/get-resale-star-gifts.d.cts +45 -0
  23. package/highlevel/methods/premium/get-resale-star-gifts.d.ts +45 -0
  24. package/highlevel/methods/premium/get-resale-star-gifts.js +102 -0
  25. package/highlevel/methods/premium/get-unique-star-gift.cjs +1 -1
  26. package/highlevel/methods/premium/get-unique-star-gift.js +1 -1
  27. package/highlevel/methods/premium/set-gift-resale-price.cjs +19 -0
  28. package/highlevel/methods/premium/set-gift-resale-price.d.cts +9 -0
  29. package/highlevel/methods/premium/set-gift-resale-price.d.ts +9 -0
  30. package/highlevel/methods/premium/set-gift-resale-price.js +14 -0
  31. package/highlevel/methods/premium/transfer-star-gift.cjs +28 -13
  32. package/highlevel/methods/premium/transfer-star-gift.d.cts +1 -1
  33. package/highlevel/methods/premium/transfer-star-gift.d.ts +1 -1
  34. package/highlevel/methods/premium/transfer-star-gift.js +28 -13
  35. package/highlevel/methods/premium/upgrade-star-gift.cjs +26 -11
  36. package/highlevel/methods/premium/upgrade-star-gift.js +26 -11
  37. package/highlevel/methods/users/resolve-peer.cjs +63 -70
  38. package/highlevel/methods/users/resolve-peer.d.cts +2 -1
  39. package/highlevel/methods/users/resolve-peer.d.ts +2 -1
  40. package/highlevel/methods/users/resolve-peer.js +61 -68
  41. package/highlevel/methods/users/resolve-phone-number.cjs +36 -0
  42. package/highlevel/methods/users/resolve-phone-number.d.cts +10 -0
  43. package/highlevel/methods/users/resolve-phone-number.d.ts +10 -0
  44. package/highlevel/methods/users/resolve-phone-number.js +31 -0
  45. package/highlevel/methods.d.cts +10 -4
  46. package/highlevel/methods.d.ts +10 -4
  47. package/highlevel/types/messages/message-action.cjs +8 -2
  48. package/highlevel/types/messages/message-action.d.cts +6 -0
  49. package/highlevel/types/messages/message-action.d.ts +6 -0
  50. package/highlevel/types/messages/message-action.js +8 -2
  51. package/highlevel/types/peers/chat-event/actions.cjs +49 -1
  52. package/highlevel/types/peers/chat-event/actions.d.cts +60 -2
  53. package/highlevel/types/peers/chat-event/actions.d.ts +60 -2
  54. package/highlevel/types/peers/chat-event/actions.js +49 -1
  55. package/highlevel/types/peers/chat-event/filters.cjs +9 -0
  56. package/highlevel/types/peers/chat-event/filters.js +9 -0
  57. package/highlevel/types/peers/chat-event/index.cjs +3 -2
  58. package/highlevel/types/peers/chat-event/index.d.cts +2 -1
  59. package/highlevel/types/peers/chat-event/index.d.ts +2 -1
  60. package/highlevel/types/peers/chat-event/index.js +3 -2
  61. package/highlevel/types/peers/chat.cjs +5 -4
  62. package/highlevel/types/peers/chat.d.cts +2 -0
  63. package/highlevel/types/peers/chat.d.ts +2 -0
  64. package/highlevel/types/peers/chat.js +5 -4
  65. package/highlevel/types/peers/peer.d.cts +2 -3
  66. package/highlevel/types/peers/peer.d.ts +2 -3
  67. package/highlevel/types/peers/user.cjs +1 -4
  68. package/highlevel/types/peers/user.js +1 -4
  69. package/highlevel/types/premium/saved-star-gift.cjs +8 -0
  70. package/highlevel/types/premium/saved-star-gift.d.cts +4 -0
  71. package/highlevel/types/premium/saved-star-gift.d.ts +4 -0
  72. package/highlevel/types/premium/saved-star-gift.js +8 -0
  73. package/highlevel/types/premium/stars-gift-unique.cjs +13 -2
  74. package/highlevel/types/premium/stars-gift-unique.d.cts +6 -2
  75. package/highlevel/types/premium/stars-gift-unique.d.ts +6 -2
  76. package/highlevel/types/premium/stars-gift-unique.js +13 -2
  77. package/highlevel/types/premium/stars-gift.cjs +11 -1
  78. package/highlevel/types/premium/stars-gift.d.cts +8 -1
  79. package/highlevel/types/premium/stars-gift.d.ts +8 -1
  80. package/highlevel/types/premium/stars-gift.js +11 -1
  81. package/highlevel/types/premium/stars-transaction.cjs +7 -1
  82. package/highlevel/types/premium/stars-transaction.d.cts +9 -2
  83. package/highlevel/types/premium/stars-transaction.d.ts +9 -2
  84. package/highlevel/types/premium/stars-transaction.js +7 -1
  85. package/highlevel/types/reactions/emoji-status.cjs +6 -0
  86. package/highlevel/types/reactions/emoji-status.d.cts +1 -0
  87. package/highlevel/types/reactions/emoji-status.d.ts +1 -0
  88. package/highlevel/types/reactions/emoji-status.js +6 -0
  89. package/highlevel/types/utils.d.cts +4 -0
  90. package/highlevel/types/utils.d.ts +4 -0
  91. package/highlevel/updates/manager.cjs +35 -26
  92. package/highlevel/updates/manager.d.cts +6 -6
  93. package/highlevel/updates/manager.d.ts +6 -6
  94. package/highlevel/updates/manager.js +35 -26
  95. package/highlevel/updates/types.d.cts +6 -6
  96. package/highlevel/updates/types.d.ts +6 -6
  97. package/highlevel/utils/misc-utils.cjs +8 -0
  98. package/highlevel/utils/misc-utils.d.cts +2 -1
  99. package/highlevel/utils/misc-utils.d.ts +2 -1
  100. package/highlevel/utils/misc-utils.js +8 -0
  101. package/highlevel/worker/port.cjs +7 -5
  102. package/highlevel/worker/port.d.cts +3 -2
  103. package/highlevel/worker/port.d.ts +3 -2
  104. package/highlevel/worker/port.js +7 -5
  105. package/methods.cjs +8 -0
  106. package/methods.js +8 -0
  107. package/network/client.cjs +12 -8
  108. package/network/client.d.cts +4 -1
  109. package/network/client.d.ts +4 -1
  110. package/network/client.js +12 -8
  111. package/network/network-manager.cjs +1 -1
  112. package/network/network-manager.js +1 -1
  113. package/network/session-connection.cjs +1 -0
  114. package/network/session-connection.js +1 -0
  115. package/package.json +2 -2
  116. package/utils/binary/compat.cjs +9 -0
  117. package/utils/binary/compat.js +9 -0
  118. package/utils/index.d.cts +0 -1
  119. package/utils/index.d.ts +0 -1
  120. package/utils/sorted-array.cjs +6 -0
  121. package/utils/sorted-array.d.cts +2 -0
  122. package/utils/sorted-array.d.ts +2 -0
  123. package/utils/sorted-array.js +6 -0
  124. package/utils.cjs +1 -2
  125. package/utils.js +2 -3
  126. package/utils/linked-list.cjs +0 -65
  127. package/utils/linked-list.d.cts +0 -21
  128. package/utils/linked-list.d.ts +0 -21
  129. package/utils/linked-list.js +0 -60
  130. package/utils/linked-list.test.d.cts +0 -1
  131. package/utils/linked-list.test.d.ts +0 -1
@@ -5,6 +5,7 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
5
5
  }
6
6
  "use strict";
7
7
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ const tl = require("@mtcute/tl");
8
9
  const typeAssertions = require("../../../utils/type-assertions.cjs");
9
10
  const findInUpdate = require("../messages/find-in-update.cjs");
10
11
  const _normalizeInputStarGift = require("./_normalize-input-star-gift.cjs");
@@ -15,16 +16,30 @@ async function upgradeStarGift(client, params) {
15
16
  stargift: await _normalizeInputStarGift._normalizeInputStarGift(client, gift),
16
17
  keepOriginalDetails
17
18
  };
18
- const form = await client.call({
19
- _: "payments.getPaymentForm",
20
- invoice
21
- });
22
- const res = await client.call({
23
- _: "payments.sendStarsForm",
24
- invoice,
25
- formId: form.formId
26
- });
27
- typeAssertions.assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
28
- return findInUpdate._findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
19
+ let updates;
20
+ try {
21
+ const form = await client.call({
22
+ _: "payments.getPaymentForm",
23
+ invoice
24
+ });
25
+ const res = await client.call({
26
+ _: "payments.sendStarsForm",
27
+ invoice,
28
+ formId: form.formId
29
+ });
30
+ typeAssertions.assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
31
+ updates = res.updates;
32
+ } catch (e) {
33
+ if (tl.tl.RpcError.is(e, "NO_PAYMENT_NEEDED")) {
34
+ updates = await client.call({
35
+ _: "payments.upgradeStarGift",
36
+ stargift: invoice.stargift,
37
+ keepOriginalDetails: invoice.keepOriginalDetails
38
+ });
39
+ } else {
40
+ throw e;
41
+ }
42
+ }
43
+ return findInUpdate._findMessageInUpdate(client, updates, false, !shouldDispatch, true);
29
44
  }
30
45
  exports.upgradeStarGift = upgradeStarGift;
@@ -1,3 +1,4 @@
1
+ import { tl } from "@mtcute/tl";
1
2
  import { assertTypeIs } from "../../../utils/type-assertions.js";
2
3
  import { _findMessageInUpdate } from "../messages/find-in-update.js";
3
4
  import { _normalizeInputStarGift } from "./_normalize-input-star-gift.js";
@@ -8,17 +9,31 @@ async function upgradeStarGift(client, params) {
8
9
  stargift: await _normalizeInputStarGift(client, gift),
9
10
  keepOriginalDetails
10
11
  };
11
- const form = await client.call({
12
- _: "payments.getPaymentForm",
13
- invoice
14
- });
15
- const res = await client.call({
16
- _: "payments.sendStarsForm",
17
- invoice,
18
- formId: form.formId
19
- });
20
- assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
21
- return _findMessageInUpdate(client, res.updates, false, !shouldDispatch, true);
12
+ let updates;
13
+ try {
14
+ const form = await client.call({
15
+ _: "payments.getPaymentForm",
16
+ invoice
17
+ });
18
+ const res = await client.call({
19
+ _: "payments.sendStarsForm",
20
+ invoice,
21
+ formId: form.formId
22
+ });
23
+ assertTypeIs("payments.sendStarsForm", res, "payments.paymentResult");
24
+ updates = res.updates;
25
+ } catch (e) {
26
+ if (tl.RpcError.is(e, "NO_PAYMENT_NEEDED")) {
27
+ updates = await client.call({
28
+ _: "payments.upgradeStarGift",
29
+ stargift: invoice.stargift,
30
+ keepOriginalDetails: invoice.keepOriginalDetails
31
+ });
32
+ } else {
33
+ throw e;
34
+ }
35
+ }
36
+ return _findMessageInUpdate(client, updates, false, !shouldDispatch, true);
22
37
  }
23
38
  export {
24
39
  upgradeStarGift
@@ -5,13 +5,15 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
5
5
  }
6
6
  "use strict";
7
7
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ const utils = require("@fuman/utils");
8
9
  const tl = require("@mtcute/tl");
9
10
  const Long = require("long");
10
- const errors$1 = require("../../../types/errors.cjs");
11
+ const errors = require("../../../types/errors.cjs");
11
12
  const peerUtils = require("../../../utils/peer-utils.cjs");
12
- const errors = require("../../types/errors.cjs");
13
+ const errors$1 = require("../../types/errors.cjs");
13
14
  const peerUtils$1 = require("../../utils/peer-utils.cjs");
14
15
  const batchedQueries = require("../chats/batched-queries.cjs");
16
+ const resolvePhoneNumber = require("./resolve-phone-number.cjs");
15
17
  function _normalizePeerId(peerId) {
16
18
  if (typeof peerId === "object") {
17
19
  if (tl.tl.isAnyPeer(peerId)) {
@@ -34,6 +36,43 @@ function _normalizePeerId(peerId) {
34
36
  }
35
37
  return peerId;
36
38
  }
39
+ function _handleContactsResolvedPeer(peerId, res) {
40
+ if (res.peer._ === "peerUser") {
41
+ const id = res.peer.userId;
42
+ const found = res.users.find((it) => it.id === id);
43
+ if (found && found._ === "user") {
44
+ if (!found.accessHash) {
45
+ throw new errors$1.MtPeerNotFoundError(
46
+ `Peer (user) with username ${peerId} was found, but it has no access hash`
47
+ );
48
+ }
49
+ return {
50
+ _: "inputPeerUser",
51
+ userId: found.id,
52
+ accessHash: found.accessHash
53
+ };
54
+ }
55
+ } else if (res.peer._ === "peerChannel") {
56
+ const id = res.peer.channelId;
57
+ const found = res.chats.find((it) => it.id === id);
58
+ if (found) {
59
+ if (!(found._ === "channel" || found._ === "channelForbidden")) {
60
+ throw new errors.MtTypeAssertionError("ResolvedPeer#chats", "channel", found._);
61
+ }
62
+ if (!found.accessHash) {
63
+ throw new errors$1.MtPeerNotFoundError(`Peer (channel) with ${peerId} was found, but it has no access hash`);
64
+ }
65
+ return {
66
+ _: "inputPeerChannel",
67
+ channelId: found.id,
68
+ accessHash: found.accessHash
69
+ };
70
+ }
71
+ } else {
72
+ throw new errors.MtTypeAssertionError("ResolvedPeer#peer", "user or channel", res.peer._);
73
+ }
74
+ utils.unreachable();
75
+ }
37
76
  async function resolvePeer(client, peerId, force = false) {
38
77
  peerId = _normalizePeerId(peerId);
39
78
  if (typeof peerId === "object") {
@@ -45,76 +84,29 @@ async function resolvePeer(client, peerId, force = false) {
45
84
  }
46
85
  if (typeof peerId === "string") {
47
86
  if (peerId === "self" || peerId === "me") return { _: "inputPeerSelf" };
48
- peerId = peerId.replace(/[@+\s()]/g, "");
49
- let res;
50
- if (peerId.match(/^\d+$/)) {
51
- const fromStorage = await client.storage.peers.getByPhone(peerId);
87
+ peerId = peerId.replace(/^@/, "");
88
+ if (peerId.match(/^\d/)) {
89
+ throw new errors.MtArgumentError(
90
+ `Invalid username: ${peerId}. If you wanted to resolve by user ID, pass a number instead. If you wanted to resolve by phone number, use resolvePhoneNumber instead.`
91
+ );
92
+ }
93
+ if (!force) {
94
+ const fromStorage = await client.storage.peers.getByUsername(peerId);
52
95
  if (fromStorage) return fromStorage;
53
- try {
54
- res = await client.call({
55
- _: "contacts.resolvePhone",
56
- phone: peerId
57
- });
58
- } catch (e) {
59
- if (tl.tl.RpcError.is(e, "PHONE_NOT_OCCUPIED")) {
60
- throw new errors.MtPeerNotFoundError(`Peer with phone number ${peerId} was not found`);
61
- } else {
62
- throw e;
63
- }
64
- }
65
- } else {
66
- if (!force) {
67
- const fromStorage = await client.storage.peers.getByUsername(peerId);
68
- if (fromStorage) return fromStorage;
69
- }
70
- try {
71
- res = await client.call({
72
- _: "contacts.resolveUsername",
73
- username: peerId
74
- });
75
- } catch (e) {
76
- if (tl.tl.RpcError.is(e, "USERNAME_NOT_OCCUPIED")) {
77
- throw new errors.MtPeerNotFoundError(`Peer with username ${peerId} was not found`);
78
- } else {
79
- throw e;
80
- }
81
- }
82
96
  }
83
- if (res.peer._ === "peerUser") {
84
- const id = res.peer.userId;
85
- const found = res.users.find((it) => it.id === id);
86
- if (found && found._ === "user") {
87
- if (!found.accessHash) {
88
- throw new errors.MtPeerNotFoundError(
89
- `Peer (user) with username ${peerId} was found, but it has no access hash`
90
- );
91
- }
92
- return {
93
- _: "inputPeerUser",
94
- userId: found.id,
95
- accessHash: found.accessHash
96
- };
97
- }
98
- } else if (res.peer._ === "peerChannel") {
99
- const id = res.peer.channelId;
100
- const found = res.chats.find((it) => it.id === id);
101
- if (found) {
102
- if (!(found._ === "channel" || found._ === "channelForbidden")) {
103
- throw new errors$1.MtTypeAssertionError("contacts.resolveUsername#chats", "channel", found._);
104
- }
105
- if (!found.accessHash) {
106
- throw new errors.MtPeerNotFoundError(`Peer (channel) with ${peerId} was found, but it has no access hash`);
107
- }
108
- return {
109
- _: "inputPeerChannel",
110
- channelId: found.id,
111
- accessHash: found.accessHash
112
- };
97
+ try {
98
+ const res = await client.call({
99
+ _: "contacts.resolveUsername",
100
+ username: peerId
101
+ });
102
+ return _handleContactsResolvedPeer(peerId, res);
103
+ } catch (e) {
104
+ if (tl.tl.RpcError.is(e, "USERNAME_NOT_OCCUPIED") || tl.tl.RpcError.is(e, "USERNAME_INVALID")) {
105
+ throw new errors$1.MtPeerNotFoundError(`Peer with username ${peerId} was not found`);
106
+ } else {
107
+ throw e;
113
108
  }
114
- } else {
115
- throw new errors$1.MtTypeAssertionError("contacts.resolveUsername", "user or channel", res.peer._);
116
109
  }
117
- throw new errors.MtPeerNotFoundError(`Could not find a peer by ${peerId}`);
118
110
  }
119
111
  const [peerType, bareId] = peerUtils.parseMarkedPeerId(peerId);
120
112
  if (peerType === "chat") {
@@ -133,7 +125,7 @@ async function resolvePeer(client, peerId, force = false) {
133
125
  }
134
126
  }
135
127
  if (cached._ === "user" && cached.phone) {
136
- const resolved = await resolvePeer(client, cached.phone, true);
128
+ const resolved = await resolvePhoneNumber.resolvePhoneNumber(client, cached.phone, true);
137
129
  if (peerUtils.getMarkedPeerId(resolved) === peerId) {
138
130
  return resolved;
139
131
  }
@@ -171,7 +163,7 @@ async function resolvePeer(client, peerId, force = false) {
171
163
  break;
172
164
  }
173
165
  }
174
- throw new errors.MtPeerNotFoundError(`Peer ${peerId} is not found in local cache`);
166
+ throw new errors$1.MtPeerNotFoundError(`Peer ${peerId} is not found in local cache`);
175
167
  }
176
168
  async function resolveUser(client, peerId, force = false) {
177
169
  return peerUtils$1.toInputUser(await resolvePeer(client, peerId, force), peerId);
@@ -179,6 +171,7 @@ async function resolveUser(client, peerId, force = false) {
179
171
  async function resolveChannel(client, peerId, force = false) {
180
172
  return peerUtils$1.toInputChannel(await resolvePeer(client, peerId, force), peerId);
181
173
  }
174
+ exports._handleContactsResolvedPeer = _handleContactsResolvedPeer;
182
175
  exports._normalizePeerId = _normalizePeerId;
183
176
  exports.resolveChannel = resolveChannel;
184
177
  exports.resolvePeer = resolvePeer;
@@ -2,12 +2,13 @@ import { ITelegramClient } from '../../client.types.js';
2
2
  import { InputPeerLike } from '../../types/peers/index.js';
3
3
  import { tl } from '@mtcute/tl';
4
4
  export declare function _normalizePeerId(peerId: InputPeerLike): number | string | tl.TypeInputPeer;
5
+ export declare function _handleContactsResolvedPeer(peerId: string, res: tl.contacts.TypeResolvedPeer): tl.TypeInputPeer;
5
6
  /**
6
7
  * Get the `InputPeer` of a known peer id.
7
8
  * Useful when an `InputPeer` is needed in Raw API.
8
9
  *
9
10
  * @param peerId The peer identifier that you want to extract the `InputPeer` from.
10
- * @param force Whether to force re-fetch the peer from the server (only for usernames and phone numbers)
11
+ * @param force Whether to force re-fetch the peer from the server
11
12
  */
12
13
  export declare function resolvePeer(client: ITelegramClient, peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputPeer>;
13
14
  /**
@@ -2,12 +2,13 @@ import { ITelegramClient } from '../../client.types.js';
2
2
  import { InputPeerLike } from '../../types/peers/index.js';
3
3
  import { tl } from '@mtcute/tl';
4
4
  export declare function _normalizePeerId(peerId: InputPeerLike): number | string | tl.TypeInputPeer;
5
+ export declare function _handleContactsResolvedPeer(peerId: string, res: tl.contacts.TypeResolvedPeer): tl.TypeInputPeer;
5
6
  /**
6
7
  * Get the `InputPeer` of a known peer id.
7
8
  * Useful when an `InputPeer` is needed in Raw API.
8
9
  *
9
10
  * @param peerId The peer identifier that you want to extract the `InputPeer` from.
10
- * @param force Whether to force re-fetch the peer from the server (only for usernames and phone numbers)
11
+ * @param force Whether to force re-fetch the peer from the server
11
12
  */
12
13
  export declare function resolvePeer(client: ITelegramClient, peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputPeer>;
13
14
  /**
@@ -1,10 +1,12 @@
1
+ import { unreachable } from "@fuman/utils";
1
2
  import { tl } from "@mtcute/tl";
2
3
  import Long from "long";
3
- import { MtTypeAssertionError } from "../../../types/errors.js";
4
+ import { MtArgumentError, MtTypeAssertionError } from "../../../types/errors.js";
4
5
  import { parseMarkedPeerId, getMarkedPeerId, toggleChannelIdMark } from "../../../utils/peer-utils.js";
5
6
  import { MtPeerNotFoundError } from "../../types/errors.js";
6
7
  import { extractUsernames, toInputUser, toInputChannel, toInputPeer } from "../../utils/peer-utils.js";
7
8
  import { _getChannelsBatched, _getUsersBatched } from "../chats/batched-queries.js";
9
+ import { resolvePhoneNumber } from "./resolve-phone-number.js";
8
10
  function _normalizePeerId(peerId) {
9
11
  if (typeof peerId === "object") {
10
12
  if (tl.isAnyPeer(peerId)) {
@@ -27,6 +29,43 @@ function _normalizePeerId(peerId) {
27
29
  }
28
30
  return peerId;
29
31
  }
32
+ function _handleContactsResolvedPeer(peerId, res) {
33
+ if (res.peer._ === "peerUser") {
34
+ const id = res.peer.userId;
35
+ const found = res.users.find((it) => it.id === id);
36
+ if (found && found._ === "user") {
37
+ if (!found.accessHash) {
38
+ throw new MtPeerNotFoundError(
39
+ `Peer (user) with username ${peerId} was found, but it has no access hash`
40
+ );
41
+ }
42
+ return {
43
+ _: "inputPeerUser",
44
+ userId: found.id,
45
+ accessHash: found.accessHash
46
+ };
47
+ }
48
+ } else if (res.peer._ === "peerChannel") {
49
+ const id = res.peer.channelId;
50
+ const found = res.chats.find((it) => it.id === id);
51
+ if (found) {
52
+ if (!(found._ === "channel" || found._ === "channelForbidden")) {
53
+ throw new MtTypeAssertionError("ResolvedPeer#chats", "channel", found._);
54
+ }
55
+ if (!found.accessHash) {
56
+ throw new MtPeerNotFoundError(`Peer (channel) with ${peerId} was found, but it has no access hash`);
57
+ }
58
+ return {
59
+ _: "inputPeerChannel",
60
+ channelId: found.id,
61
+ accessHash: found.accessHash
62
+ };
63
+ }
64
+ } else {
65
+ throw new MtTypeAssertionError("ResolvedPeer#peer", "user or channel", res.peer._);
66
+ }
67
+ unreachable();
68
+ }
30
69
  async function resolvePeer(client, peerId, force = false) {
31
70
  peerId = _normalizePeerId(peerId);
32
71
  if (typeof peerId === "object") {
@@ -38,76 +77,29 @@ async function resolvePeer(client, peerId, force = false) {
38
77
  }
39
78
  if (typeof peerId === "string") {
40
79
  if (peerId === "self" || peerId === "me") return { _: "inputPeerSelf" };
41
- peerId = peerId.replace(/[@+\s()]/g, "");
42
- let res;
43
- if (peerId.match(/^\d+$/)) {
44
- const fromStorage = await client.storage.peers.getByPhone(peerId);
80
+ peerId = peerId.replace(/^@/, "");
81
+ if (peerId.match(/^\d/)) {
82
+ throw new MtArgumentError(
83
+ `Invalid username: ${peerId}. If you wanted to resolve by user ID, pass a number instead. If you wanted to resolve by phone number, use resolvePhoneNumber instead.`
84
+ );
85
+ }
86
+ if (!force) {
87
+ const fromStorage = await client.storage.peers.getByUsername(peerId);
45
88
  if (fromStorage) return fromStorage;
46
- try {
47
- res = await client.call({
48
- _: "contacts.resolvePhone",
49
- phone: peerId
50
- });
51
- } catch (e) {
52
- if (tl.RpcError.is(e, "PHONE_NOT_OCCUPIED")) {
53
- throw new MtPeerNotFoundError(`Peer with phone number ${peerId} was not found`);
54
- } else {
55
- throw e;
56
- }
57
- }
58
- } else {
59
- if (!force) {
60
- const fromStorage = await client.storage.peers.getByUsername(peerId);
61
- if (fromStorage) return fromStorage;
62
- }
63
- try {
64
- res = await client.call({
65
- _: "contacts.resolveUsername",
66
- username: peerId
67
- });
68
- } catch (e) {
69
- if (tl.RpcError.is(e, "USERNAME_NOT_OCCUPIED")) {
70
- throw new MtPeerNotFoundError(`Peer with username ${peerId} was not found`);
71
- } else {
72
- throw e;
73
- }
74
- }
75
89
  }
76
- if (res.peer._ === "peerUser") {
77
- const id = res.peer.userId;
78
- const found = res.users.find((it) => it.id === id);
79
- if (found && found._ === "user") {
80
- if (!found.accessHash) {
81
- throw new MtPeerNotFoundError(
82
- `Peer (user) with username ${peerId} was found, but it has no access hash`
83
- );
84
- }
85
- return {
86
- _: "inputPeerUser",
87
- userId: found.id,
88
- accessHash: found.accessHash
89
- };
90
- }
91
- } else if (res.peer._ === "peerChannel") {
92
- const id = res.peer.channelId;
93
- const found = res.chats.find((it) => it.id === id);
94
- if (found) {
95
- if (!(found._ === "channel" || found._ === "channelForbidden")) {
96
- throw new MtTypeAssertionError("contacts.resolveUsername#chats", "channel", found._);
97
- }
98
- if (!found.accessHash) {
99
- throw new MtPeerNotFoundError(`Peer (channel) with ${peerId} was found, but it has no access hash`);
100
- }
101
- return {
102
- _: "inputPeerChannel",
103
- channelId: found.id,
104
- accessHash: found.accessHash
105
- };
90
+ try {
91
+ const res = await client.call({
92
+ _: "contacts.resolveUsername",
93
+ username: peerId
94
+ });
95
+ return _handleContactsResolvedPeer(peerId, res);
96
+ } catch (e) {
97
+ if (tl.RpcError.is(e, "USERNAME_NOT_OCCUPIED") || tl.RpcError.is(e, "USERNAME_INVALID")) {
98
+ throw new MtPeerNotFoundError(`Peer with username ${peerId} was not found`);
99
+ } else {
100
+ throw e;
106
101
  }
107
- } else {
108
- throw new MtTypeAssertionError("contacts.resolveUsername", "user or channel", res.peer._);
109
102
  }
110
- throw new MtPeerNotFoundError(`Could not find a peer by ${peerId}`);
111
103
  }
112
104
  const [peerType, bareId] = parseMarkedPeerId(peerId);
113
105
  if (peerType === "chat") {
@@ -126,7 +118,7 @@ async function resolvePeer(client, peerId, force = false) {
126
118
  }
127
119
  }
128
120
  if (cached._ === "user" && cached.phone) {
129
- const resolved = await resolvePeer(client, cached.phone, true);
121
+ const resolved = await resolvePhoneNumber(client, cached.phone, true);
130
122
  if (getMarkedPeerId(resolved) === peerId) {
131
123
  return resolved;
132
124
  }
@@ -173,6 +165,7 @@ async function resolveChannel(client, peerId, force = false) {
173
165
  return toInputChannel(await resolvePeer(client, peerId, force), peerId);
174
166
  }
175
167
  export {
168
+ _handleContactsResolvedPeer,
176
169
  _normalizePeerId,
177
170
  resolveChannel,
178
171
  resolvePeer,
@@ -0,0 +1,36 @@
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 tl = require("@mtcute/tl");
9
+ const errors = require("../../../types/errors.cjs");
10
+ const errors$1 = require("../../types/errors.cjs");
11
+ const resolvePeer = require("./resolve-peer.cjs");
12
+ async function resolvePhoneNumber(client, phone, force = false) {
13
+ phone = phone.replace(/[@+\s()]/g, "");
14
+ if (!phone.match(/^\d+$/)) {
15
+ throw new errors.MtArgumentError("phone should only contain digits");
16
+ }
17
+ if (!force) {
18
+ const fromStorage = await client.storage.peers.getByPhone(phone);
19
+ if (fromStorage) return fromStorage;
20
+ }
21
+ let res;
22
+ try {
23
+ res = await client.call({
24
+ _: "contacts.resolvePhone",
25
+ phone
26
+ });
27
+ } catch (e) {
28
+ if (tl.tl.RpcError.is(e, "PHONE_NOT_OCCUPIED")) {
29
+ throw new errors$1.MtPeerNotFoundError(`Peer with phone number ${phone} was not found`);
30
+ } else {
31
+ throw e;
32
+ }
33
+ }
34
+ return resolvePeer._handleContactsResolvedPeer(phone, res);
35
+ }
36
+ exports.resolvePhoneNumber = resolvePhoneNumber;
@@ -0,0 +1,10 @@
1
+ import { ITelegramClient } from '../../client.types.js';
2
+ import { tl } from '@mtcute/tl';
3
+ /**
4
+ * Get the `InputPeer` by user's phone number.
5
+ * Useful when an `InputPeer` is needed in Raw API.
6
+ *
7
+ * @param phone Phone number of the user
8
+ * @param force Whether to force re-fetch the peer from the server
9
+ */
10
+ export declare function resolvePhoneNumber(client: ITelegramClient, phone: string, force?: boolean): Promise<tl.TypeInputPeer>;
@@ -0,0 +1,10 @@
1
+ import { ITelegramClient } from '../../client.types.js';
2
+ import { tl } from '@mtcute/tl';
3
+ /**
4
+ * Get the `InputPeer` by user's phone number.
5
+ * Useful when an `InputPeer` is needed in Raw API.
6
+ *
7
+ * @param phone Phone number of the user
8
+ * @param force Whether to force re-fetch the peer from the server
9
+ */
10
+ export declare function resolvePhoneNumber(client: ITelegramClient, phone: string, force?: boolean): Promise<tl.TypeInputPeer>;
@@ -0,0 +1,31 @@
1
+ import { tl } from "@mtcute/tl";
2
+ import { MtArgumentError } from "../../../types/errors.js";
3
+ import { MtPeerNotFoundError } from "../../types/errors.js";
4
+ import { _handleContactsResolvedPeer } from "./resolve-peer.js";
5
+ async function resolvePhoneNumber(client, phone, force = false) {
6
+ phone = phone.replace(/[@+\s()]/g, "");
7
+ if (!phone.match(/^\d+$/)) {
8
+ throw new MtArgumentError("phone should only contain digits");
9
+ }
10
+ if (!force) {
11
+ const fromStorage = await client.storage.peers.getByPhone(phone);
12
+ if (fromStorage) return fromStorage;
13
+ }
14
+ let res;
15
+ try {
16
+ res = await client.call({
17
+ _: "contacts.resolvePhone",
18
+ phone
19
+ });
20
+ } catch (e) {
21
+ if (tl.RpcError.is(e, "PHONE_NOT_OCCUPIED")) {
22
+ throw new MtPeerNotFoundError(`Peer with phone number ${phone} was not found`);
23
+ } else {
24
+ throw e;
25
+ }
26
+ }
27
+ return _handleContactsResolvedPeer(phone, res);
28
+ }
29
+ export {
30
+ resolvePhoneNumber
31
+ };
@@ -131,14 +131,14 @@ export { iterInviteLinks } from './methods/invite-links/iter-invite-links.js';
131
131
  export { revokeInviteLink } from './methods/invite-links/revoke-invite-link.js';
132
132
  export { closePoll } from './methods/messages/close-poll.js';
133
133
  export { deleteMessagesById } from './methods/messages/delete-messages.js';
134
- export type { DeleteMessagesParams } from './methods/messages/delete-messages.js';
135
134
  export { deleteMessages } from './methods/messages/delete-messages.js';
135
+ export type { DeleteMessagesParams } from './methods/messages/delete-messages.js';
136
136
  export { deleteScheduledMessages } from './methods/messages/delete-scheduled-messages.js';
137
137
  export { editInlineMessage } from './methods/messages/edit-inline-message.js';
138
138
  export { editMessage } from './methods/messages/edit-message.js';
139
139
  export { forwardMessagesById } from './methods/messages/forward-messages.js';
140
- export type { ForwardMessageOptions } from './methods/messages/forward-messages.js';
141
140
  export { forwardMessages } from './methods/messages/forward-messages.js';
141
+ export type { ForwardMessageOptions } from './methods/messages/forward-messages.js';
142
142
  export { getAllScheduledMessages } from './methods/messages/get-all-scheduled-messages.js';
143
143
  export { getAvailableMessageEffects } from './methods/messages/get-available-effects.js';
144
144
  export { getCallbackQueryMessage } from './methods/messages/get-callback-query-message.js';
@@ -167,8 +167,8 @@ export { readReactions } from './methods/messages/read-reactions.js';
167
167
  export { searchGlobal } from './methods/messages/search-global.js';
168
168
  export type { SearchGlobalOffset } from './methods/messages/search-global.js';
169
169
  export { searchHashtag } from './methods/messages/search-hashtag.js';
170
- export type { SearchHashtagOffset } from './methods/messages/search-hashtag.js';
171
170
  export { iterSearchHashtag } from './methods/messages/search-hashtag.js';
171
+ export type { SearchHashtagOffset } from './methods/messages/search-hashtag.js';
172
172
  export { searchMessages } from './methods/messages/search-messages.js';
173
173
  export type { SearchMessagesOffset } from './methods/messages/search-messages.js';
174
174
  export { answerText } from './methods/messages/send-answer.js';
@@ -177,6 +177,7 @@ export { answerMediaGroup } from './methods/messages/send-answer.js';
177
177
  export { commentText } from './methods/messages/send-comment.js';
178
178
  export { commentMedia } from './methods/messages/send-comment.js';
179
179
  export { commentMediaGroup } from './methods/messages/send-comment.js';
180
+ export type { CommonSendParams } from './methods/messages/send-common.js';
180
181
  export { sendCopyGroup } from './methods/messages/send-copy-group.js';
181
182
  export type { SendCopyGroupParams } from './methods/messages/send-copy-group.js';
182
183
  export { sendCopy } from './methods/messages/send-copy.js';
@@ -185,9 +186,9 @@ export { sendMediaGroup } from './methods/messages/send-media-group.js';
185
186
  export { sendMedia } from './methods/messages/send-media.js';
186
187
  export { sendPaidReaction } from './methods/messages/send-paid-reaction.js';
187
188
  export { quoteWithText } from './methods/messages/send-quote.js';
188
- export type { QuoteParamsFrom } from './methods/messages/send-quote.js';
189
189
  export { quoteWithMedia } from './methods/messages/send-quote.js';
190
190
  export { quoteWithMediaGroup } from './methods/messages/send-quote.js';
191
+ export type { QuoteParamsFrom } from './methods/messages/send-quote.js';
191
192
  export { sendReaction } from './methods/messages/send-reaction.js';
192
193
  export { replyText } from './methods/messages/send-reply.js';
193
194
  export { replyMedia } from './methods/messages/send-reply.js';
@@ -213,6 +214,7 @@ export { removeCloudPassword } from './methods/password/remove-cloud-password.js
213
214
  export { _normalizeInputStarGift } from './methods/premium/_normalize-input-star-gift.js';
214
215
  export { acceptStarGift } from './methods/premium/accept-star-gift.js';
215
216
  export { applyBoost } from './methods/premium/apply-boost.js';
217
+ export { buyResaleGift } from './methods/premium/buy-resale-gift.js';
216
218
  export { canApplyBoost } from './methods/premium/can-apply-boost.js';
217
219
  export type { CanApplyBoostResult } from './methods/premium/can-apply-boost.js';
218
220
  export { createBusinessChatLink } from './methods/premium/create-business-chat-link.js';
@@ -223,6 +225,8 @@ export { getBoosts } from './methods/premium/get-boosts.js';
223
225
  export { getBusinessChatLinks } from './methods/premium/get-business-chat-links.js';
224
226
  export { getBusinessConnection } from './methods/premium/get-business-connection.js';
225
227
  export { getMyBoostSlots } from './methods/premium/get-my-boost-slots.js';
228
+ export { getResaleOptions } from './methods/premium/get-resale-star-gifts.js';
229
+ export type { InputStarGiftAttributeIds, ResaleStarGiftsMeta } from './methods/premium/get-resale-star-gifts.js';
226
230
  export { getSavedStarGiftsById } from './methods/premium/get-saved-star-gifts-by-id.js';
227
231
  export { getSavedStarGifts } from './methods/premium/get-saved-star-gifts.js';
228
232
  export { getStarGiftOptions } from './methods/premium/get-star-gift-options.js';
@@ -236,6 +240,7 @@ export { togglePinnedStarGifts } from './methods/premium/pin-star-gift.js';
236
240
  export { sendStarGift } from './methods/premium/send-star-gift.js';
237
241
  export { setBusinessIntro } from './methods/premium/set-business-intro.js';
238
242
  export { setBusinessWorkHours } from './methods/premium/set-business-work-hours.js';
243
+ export { setResaleStarGiftPrice } from './methods/premium/set-gift-resale-price.js';
239
244
  export { transferStarGift } from './methods/premium/transfer-star-gift.js';
240
245
  export { upgradeStarGift } from './methods/premium/upgrade-star-gift.js';
241
246
  export { addStickerToSet } from './methods/stickers/add-sticker-to-set.js';
@@ -289,6 +294,7 @@ export { resolvePeerMany } from './methods/users/resolve-peer-many.js';
289
294
  export { resolvePeer } from './methods/users/resolve-peer.js';
290
295
  export { resolveUser } from './methods/users/resolve-peer.js';
291
296
  export { resolveChannel } from './methods/users/resolve-peer.js';
297
+ export { resolvePhoneNumber } from './methods/users/resolve-phone-number.js';
292
298
  export { setMyEmojiStatus } from './methods/users/set-emoji-status.js';
293
299
  export { setEmojiStatus } from './methods/users/set-emoji-status.js';
294
300
  export { setGlobalTtl } from './methods/users/set-global-ttl.js';