2019-05-22 14:29:02 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
#include "boxes/url_auth_box.h"
|
|
|
|
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history_item_components.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "core/click_handler_types.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-05-22 14:29:02 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
|
|
|
|
void UrlAuthBox::Activate(
|
|
|
|
not_null<const HistoryItem*> message,
|
|
|
|
int row,
|
|
|
|
int column) {
|
|
|
|
const auto itemId = message->fullId();
|
|
|
|
const auto button = HistoryMessageMarkupButton::Get(itemId, row, column);
|
|
|
|
if (button->requestId || !IsServerMsgId(itemId.msg)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto session = &message->history()->session();
|
|
|
|
const auto inputPeer = message->history()->peer->input;
|
|
|
|
const auto buttonId = button->buttonId;
|
|
|
|
const auto url = QString::fromUtf8(button->data);
|
|
|
|
|
|
|
|
button->requestId = session->api().request(MTPmessages_RequestUrlAuth(
|
|
|
|
inputPeer,
|
|
|
|
MTP_int(itemId.msg),
|
|
|
|
MTP_int(buttonId)
|
|
|
|
)).done([=](const MTPUrlAuthResult &result) {
|
|
|
|
const auto button = HistoryMessageMarkupButton::Get(
|
|
|
|
itemId,
|
|
|
|
row,
|
|
|
|
column);
|
|
|
|
if (!button) return;
|
|
|
|
|
|
|
|
button->requestId = 0;
|
|
|
|
result.match([&](const MTPDurlAuthResultAccepted &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
UrlClickHandler::Open(qs(data.vurl()));
|
2019-05-22 14:29:02 +00:00
|
|
|
}, [&](const MTPDurlAuthResultDefault &data) {
|
|
|
|
HiddenUrlClickHandler::Open(url);
|
|
|
|
}, [&](const MTPDurlAuthResultRequest &data) {
|
|
|
|
Request(data, session->data().message(itemId), row, column);
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
const auto button = HistoryMessageMarkupButton::Get(
|
|
|
|
itemId,
|
|
|
|
row,
|
|
|
|
column);
|
|
|
|
if (!button) return;
|
|
|
|
|
|
|
|
button->requestId = 0;
|
|
|
|
HiddenUrlClickHandler::Open(url);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UrlAuthBox::Request(
|
|
|
|
const MTPDurlAuthResultRequest &request,
|
|
|
|
not_null<const HistoryItem*> message,
|
|
|
|
int row,
|
|
|
|
int column) {
|
|
|
|
const auto itemId = message->fullId();
|
|
|
|
const auto button = HistoryMessageMarkupButton::Get(itemId, row, column);
|
|
|
|
if (button->requestId || !IsServerMsgId(itemId.msg)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto session = &message->history()->session();
|
|
|
|
const auto inputPeer = message->history()->peer->input;
|
|
|
|
const auto buttonId = button->buttonId;
|
|
|
|
const auto url = QString::fromUtf8(button->data);
|
|
|
|
|
|
|
|
const auto bot = request.is_request_write_access()
|
2019-07-05 13:38:38 +00:00
|
|
|
? session->data().processUser(request.vbot()).get()
|
2019-05-22 14:29:02 +00:00
|
|
|
: nullptr;
|
|
|
|
const auto box = std::make_shared<QPointer<BoxContent>>();
|
|
|
|
const auto finishWithUrl = [=](const QString &url) {
|
|
|
|
if (*box) {
|
|
|
|
(*box)->closeBox();
|
|
|
|
}
|
|
|
|
UrlClickHandler::Open(url);
|
|
|
|
};
|
|
|
|
const auto callback = [=](Result result) {
|
|
|
|
if (result == Result::None) {
|
|
|
|
finishWithUrl(url);
|
|
|
|
} else if (const auto msg = session->data().message(itemId)) {
|
|
|
|
const auto allowWrite = (result == Result::AuthAndAllowWrite);
|
|
|
|
using Flag = MTPmessages_AcceptUrlAuth::Flag;
|
|
|
|
session->api().request(MTPmessages_AcceptUrlAuth(
|
|
|
|
MTP_flags(allowWrite ? Flag::f_write_allowed : Flag(0)),
|
|
|
|
inputPeer,
|
|
|
|
MTP_int(itemId.msg),
|
|
|
|
MTP_int(buttonId)
|
|
|
|
)).done([=](const MTPUrlAuthResult &result) {
|
|
|
|
const auto to = result.match(
|
|
|
|
[&](const MTPDurlAuthResultAccepted &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
return qs(data.vurl());
|
2019-05-22 14:29:02 +00:00
|
|
|
}, [&](const MTPDurlAuthResultDefault &data) {
|
|
|
|
return url;
|
|
|
|
}, [&](const MTPDurlAuthResultRequest &data) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"got urlAuthResultRequest after acceptUrlAuth."));
|
|
|
|
return url;
|
|
|
|
});
|
|
|
|
finishWithUrl(to);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finishWithUrl(url);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*box = Ui::show(
|
2019-08-06 16:40:08 +00:00
|
|
|
Box<UrlAuthBox>(session, url, qs(request.vdomain()), bot, callback),
|
2019-05-22 14:29:02 +00:00
|
|
|
LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
UrlAuthBox::UrlAuthBox(
|
|
|
|
QWidget*,
|
2019-08-06 16:40:08 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-05-22 14:29:02 +00:00
|
|
|
const QString &url,
|
|
|
|
const QString &domain,
|
|
|
|
UserData *bot,
|
|
|
|
Fn<void(Result)> callback)
|
2019-08-06 16:40:08 +00:00
|
|
|
: _content(setupContent(session, url, domain, bot, std::move(callback))) {
|
2019-05-22 14:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UrlAuthBox::prepare() {
|
|
|
|
setDimensionsToContent(st::boxWidth, _content);
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_open_link(), [=] { _callback(); });
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2019-05-22 14:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Ui::RpWidget*> UrlAuthBox::setupContent(
|
2019-08-06 16:40:08 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-05-22 14:29:02 +00:00
|
|
|
const QString &url,
|
|
|
|
const QString &domain,
|
|
|
|
UserData *bot,
|
|
|
|
Fn<void(Result)> callback) {
|
|
|
|
const auto result = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
result->add(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
result,
|
2019-06-19 16:39:25 +00:00
|
|
|
tr::lng_url_auth_open_confirm(tr::now, lt_link, url),
|
2019-05-22 14:29:02 +00:00
|
|
|
st::boxLabel),
|
|
|
|
st::boxPadding);
|
|
|
|
const auto addCheckbox = [&](const QString &text) {
|
|
|
|
const auto checkbox = result->add(
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
result,
|
|
|
|
QString(),
|
2019-05-25 14:44:15 +00:00
|
|
|
true,
|
2019-05-22 14:29:02 +00:00
|
|
|
st::urlAuthCheckbox),
|
|
|
|
style::margins(
|
|
|
|
st::boxPadding.left(),
|
|
|
|
st::boxPadding.bottom(),
|
|
|
|
st::boxPadding.right(),
|
|
|
|
st::boxPadding.bottom()));
|
|
|
|
checkbox->setAllowMultiline(true);
|
|
|
|
checkbox->setText(text, true);
|
|
|
|
return checkbox;
|
|
|
|
};
|
|
|
|
const auto auth = addCheckbox(
|
2019-06-19 16:39:25 +00:00
|
|
|
tr::lng_url_auth_login_option(
|
|
|
|
tr::now,
|
2019-05-22 14:29:02 +00:00
|
|
|
lt_domain,
|
|
|
|
textcmdStartSemibold() + domain + textcmdStopSemibold(),
|
|
|
|
lt_user,
|
|
|
|
(textcmdStartSemibold()
|
2019-08-06 16:40:08 +00:00
|
|
|
+ App::peerName(session->user())
|
2019-05-22 14:29:02 +00:00
|
|
|
+ textcmdStopSemibold())));
|
|
|
|
const auto allow = bot
|
2019-06-19 16:39:25 +00:00
|
|
|
? addCheckbox(tr::lng_url_auth_allow_messages(
|
|
|
|
tr::now,
|
2019-05-22 14:29:02 +00:00
|
|
|
lt_bot,
|
|
|
|
textcmdStartSemibold() + bot->firstName + textcmdStopSemibold()))
|
|
|
|
: nullptr;
|
|
|
|
if (allow) {
|
|
|
|
rpl::single(
|
|
|
|
auth->checked()
|
|
|
|
) | rpl::then(
|
|
|
|
auth->checkedChanges()
|
|
|
|
) | rpl::start_with_next([=](bool checked) {
|
2019-05-25 14:44:15 +00:00
|
|
|
if (!checked) {
|
|
|
|
allow->setChecked(false);
|
|
|
|
}
|
2019-05-22 14:29:02 +00:00
|
|
|
allow->setDisabled(!checked);
|
|
|
|
}, auth->lifetime());
|
|
|
|
}
|
|
|
|
_callback = [=, callback = std::move(callback)]() {
|
|
|
|
const auto authed = auth->checked();
|
|
|
|
const auto allowed = (authed && allow && allow->checked());
|
|
|
|
const auto onstack = callback;
|
|
|
|
onstack(allowed
|
|
|
|
? Result::AuthAndAllowWrite
|
|
|
|
: authed
|
|
|
|
? Result::Auth
|
|
|
|
: Result::None);
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|