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_mute_box_title" = "Mute notifications for...";
"lng_preview_loading" = "Getting Link Info..."; "lng_preview_loading" = "Getting Link Info...";
"lng_preview_cant" = "Could not generate preview for this link.";
"lng_profile_settings_section" = "Settings"; "lng_profile_settings_section" = "Settings";
"lng_profile_bot_settings" = "Bot 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 "history/view/controls/history_view_draft_options.h"
#include "base/timer_rpl.h"
#include "base/unixtime.h" #include "base/unixtime.h"
#include "boxes/peer_list_box.h" #include "boxes/peer_list_box.h"
#include "boxes/peer_list_controllers.h" #include "boxes/peer_list_controllers.h"
@ -589,6 +590,9 @@ void DraftOptionsBox(
QString link; QString link;
Ui::SettingsSlider *tabs = nullptr; Ui::SettingsSlider *tabs = nullptr;
PreviewWrap *wrap = nullptr; PreviewWrap *wrap = nullptr;
Fn<void(const QString &link, WebPageData *page)> performSwitch;
Fn<void(const QString &link, bool force)> requestAndSwitch;
rpl::lifetime resolveLifetime; rpl::lifetime resolveLifetime;
}; };
const auto state = box->lifetime().make_state<State>(); const auto state = box->lifetime().make_state<State>();
@ -740,35 +744,54 @@ void DraftOptionsBox(
}; };
const auto &resolver = args.resolver; const auto &resolver = args.resolver;
const auto performSwitch = [=](const QString &link, WebPageData *page) { state->performSwitch = [=](const QString &link, WebPageData *page) {
if (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->preview = page;
state->webpage.id = page->id; state->webpage.id = page->id;
state->webpage.url = page->url; state->webpage.url = page->url;
state->webpage.manual = true; state->webpage.manual = true;
state->link = link; state->link = link;
state->shown.force_assign(Section::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) { const auto switchTo = [=](const QString &link) {
if (link == state->link) { if (link == state->link) {
return; return;
} } else if (const auto value = resolver->lookup(link)) {
if (const auto value = resolver->lookup(link)) { state->performSwitch(link, *value);
performSwitch(link, *value);
} else { } else {
resolver->request(link); state->requestAndSwitch(link, false);
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));
}
});
} }
}; };

View File

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

View File

@ -62,7 +62,7 @@ public:
[[nodiscard]] QString find(not_null<WebPageData*> page) const; [[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); void cancel(const QString &link);
private: private: