Skip to content

Commit

Permalink
Pass context chat from one webview to another.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Aug 15, 2023
1 parent 87f52cf commit d57d95c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions Telegram/SourceFiles/core/click_handler_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions Telegram/SourceFiles/core/local_url_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ bool ResolveUsernameOrPhone(
? std::make_optional(params.value(u"voicechat"_q))
: std::nullopt),
.clickFromMessageId = myContext.itemId,
.clickFromAttachBotWebviewUrl = myContext.attachBotWebviewUrl,
});
return true;
}
Expand All @@ -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,
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Telegram/SourceFiles/data/data_stories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 16 additions & 3 deletions Telegram/SourceFiles/inline_bots/bot_attach_web_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ For license and copyright information please follow this link:
#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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
};
Expand Down Expand Up @@ -1142,6 +1154,7 @@ void AttachWebView::show(
}
});

_lastShownUrl = url;
_panel = Ui::BotWebView::Show({
.url = url,
.userDataPath = _session->domain().local().webviewDataPath(),
Expand Down
5 changes: 5 additions & 0 deletions Telegram/SourceFiles/inline_bots/bot_attach_web_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class AttachWebView final : public base::has_weak_ptr {
PeerTypes chooseTypes);
void removeFromMenu(not_null<UserData*> bot);

[[nodiscard]] std::optional<Api::SendAction> lookupLastAction(
const QString &url) const;

static void ClearAll();

private:
Expand Down Expand Up @@ -180,6 +183,8 @@ class AttachWebView final : public base::has_weak_ptr {
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;
Expand Down
5 changes: 4 additions & 1 deletion Telegram/SourceFiles/window/window_session_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/window/window_session_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class SessionNavigation : public base::has_weak_ptr {
InlineBots::PeerTypes attachBotChooseTypes;
std::optional<QString> voicechatHash;
FullMsgId clickFromMessageId;
QString clickFromAttachBotWebviewUrl;
};
void showPeerByLink(const PeerByLinkInfo &info);

Expand Down

0 comments on commit d57d95c

Please sign in to comment.