From 85ed69672194a53bbb69055545f4eea9a1a0b5c4 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 23 Jul 2021 23:28:24 +0300 Subject: [PATCH] Support stopping and restarting bots (#1316) --- src/api/gramjs/apiBuilders/users.ts | 3 +- src/api/gramjs/methods/settings.ts | 6 ++-- src/api/types/users.ts | 1 + src/components/common/DeleteChatModal.tsx | 29 ++++++++++++--- src/components/middle/HeaderActions.tsx | 33 ++++++++++++++--- src/components/middle/HeaderMenuContainer.tsx | 36 ++++++++++++++++++- src/components/middle/MiddleColumn.tsx | 2 +- src/global/types.ts | 2 +- src/modules/actions/api/bots.ts | 25 +++++++++++-- src/modules/actions/api/settings.ts | 4 +-- src/modules/reducers/settings.ts | 5 +++ src/modules/reducers/users.ts | 16 +++++++++ src/modules/selectors/users.ts | 6 ++++ 13 files changed, 149 insertions(+), 19 deletions(-) diff --git a/src/api/gramjs/apiBuilders/users.ts b/src/api/gramjs/apiBuilders/users.ts index b62d2f728..691f0c870 100644 --- a/src/api/gramjs/apiBuilders/users.ts +++ b/src/api/gramjs/apiBuilders/users.ts @@ -3,7 +3,7 @@ import { ApiUser, ApiUserStatus, ApiUserType } from '../../types'; export function buildApiUserFromFull(mtpUserFull: GramJs.UserFull): ApiUser { const { - about, commonChatsCount, pinnedMsgId, botInfo, + about, commonChatsCount, pinnedMsgId, botInfo, blocked, } = mtpUserFull; return { @@ -12,6 +12,7 @@ export function buildApiUserFromFull(mtpUserFull: GramJs.UserFull): ApiUser { bio: about, commonChatsCount, pinnedMessageId: pinnedMsgId, + isBlocked: Boolean(blocked), ...(botInfo && { botDescription: botInfo.description }), }, }; diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index df7499468..020ed853c 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -13,7 +13,7 @@ import { import { buildApiUser } from '../apiBuilders/users'; import { buildApiChatFromPreview, getApiChatIdFromMtpPeer } from '../apiBuilders/chats'; -import { buildInputPrivacyKey, buildInputPeer, buildPeer } from '../gramjsBuilders'; +import { buildInputPrivacyKey, buildInputPeer } from '../gramjsBuilders'; import { invokeRequest, uploadFile, getClient } from './client'; import { omitVirtualClassFields } from '../apiBuilders/helpers'; import { buildCollectionByKey } from '../../../util/iteratees'; @@ -129,9 +129,9 @@ export async function fetchBlockedContacts() { }; } -export function blockContact(chatOrUserId: number) { +export function blockContact(chatOrUserId: number, accessHash?: string) { return invokeRequest(new GramJs.contacts.Block({ - id: buildPeer(chatOrUserId), + id: buildInputPeer(chatOrUserId, accessHash), })); } diff --git a/src/api/types/users.ts b/src/api/types/users.ts index c6573bc47..b06128d45 100644 --- a/src/api/types/users.ts +++ b/src/api/types/users.ts @@ -23,6 +23,7 @@ export interface ApiUser { } export interface ApiUserFullInfo { + isBlocked?: boolean; bio?: string; commonChatsCount?: number; botDescription?: string; diff --git a/src/components/common/DeleteChatModal.tsx b/src/components/common/DeleteChatModal.tsx index 3540da9b3..a0a84275c 100644 --- a/src/components/common/DeleteChatModal.tsx +++ b/src/components/common/DeleteChatModal.tsx @@ -7,6 +7,7 @@ import { GlobalActions } from '../../global/types'; import { selectIsChatWithSelf, selectUser } from '../../modules/selectors'; import { isChatPrivate, + isUserBot, getUserFirstOrLastName, getPrivateChatUserId, isChatBasicGroup, @@ -34,6 +35,7 @@ export type OwnProps = { type StateProps = { isChannel: boolean; isChatWithSelf?: boolean; + isBot?: boolean; isPrivateChat: boolean; isBasicGroup: boolean; isSuperGroup: boolean; @@ -42,7 +44,9 @@ type StateProps = { contactName?: string; }; -type DispatchProps = Pick; +type DispatchProps = Pick; const DeleteChatModal: FC = ({ isOpen, @@ -50,6 +54,7 @@ const DeleteChatModal: FC = ({ isChannel, isPrivateChat, isChatWithSelf, + isBot, isBasicGroup, isSuperGroup, currentUserId, @@ -61,6 +66,7 @@ const DeleteChatModal: FC = ({ deleteHistory, deleteChannel, deleteChatUser, + blockContact, }) => { const lang = useLang(); const chatTitle = getChatTitle(lang, chat); @@ -71,6 +77,13 @@ const DeleteChatModal: FC = ({ onClose(); }, [deleteHistory, chat.id, onClose]); + const handleDeleteAndStop = useCallback(() => { + deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); + blockContact({ contactId: chat.id, accessHash: chat.accessHash }); + + onClose(); + }, [deleteHistory, chat.id, chat.accessHash, blockContact, onClose]); + const handleDeleteChat = useCallback(() => { if (isPrivateChat) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); @@ -163,6 +176,11 @@ const DeleteChatModal: FC = ({ onCloseAnimationEnd={onCloseAnimationEnd} > {renderMessage()} + {isBot && ( + + )} {canDeleteForAll && ( )} + {!IS_SINGLE_COLUMN_LAYOUT && canRestartBot && ( + + )} {!IS_SINGLE_COLUMN_LAYOUT && canSearch && (