Răsfoiți Sursa

add getting participants friendly method

painor 4 ani în urmă
părinte
comite
348c8c1527

+ 13 - 0
gramjs/client/TelegramClient.ts

@@ -9,6 +9,7 @@ import * as messageMethods from "./messages";
 import * as updateMethods from "./updates";
 import * as uploadMethods from "./uploads";
 import * as userMethods from "./users";
+import * as chatMethods from "./chats";
 import type {ButtonLike, EntityLike, MarkupLike} from "../define";
 import {Api} from "../tl";
 import {sanitizeParseMode} from "../Utils";
@@ -153,6 +154,18 @@ export class TelegramClient extends TelegramBaseClient {
     }
 
     //endregion
+
+    //region chats
+    iterParticipants(entity: EntityLike, params: chatMethods.IterParticipantsParams) {
+        return chatMethods.iterParticipants(this, entity, params);
+    }
+    getParticipants(entity: EntityLike, params: chatMethods.IterParticipantsParams) {
+        return chatMethods.getParticipants(this, entity, params);
+    }
+
+    //endregion
+
+
     //region updates
     on(event: any) {
         return updateMethods.on(this, event);

+ 36 - 7
gramjs/client/chats.ts

@@ -1,11 +1,12 @@
 import type{TelegramClient} from "./TelegramClient";
 import type{EntitiesLike, Entity, EntityLike, ValueOf} from "../define";
-import {sleep, getMinBigInt} from '../Helpers';
+import {sleep, getMinBigInt, TotalList} from '../Helpers';
 import {RequestIter} from "../requestIter";
 import {helpers, utils} from "../";
 import {Api} from "../tl";
 import bigInt, {BigInteger} from "big-integer";
 
+
 const _MAX_PARTICIPANTS_CHUNK_SIZE = 200;
 const _MAX_ADMIN_LOG_CHUNK_SIZE = 100;
 const _MAX_PROFILE_PHOTO_CHUNK_SIZE = 100;
@@ -106,12 +107,12 @@ interface ParticipantsIterInterface {
     search?: string
 }
 
-class _ParticipantsIter extends RequestIter {
+export class _ParticipantsIter extends RequestIter {
     private filterEntity: ((entity: Entity) => boolean) | undefined;
     private requests?: Api.channels.GetParticipants[];
 
     async _init({entity, filter, search}: ParticipantsIterInterface): Promise<boolean | void> {
-        if (filter.constructor === Function) {
+        if (filter && filter.constructor === Function) {
             if ([Api.ChannelParticipantsBanned, Api.ChannelParticipantsKicked, Api.ChannelParticipantsSearch, Api.ChannelParticipantsContacts].includes(filter)) {
                 filter = new filter({
                     q: '',
@@ -199,7 +200,7 @@ class _ParticipantsIter extends RequestIter {
     }
 
     async _loadNextChunk(): Promise<boolean | undefined> {
-        if (!this.requests) {
+        if (!this.requests?.length) {
             return true;
         }
         this.requests[0].limit = Math.min(
@@ -214,9 +215,10 @@ class _ParticipantsIter extends RequestIter {
                 await this.client.invoke(request)
             );
         }
-        for (let i = this.requests.length; i > 0; i--) {
+
+        for (let i = this.requests.length - 1; i >= 0; i--) {
             const participants = results[i];
-            if (participants instanceof Api.channels.ChannelParticipantsNotModified || !participants.users) {
+            if (participants instanceof Api.channels.ChannelParticipantsNotModified || !participants.users.length) {
                 this.requests.splice(i, 1);
                 continue;
             }
@@ -236,6 +238,11 @@ class _ParticipantsIter extends RequestIter {
         }
         return undefined;
     }
+
+    [Symbol.asyncIterator](): AsyncIterator<Api.User, any, undefined> {
+        return super[Symbol.asyncIterator]();
+    }
+
 }
 
 interface _AdminLogFilterInterface {
@@ -321,5 +328,27 @@ class _AdminLogIter extends RequestIter {
     }
 }
 
-// TODO implement
 
+export interface IterParticipantsParams {
+    limit?: number,
+    search?: string,
+    filter?: Api.TypeChannelParticipantsFilter,
+}
+
+export function iterParticipants(client: TelegramClient, entity: EntityLike, {
+    limit,
+    search,
+    filter,
+}: IterParticipantsParams) {
+    return new _ParticipantsIter(client, limit ?? Number.MAX_SAFE_INTEGER, {},
+        {
+            entity: entity,
+            filter: filter,
+            search: search
+        });
+}
+
+export async function getParticipants(client: TelegramClient, entity: EntityLike, params: IterParticipantsParams) {
+    const it = client.iterParticipants(entity, params);
+    return await it.collect() as TotalList<Api.User>;
+}

+ 6 - 0
gramjs/client/messages.ts

@@ -225,6 +225,9 @@ export class _MessagesIter extends RequestIter {
         }
         return true;
     }
+    [Symbol.asyncIterator](): AsyncIterator<Message, any, undefined> {
+        return super[Symbol.asyncIterator]();
+    }
 
     _updateOffset(lastMessage: Message, response: any) {
         if (!this.request){
@@ -270,6 +273,9 @@ export class _IDsIter extends RequestIter {
             this.waitTile = this.limit > 300 ? 10 : 0;
         }
     }
+    [Symbol.asyncIterator](): AsyncIterator<Message, any, undefined> {
+        return super[Symbol.asyncIterator]();
+    }
 
     async _loadNextChunk() {
         const ids = this._ids.slice(this._offset, this._offset + _MAX_CHUNK_SIZE);

+ 2 - 1
gramjs/requestIter.ts

@@ -79,7 +79,8 @@ export class RequestIter implements AsyncIterable<any> {
                         this.left = this.buffer.length;
                     }
                 }
-                if (!this.buffer) {
+
+                if (!this.buffer || !this.buffer.length) {
                     return {
                         value: undefined,
                         done: true,

+ 1 - 2
gramjs/tl/custom/message.ts

@@ -7,7 +7,6 @@ import * as utils from "../../Utils";
 import {Forward} from "./forward";
 import type {File} from "./file";
 import {Mixin} from "ts-mixer";
-import bigInt from "big-integer";
 
 interface MessageBaseInterface {
     id: any;
@@ -51,7 +50,7 @@ export class Message extends Mixin(SenderGetter, ChatGetter) {
     fromScheduled: any | undefined;
     legacy: any | undefined;
     editHide: any | undefined;
-    id: bigInt.BigInteger;
+    id: number;
     fromId?: EntityLike;
     peerId: any;
     fwdFrom: Api.TypeMessageFwdHeader;

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "telegram",
-  "version": "1.4.8",
+  "version": "1.5.1",
   "description": "NodeJS MTProto API Telegram client library,",
   "main": "index.js",
   "types": "index.d.ts",