Pass context chat from one webview to another.

This commit is contained in:
John Preston 2023-08-15 15:01:18 +02:00
parent 87f52cf7df
commit d57d95c53f
7 changed files with 33 additions and 7 deletions

View File

@ -38,6 +38,7 @@ class SessionController;
class PeerData;
struct ClickHandlerContext {
FullMsgId itemId;
QString attachBotWebviewUrl;
// Is filled from sections.
Fn<HistoryView::ElementDelegate*()> elementDelegate;
base::weak_ptr<Window::SessionController> sessionWindow;

View File

@ -447,6 +447,7 @@ bool ResolveUsernameOrPhone(
? std::make_optional(params.value(u"voicechat"_q))
: std::nullopt),
.clickFromMessageId = myContext.itemId,
.clickFromAttachBotWebviewUrl = myContext.attachBotWebviewUrl,
});
return true;
}
@ -473,7 +474,7 @@ bool ResolvePrivatePost(
if (!channelId || (msgId && !IsServerMsgId(msgId))) {
return false;
}
const auto fromMessageId = context.value<ClickHandlerContext>().itemId;
const auto my = context.value<ClickHandlerContext>();
using Navigation = Window::SessionNavigation;
controller->showPeerByLink(Navigation::PeerByLinkInfo{
.usernameOrId = channelId,
@ -487,7 +488,8 @@ bool ResolvePrivatePost(
Navigation::ThreadId{ threadId }
}
: Navigation::RepliesByLinkInfo{ v::null },
.clickFromMessageId = fromMessageId,
.clickFromMessageId = my.itemId,
.clickFromAttachBotWebviewUrl = my.attachBotWebviewUrl,
});
controller->window().activate();
return true;

View File

@ -345,7 +345,8 @@ void Stories::parseAndApply(const MTPUserStories &stories) {
}
sort(list);
};
if (result.user->isBot()
if (result.user->isSelf()
|| result.user->isBot()
|| result.user->isServiceUser()
|| result.user->isContact()) {
const auto hidden = result.user->hasStoriesHidden();

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "inline_bots/bot_attach_web_view.h"
#include "api/api_common.h"
#include "core/click_handler_types.h"
#include "data/data_bot_app.h"
#include "data/data_user.h"
#include "data/data_file_origin.h"
@ -568,7 +569,7 @@ void AttachWebView::cancel() {
_session->api().request(base::take(_requestId)).cancel();
_session->api().request(base::take(_prolongId)).cancel();
_panel = nullptr;
_context = nullptr;
_lastShownContext = base::take(_context);
_bot = nullptr;
_app = nullptr;
_botUsername = QString();
@ -713,6 +714,14 @@ void AttachWebView::removeFromMenu(not_null<UserData*> bot) {
});
}
std::optional<Api::SendAction> AttachWebView::lookupLastAction(
const QString &url) const {
if (_lastShownUrl == url && _lastShownContext) {
return _lastShownContext->action;
}
return std::nullopt;
}
void AttachWebView::resolve() {
resolveUsername(_botUsername, [=](not_null<PeerData*> bot) {
if (!_context) {
@ -1049,7 +1058,7 @@ void AttachWebView::show(
}
crl::on_main(this, [=] { cancel(); });
});
const auto handleLocalUri = [close](QString uri) {
const auto handleLocalUri = [close, url](QString uri) {
const auto local = Core::TryConvertUrlToLocal(uri);
if (uri == local || Core::InternalPassportLink(local)) {
return local.startsWith(u"tg://"_q);
@ -1058,7 +1067,10 @@ void AttachWebView::show(
}
close();
crl::on_main([=] {
UrlClickHandler::Open(local, {});
const auto variant = QVariant::fromValue(ClickHandlerContext{
.attachBotWebviewUrl = url,
});
UrlClickHandler::Open(local, variant);
});
return true;
};
@ -1142,6 +1154,7 @@ void AttachWebView::show(
}
});
_lastShownUrl = url;
_panel = Ui::BotWebView::Show({
.url = url,
.userDataPath = _session->domain().local().webviewDataPath(),

View File

@ -122,6 +122,9 @@ public:
PeerTypes chooseTypes);
void removeFromMenu(not_null<UserData*> bot);
[[nodiscard]] std::optional<Api::SendAction> lookupLastAction(
const QString &url) const;
static void ClearAll();
private:
@ -180,6 +183,8 @@ private:
const not_null<Main::Session*> _session;
std::unique_ptr<Context> _context;
std::unique_ptr<Context> _lastShownContext;
QString _lastShownUrl;
UserData *_bot = nullptr;
QString _botUsername;
QString _botAppName;

View File

@ -521,10 +521,13 @@ void SessionNavigation::showPeerByLinkResolved(
const auto contextPeer = item
? item->history()->peer
: bot;
const auto action = bot->session().attachWebView().lookupLastAction(
info.clickFromAttachBotWebviewUrl
).value_or(Api::SendAction(bot->owner().history(contextPeer)));
crl::on_main(this, [=] {
bot->session().attachWebView().requestApp(
parentController(),
Api::SendAction(bot->owner().history(contextPeer)),
action,
bot,
info.botAppName,
info.startToken,

View File

@ -217,6 +217,7 @@ public:
InlineBots::PeerTypes attachBotChooseTypes;
std::optional<QString> voicechatHash;
FullMsgId clickFromMessageId;
QString clickFromAttachBotWebviewUrl;
};
void showPeerByLink(const PeerByLinkInfo &info);