teleproto 1.214.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 (222) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +37 -0
  3. package/Helpers.d.ts +150 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +14 -0
  7. package/Password.js +270 -0
  8. package/README.md +98 -0
  9. package/Utils.d.ts +227 -0
  10. package/Utils.js +1247 -0
  11. package/Version.d.ts +1 -0
  12. package/Version.js +4 -0
  13. package/client/2fa.d.ts +48 -0
  14. package/client/2fa.js +108 -0
  15. package/client/TelegramClient.d.ts +1043 -0
  16. package/client/TelegramClient.js +1181 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +92 -0
  20. package/client/auth.js +388 -0
  21. package/client/bots.d.ts +6 -0
  22. package/client/bots.js +23 -0
  23. package/client/buttons.d.ts +4 -0
  24. package/client/buttons.js +86 -0
  25. package/client/chats.d.ts +45 -0
  26. package/client/chats.js +350 -0
  27. package/client/dialogs.d.ts +52 -0
  28. package/client/dialogs.js +174 -0
  29. package/client/downloads.d.ts +156 -0
  30. package/client/downloads.js +614 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +17 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +64 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +263 -0
  38. package/client/messages.js +813 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +37 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +237 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +33 -0
  46. package/client/updates.js +260 -0
  47. package/client/uploads.d.ts +136 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +490 -0
  51. package/crypto/AuthKey.d.ts +19 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +5 -0
  54. package/crypto/CTR.js +16 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +11 -0
  58. package/crypto/IGE.js +115 -0
  59. package/crypto/RSA.d.ts +14 -0
  60. package/crypto/RSA.js +70 -0
  61. package/crypto/converters.d.ts +19 -0
  62. package/crypto/converters.js +52 -0
  63. package/crypto/crypto.d.ts +19 -0
  64. package/crypto/crypto.js +58 -0
  65. package/crypto/words.d.ts +6 -0
  66. package/crypto/words.js +47 -0
  67. package/define.d.ts +77 -0
  68. package/entityCache.d.ts +7 -0
  69. package/entityCache.js +79 -0
  70. package/errors/Common.d.ts +71 -0
  71. package/errors/Common.js +122 -0
  72. package/errors/RPCBaseErrors.d.ts +84 -0
  73. package/errors/RPCBaseErrors.js +134 -0
  74. package/errors/RPCErrorList.d.ts +37 -0
  75. package/errors/RPCErrorList.js +117 -0
  76. package/errors/index.d.ts +11 -0
  77. package/errors/index.js +32 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +73 -0
  81. package/events/CallbackQuery.js +193 -0
  82. package/events/DeletedMessage.d.ts +45 -0
  83. package/events/DeletedMessage.js +63 -0
  84. package/events/EditedMessage.d.ts +30 -0
  85. package/events/EditedMessage.js +41 -0
  86. package/events/NewMessage.d.ts +89 -0
  87. package/events/NewMessage.js +182 -0
  88. package/events/Raw.d.ts +29 -0
  89. package/events/Raw.js +43 -0
  90. package/events/common.d.ts +66 -0
  91. package/events/common.js +141 -0
  92. package/events/index.d.ts +2 -0
  93. package/events/index.js +8 -0
  94. package/extensions/AsyncQueue.d.ts +10 -0
  95. package/extensions/AsyncQueue.js +32 -0
  96. package/extensions/BinaryReader.d.ts +99 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +6 -0
  99. package/extensions/BinaryWriter.js +15 -0
  100. package/extensions/Deferred.d.ts +8 -0
  101. package/extensions/Deferred.js +16 -0
  102. package/extensions/Logger.d.ts +54 -0
  103. package/extensions/Logger.js +128 -0
  104. package/extensions/MessagePacker.d.ts +21 -0
  105. package/extensions/MessagePacker.js +154 -0
  106. package/extensions/PendingState.d.ts +12 -0
  107. package/extensions/PendingState.js +29 -0
  108. package/extensions/PromisedNetSockets.d.ts +24 -0
  109. package/extensions/PromisedNetSockets.js +192 -0
  110. package/extensions/PromisedWebSockets.d.ts +18 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +228 -0
  114. package/extensions/index.d.ts +7 -0
  115. package/extensions/index.js +17 -0
  116. package/extensions/markdown.d.ts +5 -0
  117. package/extensions/markdown.js +76 -0
  118. package/extensions/markdownv2.d.ts +5 -0
  119. package/extensions/markdownv2.js +51 -0
  120. package/extensions/net.d.ts +1 -0
  121. package/extensions/net.js +17 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +17 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +62 -0
  126. package/inspect.d.ts +1 -0
  127. package/inspect.js +5 -0
  128. package/network/Authenticator.d.ts +12 -0
  129. package/network/Authenticator.js +192 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +290 -0
  133. package/network/MTProtoSender.js +873 -0
  134. package/network/MTProtoState.d.ts +102 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +18 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +69 -0
  139. package/network/connection/Connection.js +162 -0
  140. package/network/connection/TCPAbridged.d.ts +19 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +16 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +49 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +18 -0
  147. package/network/connection/TCPObfuscated.js +78 -0
  148. package/network/connection/index.d.ts +4 -0
  149. package/network/connection/index.js +11 -0
  150. package/network/index.d.ts +11 -0
  151. package/network/index.js +23 -0
  152. package/package.json +50 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +109 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/Memory.d.ts +38 -0
  158. package/sessions/Memory.js +272 -0
  159. package/sessions/StoreSession.d.ts +14 -0
  160. package/sessions/StoreSession.js +77 -0
  161. package/sessions/StringSession.d.ts +32 -0
  162. package/sessions/StringSession.js +116 -0
  163. package/sessions/index.d.ts +4 -0
  164. package/sessions/index.js +11 -0
  165. package/sessions/localStorage.d.ts +1 -0
  166. package/sessions/localStorage.js +4 -0
  167. package/tl/AllTLObjects.d.ts +3 -0
  168. package/tl/AllTLObjects.js +17 -0
  169. package/tl/MTProtoRequest.d.ts +18 -0
  170. package/tl/MTProtoRequest.js +38 -0
  171. package/tl/api.d.ts +32488 -0
  172. package/tl/api.js +507 -0
  173. package/tl/apiTl.d.ts +2 -0
  174. package/tl/apiTl.js +2209 -0
  175. package/tl/core/GZIPPacked.d.ts +15 -0
  176. package/tl/core/GZIPPacked.js +51 -0
  177. package/tl/core/MessageContainer.d.ts +12 -0
  178. package/tl/core/MessageContainer.js +42 -0
  179. package/tl/core/RPCResult.d.ts +14 -0
  180. package/tl/core/RPCResult.js +32 -0
  181. package/tl/core/TLMessage.d.ts +10 -0
  182. package/tl/core/TLMessage.js +14 -0
  183. package/tl/core/index.d.ts +6 -0
  184. package/tl/core/index.js +16 -0
  185. package/tl/custom/button.d.ts +24 -0
  186. package/tl/custom/button.js +78 -0
  187. package/tl/custom/chatGetter.d.ts +29 -0
  188. package/tl/custom/chatGetter.js +116 -0
  189. package/tl/custom/dialog.d.ts +30 -0
  190. package/tl/custom/dialog.js +40 -0
  191. package/tl/custom/draft.d.ts +21 -0
  192. package/tl/custom/draft.js +48 -0
  193. package/tl/custom/file.d.ts +21 -0
  194. package/tl/custom/file.js +68 -0
  195. package/tl/custom/forward.d.ts +15 -0
  196. package/tl/custom/forward.js +47 -0
  197. package/tl/custom/index.d.ts +1 -0
  198. package/tl/custom/index.js +5 -0
  199. package/tl/custom/inlineResult.d.ts +32 -0
  200. package/tl/custom/inlineResult.js +87 -0
  201. package/tl/custom/inlineResults.d.ts +20 -0
  202. package/tl/custom/inlineResults.js +26 -0
  203. package/tl/custom/message.d.ts +427 -0
  204. package/tl/custom/message.js +716 -0
  205. package/tl/custom/messageButton.d.ts +54 -0
  206. package/tl/custom/messageButton.js +152 -0
  207. package/tl/custom/senderGetter.d.ts +28 -0
  208. package/tl/custom/senderGetter.js +55 -0
  209. package/tl/generateModule.d.ts +1 -0
  210. package/tl/generateModule.js +17 -0
  211. package/tl/generationHelpers.d.ts +11 -0
  212. package/tl/generationHelpers.js +289 -0
  213. package/tl/index.d.ts +3 -0
  214. package/tl/index.js +10 -0
  215. package/tl/patched/index.d.ts +2 -0
  216. package/tl/patched/index.js +76 -0
  217. package/tl/schemaTl.d.ts +2 -0
  218. package/tl/schemaTl.js +64 -0
  219. package/tl/types-generator/generate.d.ts +1 -0
  220. package/tl/types-generator/generate.js +84 -0
  221. package/tl/types-generator/template.d.ts +6 -0
  222. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,350 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports._ParticipantsIter = void 0;
7
+ exports.iterParticipants = iterParticipants;
8
+ exports.getParticipants = getParticipants;
9
+ exports.kickParticipant = kickParticipant;
10
+ const Helpers_1 = require("../Helpers");
11
+ const requestIter_1 = require("../requestIter");
12
+ const __1 = require("..");
13
+ const tl_1 = require("../tl");
14
+ const big_integer_1 = __importDefault(require("big-integer"));
15
+ const inspect_1 = require("../inspect");
16
+ const Utils_1 = require("../Utils");
17
+ const _MAX_PARTICIPANTS_CHUNK_SIZE = 200;
18
+ const _MAX_ADMIN_LOG_CHUNK_SIZE = 100;
19
+ const _MAX_PROFILE_PHOTO_CHUNK_SIZE = 100;
20
+ class _ChatAction {
21
+ [inspect_1.inspect.custom]() {
22
+ return (0, Helpers_1.betterConsoleLog)(this);
23
+ }
24
+ constructor(client, chat, action, params = {
25
+ delay: 4,
26
+ autoCancel: true,
27
+ }) {
28
+ this._client = client;
29
+ this._chat = chat;
30
+ this._action = action;
31
+ this._delay = params.delay;
32
+ this.autoCancel = params.autoCancel;
33
+ this._request = undefined;
34
+ this._task = null;
35
+ this._running = false;
36
+ }
37
+ async start() {
38
+ this._request = new tl_1.Api.messages.SetTyping({
39
+ peer: this._chat,
40
+ action: this._action,
41
+ });
42
+ this._running = true;
43
+ this._update();
44
+ }
45
+ async stop() {
46
+ this._running = false;
47
+ if (this.autoCancel) {
48
+ await this._client.invoke(new tl_1.Api.messages.SetTyping({
49
+ peer: this._chat,
50
+ action: new tl_1.Api.SendMessageCancelAction(),
51
+ }));
52
+ }
53
+ }
54
+ async _update() {
55
+ while (this._running) {
56
+ if (this._request != undefined) {
57
+ await this._client.invoke(this._request);
58
+ }
59
+ await (0, Helpers_1.sleep)(this._delay * 1000);
60
+ }
61
+ }
62
+ progress(current, total) {
63
+ if ("progress" in this._action) {
64
+ this._action.progress = 100 * Math.round(current / total);
65
+ }
66
+ }
67
+ }
68
+ _ChatAction._str_mapping = {
69
+ typing: new tl_1.Api.SendMessageTypingAction(),
70
+ contact: new tl_1.Api.SendMessageChooseContactAction(),
71
+ game: new tl_1.Api.SendMessageGamePlayAction(),
72
+ location: new tl_1.Api.SendMessageGeoLocationAction(),
73
+ "record-audio": new tl_1.Api.SendMessageRecordAudioAction(),
74
+ "record-voice": new tl_1.Api.SendMessageRecordAudioAction(), //alias
75
+ "record-round": new tl_1.Api.SendMessageRecordRoundAction(),
76
+ "record-video": new tl_1.Api.SendMessageRecordVideoAction(),
77
+ audio: new tl_1.Api.SendMessageUploadAudioAction({ progress: 1 }),
78
+ voice: new tl_1.Api.SendMessageUploadAudioAction({ progress: 1 }), // alias
79
+ song: new tl_1.Api.SendMessageUploadAudioAction({ progress: 1 }), // alias
80
+ round: new tl_1.Api.SendMessageUploadRoundAction({ progress: 1 }),
81
+ video: new tl_1.Api.SendMessageUploadVideoAction({ progress: 1 }),
82
+ photo: new tl_1.Api.SendMessageUploadPhotoAction({ progress: 1 }),
83
+ document: new tl_1.Api.SendMessageUploadDocumentAction({ progress: 1 }),
84
+ file: new tl_1.Api.SendMessageUploadDocumentAction({ progress: 1 }), // alias
85
+ cancel: new tl_1.Api.SendMessageCancelAction(),
86
+ };
87
+ class _ParticipantsIter extends requestIter_1.RequestIter {
88
+ [inspect_1.inspect.custom]() {
89
+ return (0, Helpers_1.betterConsoleLog)(this);
90
+ }
91
+ async _init({ entity, filter, offset, search, showTotal, }) {
92
+ var _a, _b;
93
+ if (!offset) {
94
+ offset = 0;
95
+ }
96
+ if (filter && filter.constructor === Function) {
97
+ if ([
98
+ tl_1.Api.ChannelParticipantsBanned,
99
+ tl_1.Api.ChannelParticipantsKicked,
100
+ tl_1.Api.ChannelParticipantsSearch,
101
+ tl_1.Api.ChannelParticipantsContacts,
102
+ ].includes(filter)) {
103
+ filter = new filter({
104
+ q: "",
105
+ });
106
+ }
107
+ else {
108
+ filter = new filter();
109
+ }
110
+ }
111
+ entity = await this.client.getInputEntity(entity);
112
+ const ty = __1.helpers._entityType(entity);
113
+ if (search && (filter || ty != __1.helpers._EntityType.CHANNEL)) {
114
+ // We need to 'search' ourselves unless we have a PeerChannel
115
+ search = search.toLowerCase();
116
+ this.filterEntity = (entity) => {
117
+ return (__1.utils
118
+ .getDisplayName(entity)
119
+ .toLowerCase()
120
+ .includes(search) ||
121
+ ("username" in entity ? entity.username || "" : "")
122
+ .toLowerCase()
123
+ .includes(search));
124
+ };
125
+ }
126
+ else {
127
+ this.filterEntity = (entity) => true;
128
+ }
129
+ // Only used for channels, but we should always set the attribute
130
+ this.requests = [];
131
+ if (ty == __1.helpers._EntityType.CHANNEL) {
132
+ if (showTotal) {
133
+ const channel = await this.client.invoke(new tl_1.Api.channels.GetFullChannel({
134
+ channel: entity,
135
+ }));
136
+ if (!(channel.fullChat instanceof tl_1.Api.ChatFull)) {
137
+ this.total = channel.fullChat.participantsCount;
138
+ }
139
+ }
140
+ if (this.total && this.total <= 0) {
141
+ return false;
142
+ }
143
+ this.requests.push(new tl_1.Api.channels.GetParticipants({
144
+ channel: entity,
145
+ filter: filter ||
146
+ new tl_1.Api.ChannelParticipantsSearch({
147
+ q: search || "",
148
+ }),
149
+ offset,
150
+ limit: _MAX_PARTICIPANTS_CHUNK_SIZE,
151
+ hash: big_integer_1.default.zero,
152
+ }));
153
+ }
154
+ else if (ty == __1.helpers._EntityType.CHAT) {
155
+ if (!("chatId" in entity)) {
156
+ throw new Error("Found chat without id " + JSON.stringify(entity));
157
+ }
158
+ const full = await this.client.invoke(new tl_1.Api.messages.GetFullChat({
159
+ chatId: entity.chatId,
160
+ }));
161
+ if (full.fullChat instanceof tl_1.Api.ChatFull) {
162
+ if (!(full.fullChat.participants instanceof
163
+ tl_1.Api.ChatParticipantsForbidden)) {
164
+ this.total = full.fullChat.participants.participants.length;
165
+ }
166
+ else {
167
+ this.total = 0;
168
+ return false;
169
+ }
170
+ const users = new Map();
171
+ for (const user of full.users) {
172
+ users.set(user.id.toString(), user);
173
+ }
174
+ for (const participant of full.fullChat.participants
175
+ .participants) {
176
+ const user = users.get(participant.userId.toString());
177
+ if (!this.filterEntity(user)) {
178
+ continue;
179
+ }
180
+ user.participant = participant;
181
+ (_a = this.buffer) === null || _a === void 0 ? void 0 : _a.push(user);
182
+ }
183
+ return true;
184
+ }
185
+ }
186
+ else {
187
+ this.total = 1;
188
+ if (this.limit != 0) {
189
+ const user = await this.client.getEntity(entity);
190
+ if (this.filterEntity(user)) {
191
+ user.participant = undefined;
192
+ (_b = this.buffer) === null || _b === void 0 ? void 0 : _b.push(user);
193
+ }
194
+ }
195
+ return true;
196
+ }
197
+ }
198
+ async _loadNextChunk() {
199
+ var _a, _b;
200
+ if (!((_a = this.requests) === null || _a === void 0 ? void 0 : _a.length)) {
201
+ return true;
202
+ }
203
+ this.requests[0].limit = Math.min(this.limit - this.requests[0].offset, _MAX_PARTICIPANTS_CHUNK_SIZE);
204
+ const results = [];
205
+ for (const request of this.requests) {
206
+ results.push(await this.client.invoke(request));
207
+ }
208
+ for (let i = this.requests.length - 1; i >= 0; i--) {
209
+ const participants = results[i];
210
+ if (participants instanceof
211
+ tl_1.Api.channels.ChannelParticipantsNotModified ||
212
+ !participants.users.length) {
213
+ this.requests.splice(i, 1);
214
+ continue;
215
+ }
216
+ this.requests[i].offset += participants.participants.length;
217
+ const users = new Map();
218
+ for (const user of participants.users) {
219
+ users.set(user.id.toString(), user);
220
+ }
221
+ for (const participant of participants.participants) {
222
+ if (!("userId" in participant)) {
223
+ continue;
224
+ }
225
+ const user = users.get(participant.userId.toString());
226
+ if (this.filterEntity && !this.filterEntity(user)) {
227
+ continue;
228
+ }
229
+ user.participant = participant;
230
+ (_b = this.buffer) === null || _b === void 0 ? void 0 : _b.push(user);
231
+ }
232
+ }
233
+ return undefined;
234
+ }
235
+ [Symbol.asyncIterator]() {
236
+ return super[Symbol.asyncIterator]();
237
+ }
238
+ }
239
+ exports._ParticipantsIter = _ParticipantsIter;
240
+ class _AdminLogIter extends requestIter_1.RequestIter {
241
+ [inspect_1.inspect.custom]() {
242
+ return (0, Helpers_1.betterConsoleLog)(this);
243
+ }
244
+ async _init(entity, searchArgs, filterArgs) {
245
+ let eventsFilter = undefined;
246
+ if (filterArgs &&
247
+ Object.values(filterArgs).find((element) => element === true)) {
248
+ eventsFilter = new tl_1.Api.ChannelAdminLogEventsFilter(Object.assign({}, filterArgs));
249
+ }
250
+ this.entity = await this.client.getInputEntity(entity);
251
+ const adminList = [];
252
+ if (searchArgs && searchArgs.admins) {
253
+ for (const admin of searchArgs.admins) {
254
+ adminList.push(await this.client.getInputEntity(admin));
255
+ }
256
+ }
257
+ this.request = new tl_1.Api.channels.GetAdminLog({
258
+ channel: this.entity,
259
+ q: (searchArgs === null || searchArgs === void 0 ? void 0 : searchArgs.search) || "",
260
+ minId: searchArgs === null || searchArgs === void 0 ? void 0 : searchArgs.minId,
261
+ maxId: searchArgs === null || searchArgs === void 0 ? void 0 : searchArgs.maxId,
262
+ limit: 0,
263
+ eventsFilter: eventsFilter,
264
+ admins: adminList || undefined,
265
+ });
266
+ }
267
+ async _loadNextChunk() {
268
+ if (!this.request) {
269
+ return true;
270
+ }
271
+ this.request.limit = Math.min(this.left, _MAX_ADMIN_LOG_CHUNK_SIZE);
272
+ const r = await this.client.invoke(this.request);
273
+ const entities = new Map();
274
+ for (const entity of [...r.users, ...r.chats]) {
275
+ entities.set(__1.utils.getPeerId(entity), entity);
276
+ }
277
+ const eventIds = [];
278
+ for (const e of r.events) {
279
+ eventIds.push(e.id);
280
+ }
281
+ this.request.maxId = (0, Helpers_1.getMinBigInt)([big_integer_1.default.zero, ...eventIds]);
282
+ for (const ev of r.events) {
283
+ if (ev.action instanceof tl_1.Api.ChannelAdminLogEventActionEditMessage) {
284
+ // @ts-ignore
285
+ // TODO ev.action.prevMessage._finishInit(this.client, entities, this.entity);
286
+ // @ts-ignore
287
+ // TODO ev.action.newMessage._finishInit(this.client, entities, this.entity);
288
+ }
289
+ }
290
+ }
291
+ }
292
+ /** @hidden */
293
+ function iterParticipants(client, entity, { limit, offset, search, filter, showTotal = true }) {
294
+ return new _ParticipantsIter(client, limit !== null && limit !== void 0 ? limit : Number.MAX_SAFE_INTEGER, {}, {
295
+ entity: entity,
296
+ filter: filter,
297
+ offset: offset !== null && offset !== void 0 ? offset : 0,
298
+ search: search,
299
+ showTotal: showTotal,
300
+ });
301
+ }
302
+ /** @hidden */
303
+ async function getParticipants(client, entity, params) {
304
+ const it = client.iterParticipants(entity, params);
305
+ return (await it.collect());
306
+ }
307
+ /** @hidden */
308
+ async function kickParticipant(client, entity, participant) {
309
+ const peer = await client.getInputEntity(entity);
310
+ const user = await client.getInputEntity(participant);
311
+ let resp;
312
+ let request;
313
+ const type = __1.helpers._entityType(peer);
314
+ if (type === __1.helpers._EntityType.CHAT) {
315
+ request = new tl_1.Api.messages.DeleteChatUser({
316
+ chatId: (0, Helpers_1.returnBigInt)((0, Utils_1.getPeerId)(entity)),
317
+ userId: (0, Helpers_1.returnBigInt)((0, Utils_1.getPeerId)(participant)),
318
+ });
319
+ resp = await client.invoke(request);
320
+ }
321
+ else if (type === __1.helpers._EntityType.CHANNEL) {
322
+ if (user instanceof tl_1.Api.InputPeerSelf) {
323
+ request = new tl_1.Api.channels.LeaveChannel({
324
+ channel: peer,
325
+ });
326
+ resp = await client.invoke(request);
327
+ }
328
+ else {
329
+ request = new tl_1.Api.channels.EditBanned({
330
+ channel: peer,
331
+ participant: user,
332
+ bannedRights: new tl_1.Api.ChatBannedRights({
333
+ untilDate: 0,
334
+ viewMessages: true,
335
+ }),
336
+ });
337
+ resp = await client.invoke(request);
338
+ await (0, Helpers_1.sleep)(500);
339
+ await client.invoke(new tl_1.Api.channels.EditBanned({
340
+ channel: peer,
341
+ participant: user,
342
+ bannedRights: new tl_1.Api.ChatBannedRights({ untilDate: 0 }),
343
+ }));
344
+ }
345
+ }
346
+ else {
347
+ throw new Error("You must pass either a channel or a chat");
348
+ }
349
+ return client._getResponseMessage(request, resp, entity);
350
+ }
@@ -0,0 +1,52 @@
1
+ import { Api } from "../tl";
2
+ import { RequestIter } from "../requestIter";
3
+ import { TelegramClient } from "../index";
4
+ import { Dialog } from "../tl/custom/dialog";
5
+ import { DateLike, EntityLike } from "../define";
6
+ import { TotalList } from "../Helpers";
7
+ export interface DialogsIterInterface {
8
+ offsetDate: number;
9
+ offsetId: number;
10
+ offsetPeer: Api.TypePeer;
11
+ ignorePinned: boolean;
12
+ ignoreMigrated: boolean;
13
+ folder: number;
14
+ }
15
+ export declare class _DialogsIter extends RequestIter {
16
+ private request?;
17
+ private seen?;
18
+ private offsetDate?;
19
+ private ignoreMigrated?;
20
+ _init({ offsetDate, offsetId, offsetPeer, ignorePinned, ignoreMigrated, folder, }: DialogsIterInterface): Promise<true | undefined>;
21
+ [Symbol.asyncIterator](): AsyncIterator<Dialog, any, undefined>;
22
+ _loadNextChunk(): Promise<boolean | undefined>;
23
+ }
24
+ /** interface for iterating and getting dialogs. */
25
+ export interface IterDialogsParams {
26
+ /** How many dialogs to be retrieved as maximum. Can be set to undefined to retrieve all dialogs.<br/>
27
+ * Note that this may take whole minutes if you have hundreds of dialogs, as Telegram will tell the library to slow down through a FloodWaitError.*/
28
+ limit?: number;
29
+ /** The offset date of last message of dialog to be used. */
30
+ offsetDate?: DateLike;
31
+ /** The message ID to be used as offset. */
32
+ offsetId?: number;
33
+ /** offset Peer to be used (defaults to Empty = no offset) */
34
+ offsetPeer?: EntityLike;
35
+ /** Whether pinned dialogs should be ignored or not. When set to true, these won't be yielded at all. */
36
+ ignorePinned?: boolean;
37
+ /** Whether Chat that have migratedTo a Supergroup should be included or not.<br/>
38
+ * By default all the chats in your dialogs are returned, but setting this to True will ignore (i.e. skip) them in the same way official applications do.*/
39
+ ignoreMigrated?: boolean;
40
+ /** The folder from which the dialogs should be retrieved.<br/>
41
+ * If left unspecified, all dialogs (including those from folders) will be returned.<br/>
42
+ * If set to 0, all dialogs that don't belong to any folder will be returned.<br/>
43
+ * If set to a folder number like 1, only those from said folder will be returned.<br/>
44
+ * By default Telegram assigns the folder ID 1 to archived chats, so you should use that if you need to fetch the archived dialogs.<br/> */
45
+ folder?: number;
46
+ /** Alias for folder. If unspecified, all will be returned, false implies `folder:0` and True implies `folder:1`.*/
47
+ archived?: boolean;
48
+ }
49
+ /** @hidden */
50
+ export declare function iterDialogs(client: TelegramClient, { limit, offsetDate, offsetId, offsetPeer, ignorePinned, ignoreMigrated, folder, archived, }: IterDialogsParams): _DialogsIter;
51
+ /** @hidden */
52
+ export declare function getDialogs(client: TelegramClient, params: IterDialogsParams): Promise<TotalList<Dialog>>;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports._DialogsIter = void 0;
7
+ exports.iterDialogs = iterDialogs;
8
+ exports.getDialogs = getDialogs;
9
+ const tl_1 = require("../tl");
10
+ const requestIter_1 = require("../requestIter");
11
+ const index_1 = require("../index");
12
+ const dialog_1 = require("../tl/custom/dialog");
13
+ const big_integer_1 = __importDefault(require("big-integer"));
14
+ const Logger_1 = require("../extensions/Logger");
15
+ const _MAX_CHUNK_SIZE = 100;
16
+ /**
17
+ Get the key to get messages from a dialog.
18
+
19
+ We cannot just use the message ID because channels share message IDs,
20
+ and the peer ID is required to distinguish between them. But it is not
21
+ necessary in small group chats and private chats.
22
+ * @param {Api.TypePeer} [peer] the dialog peer
23
+ * @param {number} [messageId] the message id
24
+ * @return {[number,number]} the channel id and message id
25
+ */
26
+ function _dialogMessageKey(peer, messageId) {
27
+ // can't use arrays as keys for map :( need to convert to string.
28
+ return ("" +
29
+ [
30
+ peer instanceof tl_1.Api.PeerChannel ? peer.channelId : undefined,
31
+ messageId,
32
+ ]);
33
+ }
34
+ class _DialogsIter extends requestIter_1.RequestIter {
35
+ async _init({ offsetDate, offsetId, offsetPeer, ignorePinned, ignoreMigrated, folder, }) {
36
+ this.request = new tl_1.Api.messages.GetDialogs({
37
+ offsetDate,
38
+ offsetId,
39
+ offsetPeer,
40
+ limit: 1,
41
+ hash: big_integer_1.default.zero,
42
+ excludePinned: ignorePinned,
43
+ folderId: folder,
44
+ });
45
+ if (this.limit <= 0) {
46
+ // Special case, get a single dialog and determine count
47
+ const dialogs = await this.client.invoke(this.request);
48
+ if ("count" in dialogs) {
49
+ this.total = dialogs.count;
50
+ }
51
+ else {
52
+ this.total = dialogs.dialogs.length;
53
+ }
54
+ return true;
55
+ }
56
+ this.seen = new Set();
57
+ this.offsetDate = offsetDate;
58
+ this.ignoreMigrated = ignoreMigrated;
59
+ }
60
+ [Symbol.asyncIterator]() {
61
+ return super[Symbol.asyncIterator]();
62
+ }
63
+ async _loadNextChunk() {
64
+ var _a;
65
+ if (!this.request || !this.seen || !this.buffer) {
66
+ return;
67
+ }
68
+ this.request.limit = Math.min(this.left, _MAX_CHUNK_SIZE);
69
+ const r = await this.client.invoke(this.request);
70
+ if (r instanceof tl_1.Api.messages.DialogsNotModified) {
71
+ return;
72
+ }
73
+ if ("count" in r) {
74
+ this.total = r.count;
75
+ }
76
+ else {
77
+ this.total = r.dialogs.length;
78
+ }
79
+ const entities = new Map();
80
+ const messages = new Map();
81
+ for (const entity of [...r.users, ...r.chats]) {
82
+ if (entity instanceof tl_1.Api.UserEmpty ||
83
+ entity instanceof tl_1.Api.ChatEmpty) {
84
+ continue;
85
+ }
86
+ entities.set(index_1.utils.getPeerId(entity), entity);
87
+ }
88
+ for (const m of r.messages) {
89
+ let message = m;
90
+ try {
91
+ if (message && "_finishInit" in message) {
92
+ // todo make sure this never fails
93
+ message._finishInit(this.client, entities, undefined);
94
+ }
95
+ }
96
+ catch (e) {
97
+ console.log("msg", message);
98
+ this.client._log.error("Got error while trying to finish init message with id " +
99
+ m.id);
100
+ if (this.client._log.canSend(Logger_1.LogLevel.ERROR)) {
101
+ console.error(e);
102
+ }
103
+ if (this.client._errorHandler) {
104
+ await this.client._errorHandler(e);
105
+ }
106
+ }
107
+ messages.set(_dialogMessageKey(message.peerId, message.id), message);
108
+ }
109
+ for (const d of r.dialogs) {
110
+ if (d instanceof tl_1.Api.DialogFolder) {
111
+ continue;
112
+ }
113
+ const message = messages.get(_dialogMessageKey(d.peer, d.topMessage));
114
+ if (this.offsetDate != undefined) {
115
+ const date = message === null || message === void 0 ? void 0 : message.date;
116
+ if (date == undefined || date > this.offsetDate) {
117
+ continue;
118
+ }
119
+ }
120
+ const peerId = index_1.utils.getPeerId(d.peer);
121
+ if (!this.seen.has(peerId)) {
122
+ this.seen.add(peerId);
123
+ if (!entities.has(peerId)) {
124
+ /*
125
+ > In which case can a UserEmpty appear in the list of banned members?
126
+ > In a very rare cases. This is possible but isn't an expected behavior.
127
+ Real world example: https://t.me/TelethonChat/271471
128
+ */
129
+ continue;
130
+ }
131
+ const cd = new dialog_1.Dialog(this.client, d, entities, message);
132
+ if (!this.ignoreMigrated ||
133
+ (cd.entity != undefined && "migratedTo" in cd.entity)) {
134
+ this.buffer.push(cd);
135
+ }
136
+ }
137
+ }
138
+ if (r.dialogs.length < this.request.limit ||
139
+ !(r instanceof tl_1.Api.messages.DialogsSlice)) {
140
+ return true;
141
+ }
142
+ let lastMessage;
143
+ for (let dialog of r.dialogs.reverse()) {
144
+ lastMessage = messages.get(_dialogMessageKey(dialog.peer, dialog.topMessage));
145
+ if (lastMessage) {
146
+ break;
147
+ }
148
+ }
149
+ this.request.excludePinned = true;
150
+ this.request.offsetId = lastMessage ? lastMessage.id : 0;
151
+ this.request.offsetDate = lastMessage ? lastMessage.date : 0;
152
+ this.request.offsetPeer =
153
+ (_a = this.buffer[this.buffer.length - 1]) === null || _a === void 0 ? void 0 : _a.inputEntity;
154
+ }
155
+ }
156
+ exports._DialogsIter = _DialogsIter;
157
+ /** @hidden */
158
+ function iterDialogs(client, { limit = undefined, offsetDate = undefined, offsetId = 0, offsetPeer = new tl_1.Api.InputPeerEmpty(), ignorePinned = false, ignoreMigrated = false, folder = undefined, archived = undefined, }) {
159
+ if (archived != undefined) {
160
+ folder = archived ? 1 : 0;
161
+ }
162
+ return new _DialogsIter(client, limit, {}, {
163
+ offsetDate,
164
+ offsetId,
165
+ offsetPeer,
166
+ ignorePinned,
167
+ ignoreMigrated,
168
+ folder,
169
+ });
170
+ }
171
+ /** @hidden */
172
+ async function getDialogs(client, params) {
173
+ return (await client.iterDialogs(params).collect());
174
+ }