Browse Source

Add proper support for `flags2` (#366)

* regenerate api.d.ts

* Format code with Prettier

Co-authored-by: Coxxs <coxxs@users.noreply.github.com>
Coxxs 3 năm trước cách đây
mục cha
commit
39afe55335

+ 6 - 6
gramjs/tl/api.d.ts

@@ -1055,7 +1055,7 @@ export namespace Api {
         hasScheduled?: boolean;
         canViewStats?: boolean;
         blocked?: boolean;
-        flags2: null;
+        // flags2: null;
         canDeleteChannel?: boolean;
         id: long;
         about: string;
@@ -1107,7 +1107,7 @@ export namespace Api {
         hasScheduled?: boolean;
         canViewStats?: boolean;
         blocked?: boolean;
-        flags2: null;
+        // flags2: null;
         canDeleteChannel?: boolean;
         id: long;
         about: string;
@@ -5088,7 +5088,7 @@ export namespace Api {
     }
     export class ReceivedNotifyMessage extends VirtualClass<{
         id: int;
-        // flags: int;
+        flags: int;
     }> {
         CONSTRUCTOR_ID: 2743383929;
         SUBCLASS_OF_ID: 2841786398;
@@ -5096,7 +5096,7 @@ export namespace Api {
         className: "ReceivedNotifyMessage";
         static fromReader(reader: Reader): ReceivedNotifyMessage;
         id: int;
-        // flags: int;
+        flags: int;
     }
     export class ChatInviteExported extends VirtualClass<{
         // flags: null;
@@ -14892,7 +14892,7 @@ export namespace Api {
         }
         export class ImportBotAuthorization extends Request<
             Partial<{
-                // flags: int;
+                flags: int;
                 apiId: int;
                 apiHash: string;
                 botAuthToken: string;
@@ -14904,7 +14904,7 @@ export namespace Api {
             classType: "request";
             className: "auth.ImportBotAuthorization";
             static fromReader(reader: Reader): ImportBotAuthorization;
-            // flags: int;
+            flags: int;
             apiId: int;
             apiHash: string;
             botAuthToken: string;

+ 11 - 5
gramjs/tl/api.js

@@ -315,18 +315,18 @@ function createClasses(classesType, params) {
                         if (arg.isFlag) {
                             if (arg.type === "true") {
                                 args[argName] = Boolean(
-                                    args["flags"] & (1 << arg.flagIndex)
+                                    args[arg.flagName] & (1 << arg.flagIndex)
                                 );
                                 continue;
                             }
-                            if (args["flags"] & (1 << arg.flagIndex)) {
+                            if (args[arg.flagName] & (1 << arg.flagIndex)) {
                                 args[argName] = getArgFromReader(reader, arg);
                             } else {
                                 args[argName] = null;
                             }
                         } else {
                             if (arg.flagIndicator) {
-                                arg.name = "flags";
+                                arg.name = argName;
                             }
                             args[argName] = getArgFromReader(reader, arg);
                         }
@@ -338,7 +338,10 @@ function createClasses(classesType, params) {
             validate() {
                 for (const arg in argsConfig) {
                     if (argsConfig.hasOwnProperty(arg)) {
-                        if (arg === "flags" || argsConfig[arg].isFlag) {
+                        if (
+                            argsConfig[arg].flagIndicator ||
+                            argsConfig[arg].isFlag
+                        ) {
                             // we don't care about flags
                             continue;
                         }
@@ -458,7 +461,10 @@ function createClasses(classesType, params) {
                             } else {
                                 let flagCalculate = 0;
                                 for (const f in argsConfig) {
-                                    if (argsConfig[f].isFlag) {
+                                    if (
+                                        argsConfig[f].isFlag &&
+                                        arg === argsConfig[f].flagName
+                                    ) {
                                         if (
                                             (this[f] === false && argsConfig[f].type !== 'Bool') ||
                                             this[f] === undefined ||

+ 7 - 4
gramjs/tl/generationHelpers.ts

@@ -109,6 +109,7 @@ function buildArgConfig(name: string, argType: string) {
         isVector: false,
         isFlag: false,
         skipConstructorId: false,
+        flagName: null,
         flagIndex: -1,
         flagIndicator: true,
         type: null,
@@ -130,17 +131,19 @@ function buildArgConfig(name: string, argType: string) {
         // The type may be a flag (flags.IDX?REAL_TYPE)
         // Note that 'flags' is NOT the flags name; this
         // is determined by a previous argument
-        // However, we assume that the argument will always be called 'flags'
+        // However, we assume that the argument will always be starts with 'flags'
         // @ts-ignore
         const flagMatch = currentConfig.type.match(
-            /flags(\d+)?.(\d+)\?([\w<>.]+)/
+            /(flags(?:\d+)?).(\d+)\?([\w<>.]+)/
         );
 
         if (flagMatch) {
             currentConfig.isFlag = true;
-            currentConfig.flagIndex = Number(flagMatch[1] ?? flagMatch[2]);
+            // As of layer 140, flagName can be "flags" or "flags2"
+            currentConfig.flagName = flagMatch[1];
+            currentConfig.flagIndex = Number(flagMatch[2]);
             // Update the type to match the exact type, not the "flagged" one
-            [, , , currentConfig.type] = flagMatch;
+            currentConfig.type = flagMatch[3];
         }
 
         // Then check if the type is a Vector<REAL_TYPE>

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

@@ -78,7 +78,8 @@ ${indent}static fromReader(reader: Reader): ${upperFirst(name)};
 
                 const hasRequiredArgs = argKeys.some(
                     (argName) =>
-                        argName !== "flags" && !argsConfig[argName].isFlag
+                        !argsConfig[argName].flagIndicator &&
+                        !argsConfig[argName].isFlag
                 );
 
                 return `
@@ -135,7 +136,8 @@ ${indent}static fromReader(reader: Reader): ${upperFirst(name)};
 
                 const hasRequiredArgs = argKeys.some(
                     (argName) =>
-                        argName !== "flags" && !argsConfig[argName].isFlag
+                        !argsConfig[argName].flagIndicator &&
+                        !argsConfig[argName].isFlag
                 );
 
                 return `
@@ -177,10 +179,10 @@ ${indent}}`.trim();
     }
 
     function renderArg(argName, argConfig) {
-        const { isVector, isFlag, skipConstructorId, type } = argConfig;
+        const { isVector, isFlag, skipConstructorId, flagIndicator, type } = argConfig;
 
         const valueType = renderValueType(type, isVector, !skipConstructorId);
-        return `${argName === "flags" ? "// " : ""}${argName}${
+        return `${flagIndicator ? "// " : ""}${argName}${
             isFlag || (argName === "randomId" && type === "long" && !isVector)
                 ? "?"
                 : ""