From df3cadf5a1058f743ba93e36c6ac34bd0f2cd92d Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 15 Sep 2022 10:17:50 +0200 Subject: [PATCH] Fix support for forbidden chats --- src/api/gramjs/apiBuilders/chats.ts | 33 ++++++----------------------- src/api/gramjs/methods/chats.ts | 2 +- src/api/gramjs/methods/settings.ts | 2 +- src/api/gramjs/updater.ts | 5 ++++- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/api/gramjs/apiBuilders/chats.ts b/src/api/gramjs/apiBuilders/chats.ts index 56e49f52a..5db665261 100644 --- a/src/api/gramjs/apiBuilders/chats.ts +++ b/src/api/gramjs/apiBuilders/chats.ts @@ -48,20 +48,13 @@ function buildApiChatFieldsFromPeerEntity( ...(accessHash && { accessHash }), hasVideoAvatar, ...(avatarHash && { avatarHash }), - ...( - (peerEntity instanceof GramJs.Channel || peerEntity instanceof GramJs.User) - && { username: peerEntity.username } - ), + ...(('username' in peerEntity) && { username: peerEntity.username }), ...(('verified' in peerEntity) && { isVerified: peerEntity.verified }), ...(('callActive' in peerEntity) && { isCallActive: peerEntity.callActive }), ...(('callNotEmpty' in peerEntity) && { isCallNotEmpty: peerEntity.callNotEmpty }), - ...((peerEntity instanceof GramJs.Chat || peerEntity instanceof GramJs.Channel) && { - ...(peerEntity.participantsCount && { membersCount: peerEntity.participantsCount }), - joinDate: peerEntity.date, - }), - ...((peerEntity instanceof GramJs.Chat || peerEntity instanceof GramJs.Channel) && { - isProtected: Boolean('noforwards' in peerEntity && peerEntity.noforwards), - }), + ...('date' in peerEntity && { joinDate: peerEntity.date }), + ...('participantsCount' in peerEntity && { membersCount: peerEntity.participantsCount }), + ...(('noforwards' in peerEntity) && { isProtected: Boolean(peerEntity.noforwards) }), ...(isSupport && { isSupport: true }), ...buildApiChatPermissions(peerEntity), ...(('creator' in peerEntity) && { isCreator: peerEntity.creator }), @@ -113,7 +106,7 @@ function buildApiChatPermissions(peerEntity: GramJs.TypeUser | GramJs.TypeChat): return { adminRights: peerEntity.adminRights ? omitVirtualClassFields(peerEntity.adminRights) : undefined, - currentUserBannedRights: peerEntity instanceof GramJs.Channel && peerEntity.bannedRights + currentUserBannedRights: 'bannedRights' in peerEntity && peerEntity.bannedRights ? omitVirtualClassFields(peerEntity.bannedRights) : undefined, defaultBannedRights: peerEntity.defaultBannedRights @@ -178,7 +171,7 @@ function buildApiChatMigrationInfo(peerEntity: GramJs.TypeChat): { }; } { if ( - peerEntity instanceof GramJs.Chat + 'migratedTo' in peerEntity && peerEntity.migratedTo && !(peerEntity.migratedTo instanceof GramJs.InputChannelEmpty) ) { @@ -209,20 +202,8 @@ function buildApiChatRestrictionReason( export function buildApiChatFromPreview( preview: GramJs.TypeChat | GramJs.TypeUser, isSupport = false, - withForbidden = false, ): ApiChat | undefined { - if (!( - preview instanceof GramJs.Chat - || preview instanceof GramJs.Channel - || preview instanceof GramJs.User - || ( - withForbidden - && ( - preview instanceof GramJs.ChatForbidden - || preview instanceof GramJs.ChannelForbidden - ) - ) - )) { + if (preview instanceof GramJs.ChatEmpty || preview instanceof GramJs.UserEmpty) { return undefined; } diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index 2d6fac9f4..ba80e408d 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -469,7 +469,7 @@ async function getFullChannelInfo( updateLocalDb(result); const [, mtpLinkedChat] = result.chats; - const chat = buildApiChatFromPreview(mtpLinkedChat, undefined, true); + const chat = buildApiChatFromPreview(mtpLinkedChat); if (chat) { onUpdate({ '@type': 'updateChat', diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index 4eb73f604..3699f1756 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -138,7 +138,7 @@ export async function fetchBlockedContacts() { return { users: result.users.map(buildApiUser).filter(Boolean as any), - chats: result.chats.map((chat) => buildApiChatFromPreview(chat, undefined, true)).filter(Boolean as any), + chats: result.chats.map((chat) => buildApiChatFromPreview(chat)).filter(Boolean as any), blockedIds: result.blocked.map((blocked) => getApiChatIdFromMtpPeer(blocked.peerId)), totalCount: result instanceof GramJs.contacts.BlockedSlice ? result.count : result.blocked.length, }; diff --git a/src/api/gramjs/updater.ts b/src/api/gramjs/updater.ts index 375706084..83e11784a 100644 --- a/src/api/gramjs/updater.ts +++ b/src/api/gramjs/updater.ts @@ -87,7 +87,10 @@ function dispatchUserAndChatUpdates(entities: (GramJs.TypeUser | GramJs.TypeChat }); entities - .filter((e) => e instanceof GramJs.Chat || e instanceof GramJs.Channel) + .filter((e) => ( + e instanceof GramJs.Chat || e instanceof GramJs.ChatForbidden + || e instanceof GramJs.Channel || e instanceof GramJs.ChannelForbidden + )) .map((e) => buildApiChatFromPreview(e)) .forEach((chat) => { if (!chat) {