Fix pending link previews in preview options box.

This commit is contained in:
John Preston 2023-10-29 10:57:07 +04:00
parent e2ea27cbef
commit 478c6c4d36
4 changed files with 45 additions and 21 deletions

View File

@ -1166,6 +1166,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_mute_box_title" = "Mute notifications for...";
"lng_preview_loading" = "Getting Link Info...";
"lng_preview_cant" = "Could not generate preview for this link.";
"lng_profile_settings_section" = "Settings";
"lng_profile_bot_settings" = "Bot Settings";

View File

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "history/view/controls/history_view_draft_options.h"
#include "base/timer_rpl.h"
#include "base/unixtime.h"
#include "boxes/peer_list_box.h"
#include "boxes/peer_list_controllers.h"
@ -589,6 +590,9 @@ void DraftOptionsBox(
QString link;
Ui::SettingsSlider *tabs = nullptr;
PreviewWrap *wrap = nullptr;
Fn<void(const QString &link, WebPageData *page)> performSwitch;
Fn<void(const QString &link, bool force)> requestAndSwitch;
rpl::lifetime resolveLifetime;
};
const auto state = box->lifetime().make_state<State>();
@ -740,35 +744,54 @@ void DraftOptionsBox(
};
const auto &resolver = args.resolver;
const auto performSwitch = [=](const QString &link, WebPageData *page) {
if (page) {
state->performSwitch = [=](const QString &link, WebPageData *page) {
const auto now = base::unixtime::now();
if (!page || page->pendingTill > 0 && page->pendingTill < now) {
show->showToast(tr::lng_preview_cant(tr::now));
} else if (page->pendingTill > 0) {
const auto delay = std::max(page->pendingTill - now, TimeId());
base::timer_once(
(delay + 1) * crl::time(1000)
) | rpl::start_with_next([=] {
state->requestAndSwitch(link, true);
}, state->resolveLifetime);
page->owner().webPageUpdates(
) | rpl::start_with_next([=](not_null<WebPageData*> updated) {
if (updated == page && !updated->pendingTill) {
state->resolveLifetime.destroy();
state->performSwitch(link, page);
}
}, state->resolveLifetime);
} else {
state->preview = page;
state->webpage.id = page->id;
state->webpage.url = page->url;
state->webpage.manual = true;
state->link = link;
state->shown.force_assign(Section::Link);
} else {
show->showToast(u"Could not generate preview for this link."_q);
}
};
state->requestAndSwitch = [=](const QString &link, bool force) {
resolver->request(link, force);
state->resolveLifetime = resolver->resolved(
) | rpl::start_with_next([=](const QString &resolved) {
if (resolved == link) {
state->resolveLifetime.destroy();
state->performSwitch(
link,
resolver->lookup(link).value_or(nullptr));
}
});
};
const auto switchTo = [=](const QString &link) {
if (link == state->link) {
return;
}
if (const auto value = resolver->lookup(link)) {
performSwitch(link, *value);
} else if (const auto value = resolver->lookup(link)) {
state->performSwitch(link, *value);
} else {
resolver->request(link);
state->resolveLifetime = resolver->resolved(
) | rpl::start_with_next([=](const QString &resolved) {
if (resolved == link) {
state->resolveLifetime.destroy();
performSwitch(
link,
resolver->lookup(link).value_or(nullptr));
}
});
state->requestAndSwitch(link, false);
}
};

View File

@ -134,8 +134,8 @@ QString WebpageResolver::find(not_null<WebPageData*> page) const {
return QString();
}
void WebpageResolver::request(const QString &link) {
if (_requestLink == link) {
void WebpageResolver::request(const QString &link, bool force) {
if (_requestLink == link && !force) {
return;
}
const auto done = [=](const MTPDmessageMediaWebPage &data) {
@ -191,7 +191,7 @@ WebpageProcessor::WebpageProcessor(
if (!ShowWebPagePreview(_data) || _link.isEmpty()) {
return;
}
_resolver->request(_link);
_resolver->request(_link, true);
}) {
_history->session().downloaderTaskFinished(
) | rpl::filter([=] {

View File

@ -62,7 +62,7 @@ public:
[[nodiscard]] QString find(not_null<WebPageData*> page) const;
void request(const QString &link);
void request(const QString &link, bool force = false);
void cancel(const QString &link);
private: