More auto-switching between accounts.

This commit is contained in:
John Preston 2020-06-26 15:48:28 +04:00
parent 6960e4808a
commit 6068dc418d
2 changed files with 86 additions and 77 deletions

View file

@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwidget.h" #include "mainwidget.h"
#include "apiwrap.h" #include "apiwrap.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "main/main_domain.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "boxes/url_auth_box.h" #include "boxes/url_auth_box.h"
#include "ui/layers/layer_widget.h" #include "ui/layers/layer_widget.h"
@ -32,24 +33,41 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/style_history.h" #include "styles/style_history.h"
#include "data/data_session.h" #include "data/data_session.h"
namespace {
[[nodiscard]] MainWidget *CheckMainWidget(not_null<Main::Session*> session) {
if (const auto m = App::main()) { // multi good
if (&m->session() == session) {
return m;
}
}
if (&Core::App().domain().active() != &session->account()) {
Core::App().domain().activate(&session->account());
}
if (const auto m = App::main()) { // multi good
if (&m->session() == session) {
return m;
}
}
return nullptr;
}
} // namespace
namespace App { namespace App {
void sendBotCommand( void sendBotCommand(
not_null<PeerData*> peer, not_null<PeerData*> peer,
UserData *bot, UserData *bot,
const QString &cmd, MsgId replyTo) { const QString &cmd, MsgId replyTo) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&peer->session())) {
if (&m->session() == &peer->session()) { m->sendBotCommand(peer, bot, cmd, replyTo);
m->sendBotCommand(peer, bot, cmd, replyTo);
}
} }
} }
void hideSingleUseKeyboard(not_null<const HistoryItem*> message) { void hideSingleUseKeyboard(not_null<const HistoryItem*> message) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&message->history()->session())) {
if (&m->session() == &message->history()->session()) { m->hideSingleUseKeyboard(message->history()->peer, message->id);
m->hideSingleUseKeyboard(message->history()->peer, message->id);
}
} }
} }
@ -88,10 +106,8 @@ void activateBotCommand(
case ButtonType::Callback: case ButtonType::Callback:
case ButtonType::Game: { case ButtonType::Game: {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&msg->history()->session())) {
if (&m->session() == &msg->history()->session()) { m->app_sendBotCallback(button, msg, row, column);
m->app_sendBotCallback(button, msg, row, column);
}
} }
} break; } break;
@ -145,34 +161,30 @@ void activateBotCommand(
chosen |= PollData::Flag::Quiz; chosen |= PollData::Flag::Quiz;
} }
} }
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&msg->history()->session())) {
if (&m->session() == &msg->history()->session()) { Window::PeerMenuCreatePoll(m->controller(), msg->history()->peer, chosen, disabled);
Window::PeerMenuCreatePoll(m->controller(), msg->history()->peer, chosen, disabled);
}
} }
} break; } break;
case ButtonType::SwitchInlineSame: case ButtonType::SwitchInlineSame:
case ButtonType::SwitchInline: { case ButtonType::SwitchInline: {
const auto session = &msg->history()->session(); const auto session = &msg->history()->session();
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(session)) {
if (&m->session() == session) { if (const auto bot = msg->getMessageBot()) {
if (const auto bot = msg->getMessageBot()) { const auto fastSwitchDone = [&] {
const auto fastSwitchDone = [&] { auto samePeer = (button->type == ButtonType::SwitchInlineSame);
auto samePeer = (button->type == ButtonType::SwitchInlineSame); if (samePeer) {
if (samePeer) { Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data), bot, msg->id);
Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data), bot, msg->id); return true;
} else if (bot->isBot() && bot->botInfo->inlineReturnPeerId) {
if (Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data))) {
return true; return true;
} else if (bot->isBot() && bot->botInfo->inlineReturnPeerId) {
if (Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data))) {
return true;
}
} }
return false;
}();
if (!fastSwitchDone) {
m->inlineSwitchLayer('@' + bot->username + ' ' + QString::fromUtf8(button->data));
} }
return false;
}();
if (!fastSwitchDone) {
m->inlineSwitchLayer('@' + bot->username + ' ' + QString::fromUtf8(button->data));
} }
} }
} }
@ -185,19 +197,20 @@ void activateBotCommand(
} }
void searchByHashtag(const QString &tag, PeerData *inPeer) { void searchByHashtag(const QString &tag, PeerData *inPeer) {
if (const auto m = App::main()) { // multi good const auto m = inPeer
if (!inPeer || &m->session() == &inPeer->session()) { ? CheckMainWidget(&inPeer->session())
if (m->controller()->openedFolder().current()) { : App::main(); // multi good
m->controller()->closeFolder(); if (m) {
} if (m->controller()->openedFolder().current()) {
Ui::hideSettingsAndLayer(); m->controller()->closeFolder();
Core::App().hideMediaView();
m->searchMessages(
tag + ' ',
(inPeer && !inPeer->isUser())
? inPeer->owner().history(inPeer).get()
: Dialogs::Key());
} }
Ui::hideSettingsAndLayer();
Core::App().hideMediaView();
m->searchMessages(
tag + ' ',
(inPeer && !inPeer->isUser())
? inPeer->owner().history(inPeer).get()
: Dialogs::Key());
} }
} }
@ -218,6 +231,14 @@ void showPeerProfile(not_null<PeerData*> peer) {
controller->showPeerInfo(peer); controller->showPeerInfo(peer);
} }
} }
if (&Core::App().domain().active() != &peer->session().account()) {
Core::App().domain().activate(&peer->session().account());
}
if (const auto controller = window->sessionController()) {
if (&controller->session() == &peer->session()) {
controller->showPeerInfo(peer);
}
}
} }
} }
@ -226,13 +247,11 @@ void showPeerProfile(not_null<const History*> history) {
} }
void showChatsList(not_null<Main::Session*> session) { void showChatsList(not_null<Main::Session*> session) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(session)) {
if (&m->session() == session) { m->ui_showPeerHistory(
m->ui_showPeerHistory( 0,
0, Window::SectionShow::Way::ClearStack,
Window::SectionShow::Way::ClearStack, 0);
0);
}
} }
} }
@ -245,13 +264,11 @@ void showPeerHistory(not_null<const History*> history, MsgId msgId) {
} }
void showPeerHistory(not_null<const PeerData*> peer, MsgId msgId) { void showPeerHistory(not_null<const PeerData*> peer, MsgId msgId) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&peer->session())) {
if (&m->session() == &peer->session()) { m->ui_showPeerHistory(
m->ui_showPeerHistory( peer->id,
peer->id, Window::SectionShow::Way::ClearStack,
Window::SectionShow::Way::ClearStack, msgId);
msgId);
}
} }
} }
@ -273,10 +290,8 @@ bool skipPaintEvent(QWidget *widget, QPaintEvent *event) {
namespace Notify { namespace Notify {
void replyMarkupUpdated(not_null<const HistoryItem*> item) { void replyMarkupUpdated(not_null<const HistoryItem*> item) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&item->history()->session())) {
if (&m->session() == &item->history()->session()) { m->notify_replyMarkupUpdated(item);
m->notify_replyMarkupUpdated(item);
}
} }
} }
@ -284,13 +299,11 @@ void inlineKeyboardMoved(
not_null<const HistoryItem*> item, not_null<const HistoryItem*> item,
int oldKeyboardTop, int oldKeyboardTop,
int newKeyboardTop) { int newKeyboardTop) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(&item->history()->session())) {
if (&m->session() == &item->history()->session()) { m->notify_inlineKeyboardMoved(
m->notify_inlineKeyboardMoved( item,
item, oldKeyboardTop,
oldKeyboardTop, newKeyboardTop);
newKeyboardTop);
}
} }
} }
@ -299,13 +312,11 @@ bool switchInlineBotButtonReceived(
const QString &query, const QString &query,
UserData *samePeerBot, UserData *samePeerBot,
MsgId samePeerReplyTo) { MsgId samePeerReplyTo) {
if (const auto m = App::main()) { // multi good if (const auto m = CheckMainWidget(session)) {
if (session == &m->session()) { return m->notify_switchInlineBotButtonReceived(
return m->notify_switchInlineBotButtonReceived( query,
query, samePeerBot,
samePeerBot, samePeerReplyTo);
samePeerReplyTo);
}
} }
return false; return false;
} }

View file

@ -1219,7 +1219,6 @@ void OverlayWidget::onToMessage() {
} }
if (const auto item = _session->data().message(_msgid)) { if (const auto item = _session->data().message(_msgid)) {
close(); close();
Core::App().domain().activate(&_session->account());
Ui::showPeerHistoryAtItem(item); Ui::showPeerHistoryAtItem(item);
} }
} }
@ -3852,7 +3851,6 @@ void OverlayWidget::mouseReleaseEvent(QMouseEvent *e) {
if (_over == OverName && _down == OverName) { if (_over == OverName && _down == OverName) {
if (_from) { if (_from) {
close(); close();
Core::App().domain().activate(&_from->account());
Ui::showPeerProfile(_from); Ui::showPeerProfile(_from);
} }
} else if (_over == OverDate && _down == OverDate) { } else if (_over == OverDate && _down == OverDate) {