mirror of
https://github.com/Qz3rK/tdesktop.git
synced 2026-07-03 14:15:08 +02:00
Remove Session::Exists() global access point.
This commit is contained in:
@@ -8,8 +8,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "mtproto/dedicated_file_loader.h"
|
||||
|
||||
#include "mtproto/facade.h"
|
||||
#include "main/main_session.h"
|
||||
#include "main/main_account.h" // Account::sessionChanges.
|
||||
#include "main/main_session.h" // Session::account.
|
||||
#include "core/application.h"
|
||||
#include "base/call_delayed.h"
|
||||
|
||||
@@ -82,17 +82,19 @@ std::optional<DedicatedLoader::File> ParseFile(
|
||||
|
||||
} // namespace
|
||||
|
||||
WeakInstance::WeakInstance(QPointer<MTP::Instance> instance)
|
||||
: _instance(instance) {
|
||||
WeakInstance::WeakInstance(base::weak_ptr<Main::Session> session)
|
||||
: _session(session)
|
||||
, _instance(_session ? _session->account().mtp() : nullptr) {
|
||||
if (!valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
connect(_instance, &QObject::destroyed, this, [=] {
|
||||
_instance = nullptr;
|
||||
_session = nullptr;
|
||||
die();
|
||||
});
|
||||
Core::App().activeAccount().sessionChanges(
|
||||
_session->account().sessionChanges(
|
||||
) | rpl::filter([](Main::Session *session) {
|
||||
return !session;
|
||||
}) | rpl::start_with_next([=] {
|
||||
@@ -100,19 +102,22 @@ WeakInstance::WeakInstance(QPointer<MTP::Instance> instance)
|
||||
}, _lifetime);
|
||||
}
|
||||
|
||||
bool WeakInstance::valid() const {
|
||||
return (_instance != nullptr) && Main::Session::Exists();
|
||||
base::weak_ptr<Main::Session> WeakInstance::session() const {
|
||||
return _session;
|
||||
}
|
||||
|
||||
QPointer<MTP::Instance> WeakInstance::instance() const {
|
||||
bool WeakInstance::valid() const {
|
||||
return (_session != nullptr);
|
||||
}
|
||||
|
||||
Instance *WeakInstance::instance() const {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void WeakInstance::die() {
|
||||
const auto instance = _instance.data();
|
||||
for (const auto &[requestId, fail] : base::take(_requests)) {
|
||||
if (instance) {
|
||||
instance->cancel(requestId);
|
||||
if (_instance) {
|
||||
_instance->cancel(requestId);
|
||||
}
|
||||
fail(RPCError::Local(
|
||||
"UNAVAILABLE",
|
||||
@@ -138,9 +143,9 @@ void WeakInstance::reportUnavailable(
|
||||
}
|
||||
|
||||
WeakInstance::~WeakInstance() {
|
||||
if (const auto instance = _instance.data()) {
|
||||
if (_instance) {
|
||||
for (const auto &[requestId, fail] : base::take(_requests)) {
|
||||
instance->cancel(requestId);
|
||||
_instance->cancel(requestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,14 +285,14 @@ rpl::lifetime &AbstractDedicatedLoader::lifetime() {
|
||||
}
|
||||
|
||||
DedicatedLoader::DedicatedLoader(
|
||||
QPointer<MTP::Instance> instance,
|
||||
base::weak_ptr<Main::Session> session,
|
||||
const QString &folder,
|
||||
const File &file)
|
||||
: AbstractDedicatedLoader(folder + '/' + file.name, kChunkSize)
|
||||
, _size(file.size)
|
||||
, _dcId(file.dcId)
|
||||
, _location(file.location)
|
||||
, _mtp(instance) {
|
||||
, _mtp(session) {
|
||||
Expects(_size > 0);
|
||||
}
|
||||
|
||||
@@ -365,7 +370,6 @@ Fn<void(const RPCError &)> DedicatedLoader::failHandler() {
|
||||
|
||||
void ResolveChannel(
|
||||
not_null<MTP::WeakInstance*> mtp,
|
||||
int32 userId,
|
||||
const QString &username,
|
||||
Fn<void(const MTPInputChannel &channel)> done,
|
||||
Fn<void()> fail) {
|
||||
@@ -374,20 +378,21 @@ void ResolveChannel(
|
||||
).arg(username));
|
||||
fail();
|
||||
};
|
||||
if (!userId) {
|
||||
const auto session = mtp->session();
|
||||
if (!mtp->valid()) {
|
||||
failed();
|
||||
return;
|
||||
}
|
||||
|
||||
struct ResolveResult {
|
||||
int32 userId = 0;
|
||||
base::weak_ptr<Main::Session> session;
|
||||
MTPInputChannel channel;
|
||||
};
|
||||
static std::map<QString, ResolveResult> ResolveCache;
|
||||
|
||||
const auto i = ResolveCache.find(username);
|
||||
if (i != end(ResolveCache)) {
|
||||
if (i->second.userId == userId) {
|
||||
if (i->second.session.get() == session.get()) {
|
||||
done(i->second.channel);
|
||||
return;
|
||||
}
|
||||
@@ -400,7 +405,7 @@ void ResolveChannel(
|
||||
if (const auto channel = ExtractChannel(result)) {
|
||||
ResolveCache.emplace(
|
||||
username,
|
||||
ResolveResult { userId, *channel });
|
||||
ResolveResult { session, *channel });
|
||||
done(*channel);
|
||||
} else {
|
||||
failed();
|
||||
@@ -430,7 +435,6 @@ std::optional<MTPMessage> GetMessagesElement(
|
||||
|
||||
void StartDedicatedLoader(
|
||||
not_null<MTP::WeakInstance*> mtp,
|
||||
int32 userId,
|
||||
const DedicatedLoader::Location &location,
|
||||
const QString &folder,
|
||||
Fn<void(std::unique_ptr<DedicatedLoader>)> ready) {
|
||||
@@ -438,7 +442,7 @@ void StartDedicatedLoader(
|
||||
const auto file = ParseFile(result);
|
||||
ready(file
|
||||
? std::make_unique<MTP::DedicatedLoader>(
|
||||
mtp->instance(),
|
||||
mtp->session(),
|
||||
folder,
|
||||
*file)
|
||||
: nullptr);
|
||||
@@ -450,7 +454,7 @@ void StartDedicatedLoader(
|
||||
};
|
||||
|
||||
const auto [username, postId] = location;
|
||||
ResolveChannel(mtp, userId, username, [=, postId = postId](
|
||||
ResolveChannel(mtp, username, [=, postId = postId](
|
||||
const MTPInputChannel &channel) {
|
||||
mtp->send(
|
||||
MTPchannels_GetMessages(
|
||||
|
||||
Reference in New Issue
Block a user