Skip to content

Commit

Permalink
Migrate IV to TDLib.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Oct 16, 2024
1 parent 502c6d1 commit 39b7366
Show file tree
Hide file tree
Showing 7 changed files with 894 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Telegram/SourceFiles/iv/iv_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ For license and copyright information please follow this link:
#include "iv/iv_prepare.h"
#include "webview/webview_interface.h"

#include "tdb/tdb_tl_scheme.h"
#include <xxhash.h>

#include <QtCore/QRegularExpression>
#include <QtCore/QUrl>

Expand All @@ -18,6 +21,10 @@ namespace {

bool FailureRecorded/* = false*/;

[[nodiscard]] uint64 IdFromTdb(const QString &url) {
return XXH64(url.data(), url.size() * sizeof(ushort), 0);
}

} // namespace

QByteArray GeoPointId(Geo point) {
Expand Down Expand Up @@ -45,6 +52,7 @@ Geo GeoPointFromId(QByteArray data) {
};
}

#if 0 // mtp
Data::Data(const MTPDwebPage &webpage, const MTPPage &page)
: _source(std::make_unique<Source>(Source{
.pageId = webpage.vid().v,
Expand All @@ -59,14 +67,32 @@ Data::Data(const MTPDwebPage &webpage, const MTPPage &page)
? qs(*webpage.vsite_name())
: SiteNameFromUrl(qs(webpage.vurl())))
})) {
}
#endif

Data::Data(
const QString &url,
const Tdb::TLwebPageInstantView &data)
: _source(std::make_unique<Source>(Source{
.pageId = IdFromTdb(url),
.page = data,
.url = url,
})) {

}

QString Data::id() const {
#if 0 // mtp
return qs(_source->page.data().vurl());
#endif
return _source->url;
}

bool Data::partial() const {
#if 0 // mtp
return _source->page.data().is_part();
#endif
return !_source->page.data().vis_full().v;
}

Data::~Data() = default;
Expand Down
10 changes: 10 additions & 0 deletions Telegram/SourceFiles/iv/iv_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ For license and copyright information please follow this link:
*/
#pragma once

namespace Tdb {
class TLlinkPreview;
class TLwebPageInstantView;
} // namespace Tdb

