From a6d0f5a22030c8e44836b705c0d75a224b217d1d Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 29 Jun 2021 17:23:26 +0300 Subject: [PATCH] Fix support for `-` in invite links; Small refactoring --- src/api/gramjs/apiBuilders/chats.ts | 39 +++++++++++++---------------- src/config.ts | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/api/gramjs/apiBuilders/chats.ts b/src/api/gramjs/apiBuilders/chats.ts index 15ba16a38..3a214b54f 100644 --- a/src/api/gramjs/apiBuilders/chats.ts +++ b/src/api/gramjs/apiBuilders/chats.ts @@ -109,39 +109,36 @@ function buildApiChatRestrictions(peerEntity: GramJs.TypeUser | GramJs.TypeChat) }; } - if (peerEntity instanceof GramJs.User) { + const restrictions = {}; + + if ('restricted' in peerEntity) { const restrictionReason = peerEntity.restricted ? buildApiChatRestrictionReason(peerEntity.restrictionReason) : undefined; - if (restrictionReason === undefined) { - return { - isRestricted: false, - }; + if (restrictionReason) { + Object.assign(restrictions, { + isRestricted: true, + restrictionReason, + }); } + } - return { - isRestricted: peerEntity.restricted, - restrictionReason, - }; - } else if (peerEntity instanceof GramJs.Chat) { - return { + if (peerEntity instanceof GramJs.Chat) { + Object.assign(restrictions, { isNotJoined: peerEntity.left, isRestricted: peerEntity.kicked, - }; - } else if (peerEntity instanceof GramJs.Channel) { - const isRestricted = peerEntity.restricted - && peerEntity.restrictionReason - && peerEntity.restrictionReason.some((reason) => reason.platform === 'all'); + }); + } - return { + if (peerEntity instanceof GramJs.Channel) { + Object.assign(restrictions, { // `left` is weirdly set to `true` on all channels never joined before isNotJoined: peerEntity.left, - isRestricted, - restrictionReason: buildApiChatRestrictionReason(peerEntity.restrictionReason), - }; + }); } - return {}; + + return restrictions; } function buildApiChatMigrationInfo(peerEntity: GramJs.TypeChat): { diff --git a/src/config.ts b/src/config.ts index c47a1999e..4f31f831a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -116,7 +116,7 @@ export const CONTENT_TYPES_FOR_QUICK_UPLOAD = 'image/png,image/gif,image/jpeg,vi // eslint-disable-next-line max-len export const RE_LINK_TEMPLATE = '((ftp|https?):\\/\\/)?((www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})\\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)'; export const RE_TME_LINK = /^(?:https?:\/\/)?(?:t\.me\/)([\d\w_]+)(?:\/([\d]+))?$/gm; -export const RE_TME_INVITE_LINK = /^(?:https?:\/\/)?(?:t\.me\/joinchat\/)([\d\w_]+)?$/gm; +export const RE_TME_INVITE_LINK = /^(?:https?:\/\/)?(?:t\.me\/joinchat\/)([\d\w_-]+)?$/gm; // MTProto constants export const SERVICE_NOTIFICATIONS_USER_ID = 777000;