Provide privacy policy in mini-app.

This commit is contained in:
John Preston 2024-08-13 15:51:32 +02:00
parent 5f8c007a0c
commit b0fece2fd0
5 changed files with 76 additions and 23 deletions

View file

@ -13,13 +13,9 @@ struct BotCommand final {
QString command; QString command;
QString description; QString description;
inline bool operator==(const BotCommand &other) const { friend inline bool operator==(
return (command == other.command) const BotCommand &,
&& (description == other.description); const BotCommand &) = default;
}
inline bool operator!=(const BotCommand &other) const {
return !(*this == other);
}
}; };
[[nodiscard]] BotCommand BotCommandFromTL(const MTPBotCommand &result); [[nodiscard]] BotCommand BotCommandFromTL(const MTPBotCommand &result);

View file

@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_file_origin.h" #include "data/data_file_origin.h"
#include "data/data_document.h" #include "data/data_document.h"
#include "data/data_document_media.h" #include "data/data_document_media.h"
#include "data/data_peer_bot_command.h"
#include "data/data_session.h" #include "data/data_session.h"
#include "data/data_web_page.h" #include "data/data_web_page.h"
#include "main/main_app_config.h" #include "main/main_app_config.h"
@ -1216,7 +1217,7 @@ void WebViewInstance::botHandleMenuButton(
} }
break; break;
case Button::RemoveFromMenu: case Button::RemoveFromMenu:
case Button::RemoveFromMainMenu: case Button::RemoveFromMainMenu: {
const auto &bots = _session->attachWebView().attachBots(); const auto &bots = _session->attachWebView().attachBots();
const auto attached = ranges::find( const auto attached = ranges::find(
bots, bots,
@ -1248,7 +1249,19 @@ void WebViewInstance::botHandleMenuButton(
Ui::Text::WithEntities), Ui::Text::WithEntities),
done, done,
})); }));
break; } break;
case Button::ShareGame: {
const auto itemId = v::is<WebViewSourceGame>(_source)
? v::get<WebViewSourceGame>(_source).messageId
: FullMsgId();
if (!_panel || !itemId) {
return;
} else if (const auto item = _session->data().message(itemId)) {
FastShareMessage(uiShow(), item);
} else {
_panel->showToast({ tr::lng_message_not_found(tr::now) });
}
} break;
} }
} }
@ -1383,16 +1396,57 @@ void WebViewInstance::botInvokeCustomMethod(
}).send(); }).send();
} }
void WebViewInstance::botShareGameScore() { void WebViewInstance::botOpenPrivacyPolicy() {
const auto itemId = v::is<WebViewSourceGame>(_source) const auto bot = _bot;
? v::get<WebViewSourceGame>(_source).messageId const auto weak = _context.controller;
: FullMsgId(); const auto command = u"privacy"_q;
if (!_panel || !itemId) { const auto findCommand = [=] {
return; if (!bot->isBot()) {
} else if (const auto item = _session->data().message(itemId)) { return QString();
FastShareMessage(uiShow(), item); }
} else { for (const auto &data : bot->botInfo->commands) {
_panel->showToast({ tr::lng_message_not_found(tr::now) }); const auto isSame = !data.command.compare(
command,
Qt::CaseInsensitive);
if (isSame) {
return data.command;
}
}
return QString();
};
const auto makeOtherContext = [=](bool forceWindow) {
return QVariant::fromValue(ClickHandlerContext{
.sessionWindow = (forceWindow
? WindowForThread(weak, bot->owner().history(bot))
: weak),
.peer = bot,
});
};
const auto sendCommand = [=] {
const auto original = findCommand();
if (original.isEmpty()) {
return false;
}
BotCommandClickHandler('/' + original).onClick(ClickContext{
Qt::LeftButton,
makeOtherContext(true)
});
return true;
};
const auto openUrl = [=](const QString &url) {
Core::App().iv().openWithIvPreferred(
&_bot->session(),
url,
makeOtherContext(false));
};
if (const auto info = _bot->botInfo.get()) {
if (!info->privacyPolicyUrl.isEmpty()) {
openUrl(info->privacyPolicyUrl);
return;
}
}
if (!sendCommand()) {
openUrl(tr::lng_profile_bot_privacy_url(tr::now));
} }
} }

View file

@ -275,7 +275,7 @@ private:
void botSharePhone(Fn<void(bool shared)> callback) override; void botSharePhone(Fn<void(bool shared)> callback) override;
void botInvokeCustomMethod( void botInvokeCustomMethod(
Ui::BotWebView::CustomMethodRequest request) override; Ui::BotWebView::CustomMethodRequest request) override;
void botShareGameScore() override; void botOpenPrivacyPolicy() override;
void botClose() override; void botClose() override;
const std::shared_ptr<Ui::Show> _parentShow; const std::shared_ptr<Ui::Show> _parentShow;

View file

@ -538,12 +538,15 @@ bool Panel::showWebview(
}, &st::menuIconRestore); }, &st::menuIconRestore);
if (_menuButtons & MenuButton::ShareGame) { if (_menuButtons & MenuButton::ShareGame) {
callback(tr::lng_iv_share(tr::now), [=] { callback(tr::lng_iv_share(tr::now), [=] {
_delegate->botShareGameScore(); _delegate->botHandleMenuButton(MenuButton::ShareGame);
}, &st::menuIconShare); }, &st::menuIconShare);
} else { } else {
callback(tr::lng_bot_terms(tr::now), [=] { callback(tr::lng_bot_terms(tr::now), [=] {
File::OpenUrl(tr::lng_mini_apps_tos_url(tr::now)); File::OpenUrl(tr::lng_mini_apps_tos_url(tr::now));
}, &st::menuIconGroupLog); }, &st::menuIconGroupLog);
callback(tr::lng_profile_bot_privacy(tr::now), [=] {
_delegate->botOpenPrivacyPolicy();
}, &st::menuIconAntispam);
} }
const auto main = (_menuButtons & MenuButton::RemoveFromMainMenu); const auto main = (_menuButtons & MenuButton::RemoveFromMainMenu);
if (main || (_menuButtons & MenuButton::RemoveFromMenu)) { if (main || (_menuButtons & MenuButton::RemoveFromMenu)) {
@ -709,7 +712,7 @@ bool Panel::createWebview(const Webview::ThemeParams &params) {
} else if (command == "web_app_set_header_color") { } else if (command == "web_app_set_header_color") {
processHeaderColor(arguments); processHeaderColor(arguments);
} else if (command == "share_score") { } else if (command == "share_score") {
_delegate->botShareGameScore(); _delegate->botHandleMenuButton(MenuButton::ShareGame);
} }
}); });

View file

@ -72,7 +72,7 @@ public:
virtual void botAllowWriteAccess(Fn<void(bool allowed)> callback) = 0; virtual void botAllowWriteAccess(Fn<void(bool allowed)> callback) = 0;
virtual void botSharePhone(Fn<void(bool shared)> callback) = 0; virtual void botSharePhone(Fn<void(bool shared)> callback) = 0;
virtual void botInvokeCustomMethod(CustomMethodRequest request) = 0; virtual void botInvokeCustomMethod(CustomMethodRequest request) = 0;
virtual void botShareGameScore() = 0; virtual void botOpenPrivacyPolicy() = 0;
virtual void botClose() = 0; virtual void botClose() = 0;
}; };