namespace Iv {

struct Source;
Expand Down Expand Up @@ -39,7 +44,12 @@ struct Geo {

class Data final {
public:
#if 0 // mtp
Data(const MTPDwebPage &webpage, const MTPPage &page);
#endif
Data(
const QString &url,
const Tdb::TLwebPageInstantView &data);
~Data();

[[nodiscard]] QString id() const;
Expand Down
58 changes: 58 additions & 0 deletions Telegram/SourceFiles/iv/iv_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ For license and copyright information please follow this link:
#include "window/window_session_controller.h"
#include "window/window_session_controller_link_info.h"

#include "tdb/tdb_tl_scheme.h"
#include "tdb/tdb_sender.h"

#include <QtGui/QGuiApplication>
#include <QtGui/QWindow>

namespace Iv {
namespace {

using namespace Tdb;

constexpr auto kGeoPointScale = 1;
constexpr auto kGeoPointZoomMin = 13;
constexpr auto kMaxLoadParts = 5;
Expand Down Expand Up @@ -206,12 +211,19 @@ void Shown::fillChannelJoinedValues(const Prepared &result) {
const auto channelId = ChannelId(id.toLongLong());
const auto channel = _session->data().channel(channelId);
if (!channel->isLoaded() && !channel->username().isEmpty()) {
#if 0 // mtp
channel->session().api().request(MTPcontacts_ResolveUsername(
MTP_string(channel->username())
)).done([=](const MTPcontacts_ResolvedPeer &result) {
channel->owner().processUsers(result.data().vusers());
channel->owner().processChats(result.data().vchats());
}).send();
#endif
channel->session().sender().request(TLsearchPublicChat(
tl_string(channel->username())
)).done([=](const TLchat &result) {
channel->owner().processPeer(result);
}).send();
}
_inChannelValues[id] = Info::Profile::AmInChannelValue(channel);
}
Expand Down Expand Up @@ -847,10 +859,16 @@ void Instance::show(
const auto url = event.url;
auto &requested = _fullRequested[session][url];
requested.lastRequestedAt = crl::now();
#if 0 // mtp
session->api().request(MTPmessages_GetWebPage(
MTP_string(url),
MTP_int(requested.hash)
)).done([=](const MTPmessages_WebPage &result) {
#endif
session->sender().request(TLgetWebPageInstantView(
tl_string(url),
tl_bool(true)
)).done([=](const TLwebPageInstantView &result) {
const auto page = processReceivedPage(session, url, result);
if (page && page->iv) {
const auto parts = event.url.split('#');
Expand Down Expand Up @@ -904,7 +922,10 @@ void Instance::trackSession(not_null<Main::Session*> session) {
_fullRequested.remove(session);
_ivCache.remove(session);
if (_ivRequestSession == session) {
#if 0 // mtp
session->api().request(_ivRequestId).cancel();
#endif
session->sender().request(_ivRequestId).cancel();
_ivRequestSession = nullptr;
_ivRequestUri = QString();
_ivRequestId = 0;
Expand Down Expand Up @@ -950,7 +971,10 @@ void Instance::openWithIvPreferred(
const auto url = parts[0];
auto &cache = _ivCache[session];
if (const auto i = cache.find(url); i != end(cache)) {
#if 0 // mtp
const auto page = i->second;
#endif
const auto &page = i->second;
if (page && page->iv) {
auto my = context.value<ClickHandlerContext>();
if (const auto window = my.sessionWindow.get()) {
Expand All @@ -965,25 +989,39 @@ void Instance::openWithIvPreferred(
} else if (_ivRequestSession == session.get() && _ivRequestUri == uri) {
return;
} else if (_ivRequestId) {
#if 0 // mtp
_ivRequestSession->api().request(_ivRequestId).cancel();
#endif
_ivRequestSession->sender().request(_ivRequestId).cancel();
}
#if 0 // mtp
const auto finish = [=](WebPageData *page) {
#endif
const auto finish = [=](PageIv *page) {
Expects(_ivRequestSession == session);

_ivRequestId = 0;
_ivRequestUri = QString();
_ivRequestSession = nullptr;
#if 0 // mtp
_ivCache[session][url] = page;
#endif
openWithIvPreferred(session, uri, context);
};
_ivRequestSession = session;
_ivRequestUri = uri;
auto &requested = _fullRequested[session][url];
requested.lastRequestedAt = crl::now();
#if 0 // mtp
_ivRequestId = session->api().request(MTPmessages_GetWebPage(
MTP_string(url),
MTP_int(requested.hash)
)).done([=](const MTPmessages_WebPage &result) {
#endif
_ivRequestId = session->sender().request(TLgetWebPageInstantView(
tl_string(url),
tl_bool(true)
)).done([=](const TLwebPageInstantView &result) {
finish(processReceivedPage(session, url, result));
}).fail([=] {
finish(nullptr);
Expand All @@ -1003,20 +1041,39 @@ void Instance::requestFull(
return;
}
requested.lastRequestedAt = now;
#if 0 // mtp
session->api().request(MTPmessages_GetWebPage(
MTP_string(id),
MTP_int(requested.hash)
)).done([=](const MTPmessages_WebPage &result) {
#endif
session->sender().request(TLgetWebPageInstantView(
tl_string(id),
tl_bool(true)
)).done([=](const TLwebPageInstantView &result) {
const auto page = processReceivedPage(session, id, result);
if (page && page->iv && _shown && _shownSession == session) {
_shown->update(page->iv.get());
}
}).send();
}

#if 0 // mtp
WebPageData *Instance::processReceivedPage(
#endif
Instance::PageIv *Instance::processReceivedPage(
not_null<Main::Session*> session,
const QString &url,
const TLwebPageInstantView &result) {
auto owned = std::make_unique<PageIv>(PageIv{
.iv = std::make_unique<Data>(url, result),
});
const auto raw = owned.get();
_ivCache[session][url] = std::move(owned);
auto &requested = _fullRequested[session][url];
requested.page = raw;
return raw;
#if 0 // mtp
const MTPmessages_WebPage &result) {
const auto &data = result.data();
const auto owner = &session->data();
Expand All @@ -1038,6 +1095,7 @@ WebPageData *Instance::processReceivedPage(
requested.page = owner->processWebpage(mtp).get();
});
return requested.page;
#endif
}

void Instance::processOpenChannel(const QString &context) {
Expand Down
21 changes: 21 additions & 0 deletions Telegram/SourceFiles/iv/iv_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ For license and copyright information please follow this link:

#include "iv/iv_delegate.h"

namespace Tdb {
class TLwebPageInstantView;
} // namespace Tdb

namespace Main {
class Session;
class SessionShow;
Expand Down Expand Up @@ -61,9 +65,15 @@ class Instance final {
[[nodiscard]] rpl::lifetime &lifetime();

private:
struct PageIv {
std::unique_ptr<Data> iv;
};
struct FullResult {
crl::time lastRequestedAt = 0;
#if 0 // mtp
WebPageData *page = nullptr;
#endif
PageIv *page = nullptr;
int32 hash = 0;
};

Expand All @@ -73,10 +83,16 @@ class Instance final {

void trackSession(not_null<Main::Session*> session);

#if 0 // mtp
WebPageData *processReceivedPage(
not_null<Main::Session*> session,
const QString &url,
const MTPmessages_WebPage &result);
#endif
PageIv *processReceivedPage(
not_null<Main::Session*> session,
const QString &url,
const Tdb::TLwebPageInstantView &result);

const not_null<Delegate*> _delegate;

Expand All @@ -90,9 +106,14 @@ class Instance final {
not_null<Main::Session*>,
base::flat_map<QString, FullResult>> _fullRequested;

#if 0 // mtp
base::flat_map<
not_null<Main::Session*>,
base::flat_map<QString, WebPageData*>> _ivCache;
#endif
base::flat_map<
not_null<Main::Session*>,
base::flat_map<QString, std::unique_ptr<PageIv>>> _ivCache;
Main::Session *_ivRequestSession = nullptr;
QString _ivRequestUri;
mtpRequestId _ivRequestId = 0;
Expand Down
Loading

0 comments on commit 39b7366

Please sign in to comment.