tdesktop/Telegram/SourceFiles/inline_bots/bot_attach_web_view.h

296 lines
7.4 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2022-04-28 12:16:26 +00:00
#include "base/flags.h"
2023-09-06 09:41:23 +00:00
#include "base/timer.h"
#include "base/weak_ptr.h"
2023-08-29 17:36:12 +00:00
#include "mtproto/sender.h"
#include "ui/chat/attach/attach_bot_webview.h"
2023-09-06 09:41:23 +00:00
#include "ui/rp_widget.h"
namespace Api {
struct SendAction;
} // namespace Api
namespace Ui {
class GenericBox;
class DropdownMenu;
} // namespace Ui
namespace Ui::BotWebView {
class Panel;
} // namespace Ui::BotWebView
namespace Main {
class Session;
} // namespace Main
namespace Window {
class SessionController;
} // namespace Window
2022-03-29 14:14:11 +00:00
namespace Data {
class DocumentMedia;
} // namespace Data
namespace InlineBots {
2022-04-28 12:16:26 +00:00
enum class PeerType : uint8 {
SameBot = 0x01,
Bot = 0x02,
User = 0x04,
Group = 0x08,
Broadcast = 0x10,
};
using PeerTypes = base::flags<PeerType>;
[[nodiscard]] bool PeerMatchesTypes(
not_null<PeerData*> peer,
not_null<UserData*> bot,
PeerTypes types);
2022-06-10 13:38:52 +00:00
[[nodiscard]] PeerTypes ParseChooseTypes(QStringView choose);
2022-04-28 12:16:26 +00:00
struct AttachWebViewBot {
not_null<UserData*> user;
2022-03-31 09:24:13 +00:00
DocumentData *icon = nullptr;
2022-03-29 14:14:11 +00:00
std::shared_ptr<Data::DocumentMedia> media;
QString name;
2022-04-28 12:16:26 +00:00
PeerTypes types = 0;
2023-09-06 09:41:23 +00:00
bool inactive : 1 = false;
bool inMainMenu : 1 = false;
bool inAttachMenu : 1 = false;
bool disclaimerRequired : 1 = false;
bool hasSettings : 1 = false;
bool requestWriteAccess : 1 = false;
};
struct AddToMenuOpenAttach {
QString startCommand;
PeerTypes chooseTypes;
};
struct AddToMenuOpenMenu {
QString startCommand;
};
struct AddToMenuOpenApp {
not_null<BotAppData*> app;
QString startCommand;
};
using AddToMenuOpen = std::variant<
AddToMenuOpenAttach,
AddToMenuOpenMenu,
AddToMenuOpenApp>;
2023-08-29 17:36:12 +00:00
class AttachWebView final
: public base::has_weak_ptr
, public Ui::BotWebView::Delegate {
public:
explicit AttachWebView(not_null<Main::Session*> session);
~AttachWebView();
struct WebViewButton {
QString text;
2022-03-31 09:24:13 +00:00
QString startCommand;
QByteArray url;
2023-09-06 09:41:23 +00:00
bool fromAttachMenu = false;
bool fromMainMenu = false;
bool fromSwitch = false;
};
2022-03-31 09:24:13 +00:00
void request(
not_null<Window::SessionController*> controller,
2022-10-28 07:57:09 +00:00
const Api::SendAction &action,
2022-03-31 09:24:13 +00:00
const QString &botUsername,
const QString &startCommand);
void request(
not_null<Window::SessionController*> controller,
2022-10-28 07:57:09 +00:00
const Api::SendAction &action,
not_null<UserData*> bot,
2022-03-31 09:24:13 +00:00
const WebViewButton &button);
2022-03-29 09:21:10 +00:00
void requestSimple(
not_null<Window::SessionController*> controller,
2022-03-29 09:21:10 +00:00
not_null<UserData*> bot,
const WebViewButton &button);
void requestMenu(
not_null<Window::SessionController*> controller,
not_null<UserData*> bot);
2023-03-03 08:35:49 +00:00
void requestApp(
not_null<Window::SessionController*> controller,
const Api::SendAction &action,
not_null<UserData*> bot,
const QString &appName,
const QString &startParam,
bool forceConfirmation);
void cancel();
2022-03-29 10:34:57 +00:00
void requestBots(Fn<void()> callback = nullptr);
[[nodiscard]] const std::vector<AttachWebViewBot> &attachBots() const {
return _attachBots;
}
[[nodiscard]] rpl::producer<> attachBotsUpdates() const {
return _attachBotsUpdates.events();
}
[[nodiscard]] bool disclaimerAccepted(
const AttachWebViewBot &bot) const;
[[nodiscard]] bool showMainMenuNewBadge(
const AttachWebViewBot &bot) const;
2022-03-31 09:24:13 +00:00
void requestAddToMenu(
not_null<UserData*> bot,
AddToMenuOpen open);
void requestAddToMenu(
2022-03-31 09:24:13 +00:00
not_null<UserData*> bot,
AddToMenuOpen open,
Window::SessionController *controller,
std::optional<Api::SendAction> action);
2022-03-29 14:14:11 +00:00
void removeFromMenu(not_null<UserData*> bot);
[[nodiscard]] std::optional<Api::SendAction> lookupLastAction(
const QString &url) const;
2022-03-29 10:34:57 +00:00
static void ClearAll();
private:
struct Context;
2023-08-29 17:36:12 +00:00
Webview::ThemeParams botThemeParams() override;
bool botHandleLocalUri(QString uri) override;
void botHandleInvoice(QString slug) override;
void botHandleMenuButton(Ui::BotWebView::MenuButton button) override;
void botSendData(QByteArray data) override;
void botSwitchInlineQuery(
std::vector<QString> chatTypes,
QString query) override;
void botCheckWriteAccess(Fn<void(bool allowed)> callback) override;
void botAllowWriteAccess(Fn<void(bool allowed)> callback) override;
void botSharePhone(Fn<void(bool shared)> callback) override;
void botInvokeCustomMethod(
Ui::BotWebView::CustomMethodRequest request) override;
void botClose() override;
[[nodiscard]] static Context LookupContext(
not_null<Window::SessionController*> controller,
const Api::SendAction &action);
[[nodiscard]] static bool IsSame(
const std::unique_ptr<Context> &a,
const Context &b);
void requestWithOptionalConfirm(
not_null<UserData*> bot,
const WebViewButton &button,
const Context &context,
Window::SessionController *controllerForConfirm = nullptr);
void resolve();
2022-03-31 09:24:13 +00:00
void request(const WebViewButton &button);
void requestSimple(const WebViewButton &button);
void resolveUsername(
const QString &username,
Fn<void(not_null<PeerData*>)> done);
void confirmOpen(
not_null<Window::SessionController*> controller,
Fn<void()> done);
void acceptMainMenuDisclaimer(
2023-09-06 09:41:23 +00:00
not_null<Window::SessionController*> controller,
const WebViewButton &button);
enum class ToggledState {
Removed,
Added,
AllowedToWrite,
};
2022-03-29 14:14:11 +00:00
void toggleInMenu(
not_null<UserData*> bot,
ToggledState state,
2022-03-29 16:12:47 +00:00
Fn<void()> callback = nullptr);
void show(
uint64 queryId,
const QString &url,
const QString &buttonText = QString(),
bool allowClipboardRead = false,
const BotAppData *app = nullptr,
bool fromMainMenu = false);
2022-03-29 14:14:11 +00:00
void confirmAddToMenu(
AttachWebViewBot bot,
Fn<void()> callback = nullptr);
2023-03-03 08:35:49 +00:00
void confirmAppOpen(bool requestWriteAccess);
void requestAppView(bool allowWrite);
2022-03-29 10:34:57 +00:00
void started(uint64 queryId);
2023-03-03 08:35:49 +00:00
void showToast(
const QString &text,
Window::SessionController *controller = nullptr);
const not_null<Main::Session*> _session;
2023-09-06 09:41:23 +00:00
base::Timer _refreshTimer;
std::unique_ptr<Context> _context;
std::unique_ptr<Context> _lastShownContext;
QString _lastShownUrl;
2023-08-29 17:36:12 +00:00
uint64 _lastShownQueryId = 0;
QString _lastShownButtonText;
UserData *_bot = nullptr;
QString _botUsername;
2023-03-03 08:35:49 +00:00
QString _botAppName;
2022-03-31 09:24:13 +00:00
QString _startCommand;
2023-03-03 08:35:49 +00:00
BotAppData *_app = nullptr;
QPointer<Ui::GenericBox> _confirmAddBox;
bool _appConfirmationRequired = false;
bool _appRequestWriteAccess = false;
mtpRequestId _requestId = 0;
2022-03-29 10:34:57 +00:00
mtpRequestId _prolongId = 0;
uint64 _botsHash = 0;
mtpRequestId _botsRequestId = 0;
std::vector<Fn<void()>> _botsRequestCallbacks;
std::unique_ptr<Context> _addToMenuContext;
2022-03-29 14:14:11 +00:00
UserData *_addToMenuBot = nullptr;
mtpRequestId _addToMenuId = 0;
AddToMenuOpen _addToMenuOpen;
2022-06-02 20:26:41 +00:00
base::weak_ptr<Window::SessionController> _addToMenuChooseController;
2022-03-29 14:14:11 +00:00
std::vector<AttachWebViewBot> _attachBots;
rpl::event_stream<> _attachBotsUpdates;
base::flat_set<not_null<UserData*>> _disclaimerAccepted;
std::unique_ptr<Ui::BotWebView::Panel> _panel;
};
[[nodiscard]] std::unique_ptr<Ui::DropdownMenu> MakeAttachBotsMenu(
not_null<QWidget*> parent,
not_null<Window::SessionController*> controller,
2022-04-28 12:16:26 +00:00
not_null<PeerData*> peer,
2022-10-28 07:57:09 +00:00
Fn<Api::SendAction()> actionFactory,
Fn<void(bool)> attach);
2023-09-06 09:41:23 +00:00
class MenuBotIcon final : public Ui::RpWidget {
public:
MenuBotIcon(
QWidget *parent,
std::shared_ptr<Data::DocumentMedia> media);
private:
void paintEvent(QPaintEvent *e) override;
void validate();
std::shared_ptr<Data::DocumentMedia> _media;
QImage _image;
QImage _mask;
};
} // namespace InlineBots