Ver Fonte

Add toJSON method to raw types.
Make fileToMedia public
Fix attributes not being set when set files.

painor há 3 anos atrás
pai
commit
cbd6a803d2

+ 29 - 44
gramjs/Utils.ts

@@ -565,11 +565,7 @@ export function isAudio(file: any): boolean {
     const ext = _getExtension(file);
     if (!ext) {
         const metadata = _getMetadata(file);
-        if (metadata) {
-            return (metadata.get("mimeType") || "").startsWith("audio/");
-        } else {
-            return false;
-        }
+        return (metadata.get("mimeType") || "").startsWith("audio/");
     } else {
         file = "a" + ext;
         return (mime.lookup(file) || "").startsWith("audio/");
@@ -634,16 +630,16 @@ function _getExtension(file: any): string {
     }
 }
 
-function _getMetadata(file: any): Map<string, string> | undefined {
+function _getMetadata(file: any): Map<string, string> {
     //TODO Return nothing for now until we find a better way
-    return undefined;
+    return new Map<string, string>();
 }
 
 function isVideo(file: any): boolean {
     const ext = _getExtension(file);
     if (!ext) {
         const metadata = _getMetadata(file);
-        if (metadata && metadata.has("mimeType")) {
+        if (metadata.has("mimeType")) {
             return metadata.get("mimeType")?.startsWith("video/") || false;
         } else {
             return false;
@@ -684,51 +680,40 @@ export function getAttributes(
     );
     if (isAudio(file)) {
         const m = _getMetadata(file);
-        if (m) {
-            attrObj.set(
-                Api.DocumentAttributeAudio,
-                new Api.DocumentAttributeAudio({
-                    voice: voiceNote,
-                    title: m.has("title") ? m.get("title") : undefined,
-                    performer: m.has("author") ? m.get("author") : undefined,
-                    duration: Number.parseInt(m.get("duration") ?? "0"),
-                })
-            );
-        }
+        attrObj.set(
+            Api.DocumentAttributeAudio,
+            new Api.DocumentAttributeAudio({
+                voice: voiceNote,
+                title: m.has("title") ? m.get("title") : undefined,
+                performer: m.has("author") ? m.get("author") : undefined,
+                duration: Number.parseInt(m.get("duration") ?? "0"),
+            })
+        );
     }
     if (!forceDocument && isVideo(file)) {
         let doc;
-        const m = _getMetadata(file);
-        if (m) {
+        if (thumb) {
+            const t_m = _getMetadata(thumb);
+            const width = Number.parseInt(t_m?.get("width") || "1");
+            const height = Number.parseInt(t_m?.get("height") || "1");
             doc = new Api.DocumentAttributeVideo({
+                duration: 0,
+                h: height,
+                w: width,
                 roundMessage: videoNote,
-                w: Number.parseInt(m.get("width") ?? "0"),
-                h: Number.parseInt(m.get("height") ?? "0"),
-                duration: Number.parseInt(m.get("duration") ?? "0"),
                 supportsStreaming: supportsStreaming,
             });
         } else {
-            if (thumb) {
-                const t_m = _getMetadata(thumb);
-                const width = Number.parseInt(t_m?.get("width") || "1");
-                const height = Number.parseInt(t_m?.get("height") || "1");
-                doc = new Api.DocumentAttributeVideo({
-                    duration: 0,
-                    h: height,
-                    w: width,
-                    roundMessage: videoNote,
-                    supportsStreaming: supportsStreaming,
-                });
-            } else {
-                doc = new Api.DocumentAttributeVideo({
-                    duration: 0,
-                    h: 1,
-                    w: 1,
-                    roundMessage: videoNote,
-                    supportsStreaming: supportsStreaming,
-                });
-            }
+            const m = _getMetadata(file);
+            doc = new Api.DocumentAttributeVideo({
+                roundMessage: videoNote,
+                w: Number.parseInt(m.get("width") ?? "1"),
+                h: Number.parseInt(m.get("height") ?? "1"),
+                duration: Number.parseInt(m.get("duration") ?? "0"),
+                supportsStreaming: supportsStreaming,
+            });
         }
+
         attrObj.set(Api.DocumentAttributeVideo, doc);
     }
     if (videoNote) {

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "1.10.1";
+export const version = "1.10.4";

+ 16 - 5
gramjs/client/messages.ts

@@ -472,7 +472,7 @@ const IterMessagesDefaults: IterMessagesParams = {
     reverse: false,
     replyTo: undefined,
     scheduled: false,
-}
+};
 
 /**
  * Interface for sending a message. only message is required
@@ -574,8 +574,19 @@ export function iterMessages(
     options: Partial<IterMessagesParams>
 ) {
     const {
-        limit, offsetDate, offsetId, maxId, minId, addOffset, search,
-        filter, fromUser, waitTime, ids, reverse, replyTo,
+        limit,
+        offsetDate,
+        offsetId,
+        maxId,
+        minId,
+        addOffset,
+        search,
+        filter,
+        fromUser,
+        waitTime,
+        ids,
+        reverse,
+        replyTo,
     } = { ...IterMessagesDefaults, ...options };
     if (ids) {
         let idsArray;
@@ -735,7 +746,7 @@ export async function sendMessage(
         if (formattingEntities == undefined) {
             [message, formattingEntities] = await _parseMessageText(
                 client,
-                message || '',
+                message || "",
                 parseMode
             );
         }
@@ -782,7 +793,7 @@ export async function forwardMessages(
     { messages, fromPeer, silent, schedule }: ForwardMessagesParams
 ) {
     if (!isArrayLike(messages)) {
-        messages = [messages]
+        messages = [messages];
     }
     entity = await client.getInputEntity(entity);
     let fromPeerId: number | undefined;

+ 1 - 1
gramjs/client/uploads.ts

@@ -241,7 +241,7 @@ interface FileToMediaInterface {
 }
 
 /** @hidden */
-async function _fileToMedia(
+export async function _fileToMedia(
     client: TelegramClient,
     {
         file,

+ 1 - 1
gramjs/events/NewMessage.ts

@@ -176,7 +176,7 @@ export class NewMessage extends EventBuilder {
         }
     }
 
-    filter(event: NewMessageEvent): any {
+    filter(event: NewMessageEvent): EventCommon | undefined {
         if (this._noCheck) {
             return event;
         }

+ 5 - 2
gramjs/events/common.ts

@@ -120,11 +120,14 @@ export class EventBuilder {
         this.chats = await _intoIdSet(client, this.chats);
     }
 
-    filter(event: any): undefined | EventBuilder {
+    filter(event: EventCommon): undefined | EventCommon {
         if (!this.resolved) {
             return;
         }
-        if (this.chats != undefined && event.chatId != undefined) {
+        if (this.chats != undefined) {
+            if (event.chatId == undefined) {
+                return;
+            }
             const inside = this.chats.includes(event.chatId);
             if (inside == this.blacklistChats) {
                 // If this chat matches but it's a blacklist ignore.

+ 2 - 0
gramjs/tl/api.d.ts

@@ -31,6 +31,8 @@ export namespace Api {
         className: string;
         classType: "constructor" | "request";
         constructor(args: Args);
+        originalArgs: Args;
+        toJSON(): Args;
     }
     class Request<Args, Response> extends VirtualClass<Partial<Args>> {
         static readResult(reader: Reader): Buffer;

+ 5 - 0
gramjs/tl/api.js

@@ -288,6 +288,7 @@ function createClasses(classesType, params) {
 
             constructor(args) {
                 args = args || {};
+                this.originalArgs = args;
                 this.init(args);
                 for (const argName in argsConfig) {
                     if (argName === "randomId" && !args[argName]) {
@@ -578,6 +579,10 @@ function createClasses(classesType, params) {
                     }
                 }
             }
+
+            toJSON() {
+                return this.originalArgs;
+            }
         }
 
         if (namespace) {

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

@@ -236,6 +236,11 @@ export class CustomMessage extends SenderGetter {
     _sender?: any;
     /** @hidden */
     _entities?: Map<number, Entity>;
+    /** @hidden */
+    /* @ts-ignore */
+    getBytes(): Buffer;
+    originalArgs: any;
+
     patternMatch?: RegExpMatchArray;
 
     [inspect.custom]() {
@@ -771,7 +776,7 @@ export class CustomMessage extends SenderGetter {
             const params = {
                 messages: [this.id],
                 fromPeer: (await this.getInputChat())!,
-            }
+            };
             return this._client.forwardMessages(entity, params);
         }
     }

+ 4 - 2
gramjs/tl/types-generator/template.js

@@ -45,7 +45,7 @@ module.exports = ({ types, constructors, functions }) => {
                     constructorId,
                     subclassOfId,
                 } = args;
-                if (name == "Message") {
+                if (name === "Message") {
                     return `export class Message extends CustomMessage {
 ${indent}CONSTRUCTOR_ID: ${constructorId};
 ${indent}SUBCLASS_OF_ID: ${subclassOfId};
@@ -53,7 +53,7 @@ ${indent}classType: "request";
 ${indent}className: "${getClassNameWithNameSpace(name, namespace)}";
 ${indent}static fromReader(reader: Reader): ${upperFirst(name)};
     }`;
-                } else if (name == "MessageService") {
+                } else if (name === "MessageService") {
                     return `export class MessageService extends CustomMessage {
 ${indent}CONSTRUCTOR_ID: ${constructorId};
 ${indent}SUBCLASS_OF_ID: ${subclassOfId};
@@ -262,6 +262,8 @@ export namespace Api {
     className: string;
     classType: 'constructor' | 'request';
     constructor(args: Args);
+    originalArgs: Args;
+    toJSON(): Args;
   }
   class Request<Args, Response> extends VirtualClass<Partial<Args>> {
     static readResult(reader: Reader): Buffer;

Diff do ficheiro suprimidas por serem muito extensas
+ 8300 - 2
package-lock.json


+ 1 - 1
package.json

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

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff