GramJs: Support flags2 (#2130)

This commit is contained in:
Alexander Zinchuk 2022-11-16 16:16:20 +04:00
parent 1a87b3cd53
commit ee85cb4441
6 changed files with 26 additions and 25 deletions

View File

@ -89,7 +89,7 @@ function mergeWithNamespaces(obj1, obj2) {
}
function extractParams(fileContent) {
const f = parseTl(fileContent, 109);
const f = parseTl(fileContent);
const constructors = [];
const functions = [];
for (const d of f) {
@ -259,21 +259,15 @@ function createClasses(classesType, params) {
if (argsConfig.hasOwnProperty(argName)) {
const arg = argsConfig[argName];
if (arg.isFlag) {
const flagValue = arg.flagIndex > 30
? args.flags2 & (1 << (arg.flagIndex - 31)) : args.flags & (1 << arg.flagIndex);
const flagGroupSuffix = arg.flagGroup > 1 ? arg.flagGroup : '';
const flagValue = args[`flags${flagGroupSuffix}`] & (1 << arg.flagIndex);
if (arg.type === 'true') {
args[argName] = Boolean(flagValue);
continue;
}
if (flagValue) {
args[argName] = getArgFromReader(reader, arg);
} else {
args[argName] = undefined;
}
args[argName] = flagValue ? getArgFromReader(reader, arg) : undefined;
} else {
if (arg.flagIndicator) {
arg.name = 'flags';
}
args[argName] = getArgFromReader(reader, arg);
}
}

View File

@ -79,7 +79,7 @@ chatForbidden#6592a1a7 id:long title:string = Chat;
channel#8261ac61 flags:# creator:flags.0?true left:flags.2?true broadcast:flags.5?true verified:flags.7?true megagroup:flags.8?true restricted:flags.9?true signatures:flags.11?true min:flags.12?true scam:flags.19?true has_link:flags.20?true has_geo:flags.21?true slowmode_enabled:flags.22?true call_active:flags.23?true call_not_empty:flags.24?true fake:flags.25?true gigagroup:flags.26?true noforwards:flags.27?true join_to_send:flags.28?true join_request:flags.29?true id:long access_hash:flags.13?long title:string username:flags.6?string photo:ChatPhoto date:int restriction_reason:flags.9?Vector<RestrictionReason> admin_rights:flags.14?ChatAdminRights banned_rights:flags.15?ChatBannedRights default_banned_rights:flags.18?ChatBannedRights participants_count:flags.17?int = Chat;
channelForbidden#17d493d5 flags:# broadcast:flags.5?true megagroup:flags.8?true id:long access_hash:long title:string until_date:flags.16?int = Chat;
chatFull#c9d31138 flags:# can_set_username:flags.7?true has_scheduled:flags.8?true id:long about:string participants:ChatParticipants chat_photo:flags.2?Photo notify_settings:PeerNotifySettings exported_invite:flags.13?ExportedChatInvite bot_info:flags.3?Vector<BotInfo> pinned_msg_id:flags.6?int folder_id:flags.11?int call:flags.12?InputGroupCall ttl_period:flags.14?int groupcall_default_join_as:flags.15?Peer theme_emoticon:flags.16?string requests_pending:flags.17?int recent_requesters:flags.17?Vector<long> available_reactions:flags.18?ChatReactions = ChatFull;
channelFull#f2355507 flags:# can_view_participants:flags.3?true can_set_username:flags.6?true can_set_stickers:flags.7?true hidden_prehistory:flags.10?true can_set_location:flags.16?true has_scheduled:flags.19?true can_view_stats:flags.20?true blocked:flags.22?true flags2:# can_delete_channel:flags.31?true id:long about:string participants_count:flags.0?int admins_count:flags.1?int kicked_count:flags.2?int banned_count:flags.2?int online_count:flags.13?int read_inbox_max_id:int read_outbox_max_id:int unread_count:int chat_photo:Photo notify_settings:PeerNotifySettings exported_invite:flags.23?ExportedChatInvite bot_info:Vector<BotInfo> migrated_from_chat_id:flags.4?long migrated_from_max_id:flags.4?int pinned_msg_id:flags.5?int stickerset:flags.8?StickerSet available_min_id:flags.9?int folder_id:flags.11?int linked_chat_id:flags.14?long location:flags.15?ChannelLocation slowmode_seconds:flags.17?int slowmode_next_send_date:flags.18?int stats_dc:flags.12?int pts:int call:flags.21?InputGroupCall ttl_period:flags.24?int pending_suggestions:flags.25?Vector<string> groupcall_default_join_as:flags.26?Peer theme_emoticon:flags.27?string requests_pending:flags.28?int recent_requesters:flags.28?Vector<long> default_send_as:flags.29?Peer available_reactions:flags.30?ChatReactions = ChatFull;
channelFull#f2355507 flags:# can_view_participants:flags.3?true can_set_username:flags.6?true can_set_stickers:flags.7?true hidden_prehistory:flags.10?true can_set_location:flags.16?true has_scheduled:flags.19?true can_view_stats:flags.20?true blocked:flags.22?true flags2:# can_delete_channel:flags2.0?true id:long about:string participants_count:flags.0?int admins_count:flags.1?int kicked_count:flags.2?int banned_count:flags.2?int online_count:flags.13?int read_inbox_max_id:int read_outbox_max_id:int unread_count:int chat_photo:Photo notify_settings:PeerNotifySettings exported_invite:flags.23?ExportedChatInvite bot_info:Vector<BotInfo> migrated_from_chat_id:flags.4?long migrated_from_max_id:flags.4?int pinned_msg_id:flags.5?int stickerset:flags.8?StickerSet available_min_id:flags.9?int folder_id:flags.11?int linked_chat_id:flags.14?long location:flags.15?ChannelLocation slowmode_seconds:flags.17?int slowmode_next_send_date:flags.18?int stats_dc:flags.12?int pts:int call:flags.21?InputGroupCall ttl_period:flags.24?int pending_suggestions:flags.25?Vector<string> groupcall_default_join_as:flags.26?Peer theme_emoticon:flags.27?string requests_pending:flags.28?int recent_requesters:flags.28?Vector<long> default_send_as:flags.29?Peer available_reactions:flags.30?ChatReactions = ChatFull;
chatParticipant#c02d4007 user_id:long inviter_id:long date:int = ChatParticipant;
chatParticipantCreator#e46bcee4 user_id:long = ChatParticipant;
chatParticipantAdmin#a0933f5b user_id:long inviter_id:long date:int = ChatParticipant;

View File

@ -110,7 +110,7 @@ const fromLine = (line, isFunction) => {
.replace(/(:|\?)bytes /g, '$1string ')
.replace(/</g, ' ')
.replace(/>|{|}/g, '')
.replace(/ \w+:flags\.\d+\?true/g, '');
.replace(/ \w+:flags\d*\.\d+\?true/g, '');
if (currentConfig.name === 'inputMediaInvoice') {
// eslint-disable-next-line no-empty
@ -147,6 +147,7 @@ function buildArgConfig(name, argType) {
isVector: false,
isFlag: false,
skipConstructorId: false,
flagGroup: 0,
flagIndex: -1,
flagIndicator: true,
type: undefined,
@ -159,17 +160,18 @@ function buildArgConfig(name, argType) {
// Strip the exclamation mark always to have only the name
currentConfig.type = argType.replace(/^!+/, '');
// The type may be a flag (flags.IDX?REAL_TYPE)
// The type may be a flag (flags[N].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'
const flagMatch = currentConfig.type.match(/flags.(\d+)\?([\w<>.]+)/);
// However, we assume that the argument will always be called 'flags[N]'
const flagMatch = currentConfig.type.match(/flags(\d*)\.(\d+)\?([\w<>.]+)/);
if (flagMatch) {
currentConfig.isFlag = true;
currentConfig.flagIndex = Number(flagMatch[1]);
currentConfig.flagGroup = Number(flagMatch[1] || 1);
currentConfig.flagIndex = Number(flagMatch[2]);
// Update the type to match the exact type, not the "flagged" one
[, , currentConfig.type] = flagMatch;
[, , , currentConfig.type] = flagMatch;
}
// Then check if the type is a Vector<REAL_TYPE>
@ -210,7 +212,7 @@ function buildArgConfig(name, argType) {
return currentConfig;
}
function* parseTl(content, layer, methods = [], ignoreIds = CORE_TYPES) {
function* parseTl(content, methods = [], ignoreIds = CORE_TYPES) {
(methods || []).reduce((o, m) => ({
...o,
[m.name]: m,

View File

@ -99,7 +99,7 @@ channel#8261ac61 flags:# creator:flags.0?true left:flags.2?true broadcast:flags.
channelForbidden#17d493d5 flags:# broadcast:flags.5?true megagroup:flags.8?true id:long access_hash:long title:string until_date:flags.16?int = Chat;
chatFull#c9d31138 flags:# can_set_username:flags.7?true has_scheduled:flags.8?true id:long about:string participants:ChatParticipants chat_photo:flags.2?Photo notify_settings:PeerNotifySettings exported_invite:flags.13?ExportedChatInvite bot_info:flags.3?Vector<BotInfo> pinned_msg_id:flags.6?int folder_id:flags.11?int call:flags.12?InputGroupCall ttl_period:flags.14?int groupcall_default_join_as:flags.15?Peer theme_emoticon:flags.16?string requests_pending:flags.17?int recent_requesters:flags.17?Vector<long> available_reactions:flags.18?ChatReactions = ChatFull;
channelFull#f2355507 flags:# can_view_participants:flags.3?true can_set_username:flags.6?true can_set_stickers:flags.7?true hidden_prehistory:flags.10?true can_set_location:flags.16?true has_scheduled:flags.19?true can_view_stats:flags.20?true blocked:flags.22?true flags2:# can_delete_channel:flags.31?true id:long about:string participants_count:flags.0?int admins_count:flags.1?int kicked_count:flags.2?int banned_count:flags.2?int online_count:flags.13?int read_inbox_max_id:int read_outbox_max_id:int unread_count:int chat_photo:Photo notify_settings:PeerNotifySettings exported_invite:flags.23?ExportedChatInvite bot_info:Vector<BotInfo> migrated_from_chat_id:flags.4?long migrated_from_max_id:flags.4?int pinned_msg_id:flags.5?int stickerset:flags.8?StickerSet available_min_id:flags.9?int folder_id:flags.11?int linked_chat_id:flags.14?long location:flags.15?ChannelLocation slowmode_seconds:flags.17?int slowmode_next_send_date:flags.18?int stats_dc:flags.12?int pts:int call:flags.21?InputGroupCall ttl_period:flags.24?int pending_suggestions:flags.25?Vector<string> groupcall_default_join_as:flags.26?Peer theme_emoticon:flags.27?string requests_pending:flags.28?int recent_requesters:flags.28?Vector<long> default_send_as:flags.29?Peer available_reactions:flags.30?ChatReactions = ChatFull;
channelFull#f2355507 flags:# can_view_participants:flags.3?true can_set_username:flags.6?true can_set_stickers:flags.7?true hidden_prehistory:flags.10?true can_set_location:flags.16?true has_scheduled:flags.19?true can_view_stats:flags.20?true blocked:flags.22?true flags2:# can_delete_channel:flags2.0?true id:long about:string participants_count:flags.0?int admins_count:flags.1?int kicked_count:flags.2?int banned_count:flags.2?int online_count:flags.13?int read_inbox_max_id:int read_outbox_max_id:int unread_count:int chat_photo:Photo notify_settings:PeerNotifySettings exported_invite:flags.23?ExportedChatInvite bot_info:Vector<BotInfo> migrated_from_chat_id:flags.4?long migrated_from_max_id:flags.4?int pinned_msg_id:flags.5?int stickerset:flags.8?StickerSet available_min_id:flags.9?int folder_id:flags.11?int linked_chat_id:flags.14?long location:flags.15?ChannelLocation slowmode_seconds:flags.17?int slowmode_next_send_date:flags.18?int stats_dc:flags.12?int pts:int call:flags.21?InputGroupCall ttl_period:flags.24?int pending_suggestions:flags.25?Vector<string> groupcall_default_join_as:flags.26?Peer theme_emoticon:flags.27?string requests_pending:flags.28?int recent_requesters:flags.28?Vector<long> default_send_as:flags.29?Peer available_reactions:flags.30?ChatReactions = ChatFull;
chatParticipant#c02d4007 user_id:long inviter_id:long date:int = ChatParticipant;
chatParticipantCreator#e46bcee4 user_id:long = ChatParticipant;

View File

@ -27,7 +27,7 @@ function main() {
}
function extractParams(fileContent) {
const defInterator = parseTl(fileContent, 109);
const defInterator = parseTl(fileContent);
const types = {};
const constructors = [];
const functions = [];

View File

@ -1,6 +1,7 @@
// Not sure what they are for.
const RAW_TYPES = new Set(['Bool', 'X'])
const FLAGS = ['flags', 'flags2'];
const FLAG_REGEX = /flags\d*/;
module.exports = ({ types, constructors, functions }) => {
function groupByKey(collection, key) {
@ -17,6 +18,10 @@ module.exports = ({ types, constructors, functions }) => {
}, {})
}
function isFlagArg(argName) {
return argName.match(FLAG_REGEX);
}
function renderTypes(types, indent) {
return types.map(({ name, constructors }) => `
${!constructors.length ? '// ' : ''}export type Type${upperFirst(name)} = ${constructors.map((name) => name)
@ -33,7 +38,7 @@ module.exports = ({ types, constructors, functions }) => {
return `export class ${upperFirst(name)} extends VirtualClass<void> {};`
}
let hasRequiredArgs = argKeys.some((argName) => !FLAGS.includes(argName) && !argsConfig[argName].isFlag)
let hasRequiredArgs = argKeys.some((argName) => !isFlagArg(argName) && !argsConfig[argName].isFlag)
return `
export class ${upperFirst(name)} extends VirtualClass<{
@ -62,7 +67,7 @@ ${indent}};`.trim()
return `export class ${upperFirst(name)} extends Request<void, ${renderedResult}> {};`
}
let hasRequiredArgs = argKeys.some((argName) => !FLAGS.includes(argName) && !argsConfig[argName].isFlag)
let hasRequiredArgs = argKeys.some((argName) => !isFlagArg(argName) && !argsConfig[argName].isFlag)
return `
export class ${upperFirst(name)} extends Request<Partial<{
@ -96,7 +101,7 @@ ${indent}};`.trim()
const valueType = renderValueType(type, isVector, !skipConstructorId)
return `${FLAGS.includes(argName) ? '// ' : ''}${argName}${isFlag ? '?' : ''}: ${valueType}`
return `${isFlagArg(argName) ? '// ' : ''}${argName}${isFlag ? '?' : ''}: ${valueType}`
}
function renderValueType(type, isVector, isTlType) {