2020-09-08 15:01:12 +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 "api/api_authorizations.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "core/changelogs.h"
|
2021-11-16 16:48:40 +00:00
|
|
|
#include "core/application.h"
|
2022-06-11 00:07:22 +00:00
|
|
|
#include "core/core_settings.h"
|
2020-09-08 15:01:12 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto TestApiId = 17349;
|
2021-11-30 08:36:12 +00:00
|
|
|
constexpr auto SnapApiId = 611335;
|
2020-09-08 15:01:12 +00:00
|
|
|
constexpr auto DesktopApiId = 2040;
|
|
|
|
|
|
|
|
Authorizations::Entry ParseEntry(const MTPDauthorization &data) {
|
|
|
|
auto result = Authorizations::Entry();
|
|
|
|
|
|
|
|
result.hash = data.is_current() ? 0 : data.vhash().v;
|
|
|
|
result.incomplete = data.is_password_pending();
|
|
|
|
|
2021-11-30 08:36:12 +00:00
|
|
|
const auto apiId = result.apiId = data.vapi_id().v;
|
2020-09-08 15:01:12 +00:00
|
|
|
const auto isTest = (apiId == TestApiId);
|
2021-11-30 08:36:12 +00:00
|
|
|
const auto isDesktop = (apiId == DesktopApiId)
|
|
|
|
|| (apiId == SnapApiId)
|
|
|
|
|| isTest;
|
2020-09-08 15:01:12 +00:00
|
|
|
|
|
|
|
const auto appName = isDesktop
|
2022-11-29 21:46:36 +00:00
|
|
|
? u"Telegram Desktop%1"_q.arg(isTest ? " (GitHub)" : QString())
|
|
|
|
: qs(data.vapp_name());// + u" for "_q + qs(d.vplatform());
|
2020-09-08 15:01:12 +00:00
|
|
|
const auto appVer = [&] {
|
|
|
|
const auto version = qs(data.vapp_version());
|
|
|
|
if (isDesktop) {
|
|
|
|
const auto verInt = version.toInt();
|
|
|
|
if (version == QString::number(verInt)) {
|
|
|
|
return Core::FormatVersionDisplay(verInt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (const auto index = version.indexOf('('); index >= 0) {
|
|
|
|
return version.mid(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}();
|
|
|
|
|
2021-11-16 16:48:40 +00:00
|
|
|
result.name = result.hash
|
|
|
|
? qs(data.vdevice_model())
|
|
|
|
: Core::App().settings().deviceModel();
|
2020-09-08 15:01:12 +00:00
|
|
|
|
|
|
|
const auto country = qs(data.vcountry());
|
2021-11-16 14:42:33 +00:00
|
|
|
//const auto platform = qs(data.vplatform());
|
2020-09-08 15:01:12 +00:00
|
|
|
//const auto &countries = countriesByISO2();
|
|
|
|
//const auto j = countries.constFind(country);
|
|
|
|
//if (j != countries.cend()) {
|
|
|
|
// country = QString::fromUtf8(j.value()->name);
|
|
|
|
//}
|
2021-11-26 13:01:37 +00:00
|
|
|
result.system = qs(data.vsystem_version());
|
2021-11-30 08:36:12 +00:00
|
|
|
result.platform = qs(data.vplatform());
|
2020-09-08 15:01:12 +00:00
|
|
|
result.activeTime = data.vdate_active().v
|
|
|
|
? data.vdate_active().v
|
|
|
|
: data.vdate_created().v;
|
2021-11-16 14:42:33 +00:00
|
|
|
result.info = QString("%1%2").arg(
|
|
|
|
appName,
|
|
|
|
appVer.isEmpty() ? QString() : (' ' + appVer));
|
|
|
|
result.ip = qs(data.vip());
|
2020-09-08 15:01:12 +00:00
|
|
|
if (!result.hash) {
|
|
|
|
result.active = tr::lng_status_online(tr::now);
|
|
|
|
} else {
|
|
|
|
const auto now = QDateTime::currentDateTime();
|
|
|
|
const auto lastTime = base::unixtime::parse(result.activeTime);
|
|
|
|
const auto nowDate = now.date();
|
|
|
|
const auto lastDate = lastTime.date();
|
|
|
|
if (lastDate == nowDate) {
|
2022-10-17 03:51:59 +00:00
|
|
|
result.active = QLocale().toString(lastTime, cTimeFormat());
|
2020-09-08 15:01:12 +00:00
|
|
|
} else if (lastDate.year() == nowDate.year()
|
|
|
|
&& lastDate.weekNumber() == nowDate.weekNumber()) {
|
|
|
|
result.active = langDayOfWeek(lastDate);
|
|
|
|
} else {
|
2022-10-17 03:51:59 +00:00
|
|
|
result.active = QLocale().toString(lastDate, cDateFormat());
|
2020-09-08 15:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-26 13:01:37 +00:00
|
|
|
result.location = country;
|
2020-09-08 15:01:12 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Authorizations::Authorizations(not_null<ApiWrap*> api)
|
|
|
|
: _api(&api->instance()) {
|
2021-11-16 16:48:40 +00:00
|
|
|
Core::App().settings().deviceModelChanges(
|
|
|
|
) | rpl::start_with_next([=](const QString &model) {
|
|
|
|
auto changed = false;
|
|
|
|
for (auto &entry : _list) {
|
|
|
|
if (!entry.hash) {
|
|
|
|
entry.name = model;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
_listChanges.fire({});
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2021-11-26 14:01:58 +00:00
|
|
|
|
|
|
|
if (Core::App().settings().disableCallsLegacy()) {
|
|
|
|
toggleCallsDisabledHere(true);
|
|
|
|
}
|
2020-09-08 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Authorizations::reload() {
|
|
|
|
if (_requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestId = _api.request(MTPaccount_GetAuthorizations(
|
|
|
|
)).done([=](const MTPaccount_Authorizations &result) {
|
|
|
|
_requestId = 0;
|
2020-09-18 19:04:18 +00:00
|
|
|
_lastReceived = crl::now();
|
2020-09-08 15:01:12 +00:00
|
|
|
result.match([&](const MTPDaccount_authorizations &auths) {
|
2021-11-25 20:52:06 +00:00
|
|
|
_ttlDays = auths.vauthorization_ttl_days().v;
|
2020-09-08 15:01:12 +00:00
|
|
|
_list = (
|
|
|
|
auths.vauthorizations().v
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](const MTPAuthorization &d) {
|
2020-09-08 15:01:12 +00:00
|
|
|
return ParseEntry(d.c_authorization());
|
|
|
|
}) | ranges::to<List>;
|
|
|
|
_listChanges.fire({});
|
|
|
|
});
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-09-08 15:01:12 +00:00
|
|
|
_requestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Authorizations::cancelCurrentRequest() {
|
|
|
|
_api.request(base::take(_requestId)).cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Authorizations::requestTerminate(
|
|
|
|
Fn<void(const MTPBool &result)> &&done,
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const MTP::Error &error)> &&fail,
|
2020-09-08 15:01:12 +00:00
|
|
|
std::optional<uint64> hash) {
|
2020-12-15 14:23:41 +00:00
|
|
|
const auto send = [&](auto request) {
|
|
|
|
_api.request(
|
|
|
|
std::move(request)
|
2021-02-16 22:25:57 +00:00
|
|
|
).done([=, done = std::move(done)](const MTPBool &result) {
|
|
|
|
done(result);
|
|
|
|
if (mtpIsTrue(result)) {
|
|
|
|
if (hash) {
|
|
|
|
_list.erase(
|
|
|
|
ranges::remove(_list, *hash, &Entry::hash),
|
|
|
|
end(_list));
|
|
|
|
} else {
|
|
|
|
_list.clear();
|
|
|
|
}
|
|
|
|
_listChanges.fire({});
|
|
|
|
}
|
|
|
|
}).fail(
|
2020-12-15 14:23:41 +00:00
|
|
|
std::move(fail)
|
|
|
|
).send();
|
|
|
|
};
|
|
|
|
if (hash) {
|
|
|
|
send(MTPaccount_ResetAuthorization(MTP_long(*hash)));
|
|
|
|
} else {
|
|
|
|
send(MTPauth_ResetAuthorizations());
|
|
|
|
}
|
2020-09-08 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Authorizations::List Authorizations::list() const {
|
|
|
|
return _list;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Authorizations::listChanges() const
|
|
|
|
-> rpl::producer<Authorizations::List> {
|
|
|
|
return rpl::single(
|
|
|
|
list()
|
|
|
|
) | rpl::then(
|
|
|
|
_listChanges.events() | rpl::map([=] { return list(); }));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> Authorizations::totalChanges() const {
|
|
|
|
return rpl::single(
|
|
|
|
total()
|
|
|
|
) | rpl::then(
|
|
|
|
_listChanges.events() | rpl::map([=] { return total(); }));
|
|
|
|
}
|
|
|
|
|
2021-11-25 20:52:06 +00:00
|
|
|
void Authorizations::updateTTL(int days) {
|
|
|
|
_api.request(_ttlRequestId).cancel();
|
|
|
|
_ttlRequestId = _api.request(MTPaccount_SetAuthorizationTTL(
|
|
|
|
MTP_int(days)
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done([=] {
|
2021-11-25 20:52:06 +00:00
|
|
|
_ttlRequestId = 0;
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-11-25 20:52:06 +00:00
|
|
|
_ttlRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
_ttlDays = days;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> Authorizations::ttlDays() const {
|
|
|
|
return _ttlDays.value() | rpl::filter(rpl::mappers::_1 != 0);
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:01:58 +00:00
|
|
|
void Authorizations::toggleCallsDisabled(uint64 hash, bool disabled) {
|
|
|
|
if (const auto sent = _toggleCallsDisabledRequests.take(hash)) {
|
|
|
|
_api.request(*sent).cancel();
|
|
|
|
}
|
|
|
|
using Flag = MTPaccount_ChangeAuthorizationSettings::Flag;
|
|
|
|
const auto id = _api.request(MTPaccount_ChangeAuthorizationSettings(
|
|
|
|
MTP_flags(Flag::f_call_requests_disabled),
|
|
|
|
MTP_long(hash),
|
|
|
|
MTPBool(), // encrypted_requests_disabled
|
|
|
|
MTP_bool(disabled)
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done([=] {
|
2021-11-26 14:01:58 +00:00
|
|
|
_toggleCallsDisabledRequests.remove(hash);
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-11-26 14:01:58 +00:00
|
|
|
_toggleCallsDisabledRequests.remove(hash);
|
|
|
|
}).send();
|
|
|
|
_toggleCallsDisabledRequests.emplace(hash, id);
|
|
|
|
if (!hash) {
|
|
|
|
_callsDisabledHere = disabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Authorizations::callsDisabledHere() const {
|
|
|
|
return _callsDisabledHere.current();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> Authorizations::callsDisabledHereValue() const {
|
|
|
|
return _callsDisabledHere.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> Authorizations::callsDisabledHereChanges() const {
|
|
|
|
return _callsDisabledHere.changes();
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:01:12 +00:00
|
|
|
int Authorizations::total() const {
|
|
|
|
return ranges::count_if(
|
|
|
|
_list,
|
|
|
|
ranges::not_fn(&Entry::incomplete));
|
|
|
|
}
|
|
|
|
|
2020-09-18 19:04:18 +00:00
|
|
|
crl::time Authorizations::lastReceivedTime() {
|
|
|
|
return _lastReceived;
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:01:12 +00:00
|
|
|
} // namespace Api
|