mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-16 14:17:12 +02:00
Changed behavior for sponsored messages to display them only when ready.
This commit is contained in:
parent
e29c6d2f23
commit
b753448052
4 changed files with 94 additions and 26 deletions
|
@ -12,7 +12,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
#include "core/click_handler_types.h"
|
||||
#include "data/data_channel.h"
|
||||
#include "data/data_document.h"
|
||||
#include "data/data_document_media.h"
|
||||
#include "data/data_file_origin.h"
|
||||
#include "data/data_photo.h"
|
||||
#include "data/data_photo_media.h"
|
||||
#include "data/data_session.h"
|
||||
#include "data/data_user.h"
|
||||
#include "history/history.h"
|
||||
|
@ -69,16 +72,17 @@ void SponsoredMessages::clearOldRequests() {
|
|||
}
|
||||
}
|
||||
|
||||
bool SponsoredMessages::append(not_null<History*> history) {
|
||||
SponsoredMessages::AppendResult SponsoredMessages::append(
|
||||
not_null<History*> history) {
|
||||
const auto it = _data.find(history);
|
||||
if (it == end(_data)) {
|
||||
return false;
|
||||
return SponsoredMessages::AppendResult::None;
|
||||
}
|
||||
auto &list = it->second;
|
||||
if (list.showedAll
|
||||
|| !TooEarlyForRequest(list.received)
|
||||
|| list.postsBetween) {
|
||||
return false;
|
||||
return SponsoredMessages::AppendResult::None;
|
||||
}
|
||||
|
||||
const auto entryIt = ranges::find_if(list.entries, [](const Entry &e) {
|
||||
|
@ -86,19 +90,30 @@ bool SponsoredMessages::append(not_null<History*> history) {
|
|||
});
|
||||
if (entryIt == end(list.entries)) {
|
||||
list.showedAll = true;
|
||||
return false;
|
||||
return SponsoredMessages::AppendResult::None;
|
||||
}
|
||||
if (const auto media = entryIt->documentMedia) {
|
||||
const auto fullDuration = media->owner()->duration();
|
||||
if (fullDuration <= 0) {
|
||||
if (!media->loaded()) {
|
||||
return SponsoredMessages::AppendResult::MediaLoading;
|
||||
}
|
||||
} else {
|
||||
constexpr auto kEnoughDuration = float64(2000);
|
||||
if ((kEnoughDuration / fullDuration) > media->progress()) {
|
||||
return SponsoredMessages::AppendResult::MediaLoading;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (entryIt->photoMedia && !entryIt->photoMedia->loaded()) {
|
||||
return SponsoredMessages::AppendResult::MediaLoading;
|
||||
}
|
||||
// SponsoredMessages::Details can be requested within
|
||||
// the constructor of HistoryItem, so itemFullId is used as a key.
|
||||
entryIt->itemFullId = FullMsgId(
|
||||
history->peer->id,
|
||||
_session->data().nextLocalMessageId());
|
||||
entryIt->item.reset(history->addSponsoredMessage(
|
||||
entryIt->itemFullId.msg,
|
||||
entryIt->sponsored.from,
|
||||
entryIt->sponsored.textWithEntities));
|
||||
|
||||
return true;
|
||||
return SponsoredMessages::AppendResult::Appended;
|
||||
}
|
||||
|
||||
void SponsoredMessages::inject(
|
||||
|
@ -221,8 +236,7 @@ void SponsoredMessages::request(not_null<History*> history, Fn<void()> done) {
|
|||
const auto channel = history->peer->asChannel();
|
||||
Assert(channel != nullptr);
|
||||
request.requestId = _session->api().request(
|
||||
MTPchannels_GetSponsoredMessages(
|
||||
channel->inputChannel)
|
||||
MTPchannels_GetSponsoredMessages(channel->inputChannel)
|
||||
).done([=](const MTPmessages_sponsoredMessages &result) {
|
||||
parse(history, result);
|
||||
if (done) {
|
||||
|
@ -270,15 +284,15 @@ void SponsoredMessages::append(
|
|||
const MTPSponsoredMessage &message) {
|
||||
const auto &data = message.data();
|
||||
const auto randomId = data.vrandom_id().v;
|
||||
auto mediaPhotoId = PhotoId(0);
|
||||
auto mediaDocumentId = DocumentId(0);
|
||||
auto mediaPhoto = std::shared_ptr<Data::PhotoMedia>(nullptr);
|
||||
auto mediaDocument = std::shared_ptr<Data::DocumentMedia>(nullptr);
|
||||
{
|
||||
if (data.vmedia()) {
|
||||
data.vmedia()->match([&](const MTPDmessageMediaPhoto &media) {
|
||||
if (const auto tlPhoto = media.vphoto()) {
|
||||
tlPhoto->match([&](const MTPDphoto &data) {
|
||||
const auto p = history->owner().processPhoto(data);
|
||||
mediaPhotoId = p->id;
|
||||
mediaPhoto = p->createMediaView();
|
||||
}, [](const MTPDphotoEmpty &) {
|
||||
});
|
||||
}
|
||||
|
@ -290,7 +304,7 @@ void SponsoredMessages::append(
|
|||
|| d->isSilentVideo()
|
||||
|| d->isAnimation()
|
||||
|| d->isGifv()) {
|
||||
mediaDocumentId = d->id;
|
||||
mediaDocument = d->createMediaView();
|
||||
}
|
||||
}, [](const MTPDdocumentEmpty &) {
|
||||
});
|
||||
|
@ -306,8 +320,10 @@ void SponsoredMessages::append(
|
|||
.photoId = data.vphoto()
|
||||
? history->session().data().processPhoto(*data.vphoto())->id
|
||||
: PhotoId(0),
|
||||
.mediaPhotoId = mediaPhotoId,
|
||||
.mediaDocumentId = mediaDocumentId,
|
||||
.mediaPhotoId = (mediaPhoto ? mediaPhoto->owner()->id : PhotoId(0)),
|
||||
.mediaDocumentId = (mediaDocument
|
||||
? mediaDocument->owner()->id
|
||||
: DocumentId(0)),
|
||||
.backgroundEmojiId = data.vcolor().has_value()
|
||||
? data.vcolor()->data().vbackground_emoji_id().value_or_empty()
|
||||
: uint64(0),
|
||||
|
@ -341,7 +357,31 @@ void SponsoredMessages::append(
|
|||
.sponsorInfo = std::move(sponsorInfo),
|
||||
.additionalInfo = std::move(additionalInfo),
|
||||
};
|
||||
list.entries.push_back({ nullptr, {}, std::move(sharedMessage) });
|
||||
list.entries.push_back({
|
||||
nullptr,
|
||||
{},
|
||||
std::move(sharedMessage),
|
||||
mediaPhoto,
|
||||
mediaDocument,
|
||||
});
|
||||
|
||||
const auto fileOrigin = FullMsgId(
|
||||
history->peer->id,
|
||||
_session->data().nextLocalMessageId());
|
||||
list.entries.back().itemFullId = fileOrigin;
|
||||
if (mediaPhoto) {
|
||||
mediaPhoto->owner()->load(
|
||||
list.entries.back().itemFullId,
|
||||
LoadFromCloudOrLocal,
|
||||
true);
|
||||
}
|
||||
if (mediaDocument) {
|
||||
mediaDocument->owner()->save(
|
||||
fileOrigin,
|
||||
QString(),
|
||||
LoadFromCloudOrLocal,
|
||||
true);
|
||||
}
|
||||
}
|
||||
|
||||
void SponsoredMessages::clearItems(not_null<History*> history) {
|
||||
|
|
|
@ -20,6 +20,9 @@ class Session;
|
|||
|
||||
namespace Data {
|
||||
|
||||
class DocumentMedia;
|
||||
class PhotoMedia;
|
||||
|
||||
struct SponsoredReportResult final {
|
||||
using Id = QByteArray;
|
||||
struct Option final {
|
||||
|
@ -65,6 +68,11 @@ struct SponsoredMessage {
|
|||
|
||||
class SponsoredMessages final {
|
||||
public:
|
||||
enum class AppendResult {
|
||||
None,
|
||||
Appended,
|
||||
MediaLoading,
|
||||
};
|
||||
enum class State {
|
||||
None,
|
||||
AppendToEnd,
|
||||
|
@ -92,7 +100,7 @@ public:
|
|||
[[nodiscard]] Details lookupDetails(const FullMsgId &fullId) const;
|
||||
void clicked(const FullMsgId &fullId, bool isMedia, bool isFullscreen);
|
||||
|
||||
[[nodiscard]] bool append(not_null<History*> history);
|
||||
[[nodiscard]] AppendResult append(not_null<History*> history);
|
||||
void inject(
|
||||
not_null<History*> history,
|
||||
MsgId injectAfterMsgId,
|
||||
|
@ -114,6 +122,8 @@ private:
|
|||
OwnedItem item;
|
||||
FullMsgId itemFullId;
|
||||
SponsoredMessage sponsored;
|
||||
std::shared_ptr<Data::PhotoMedia> photoMedia;
|
||||
std::shared_ptr<Data::DocumentMedia> documentMedia;
|
||||
};
|
||||
struct List {
|
||||
std::vector<Entry> entries;
|
||||
|
|
|
@ -321,10 +321,28 @@ HistoryWidget::HistoryWidget(
|
|||
}), lifetime());
|
||||
_scroll->addContentRequests(
|
||||
) | rpl::start_with_next([=] {
|
||||
if (_history
|
||||
&& _history->loadedAtBottom()
|
||||
&& session().sponsoredMessages().append(_history)) {
|
||||
_scroll->contentAdded();
|
||||
if (_history && _history->loadedAtBottom()) {
|
||||
using Result = Data::SponsoredMessages::AppendResult;
|
||||
const auto tryToAppend = [=] {
|
||||
const auto r = session().sponsoredMessages().append(_history);
|
||||
if (r == Result::Appended) {
|
||||
_scroll->contentAdded();
|
||||
}
|
||||
return r;
|
||||
};
|
||||
if (tryToAppend() == Result::MediaLoading) {
|
||||
const auto sharedLifetime = std::make_shared<rpl::lifetime>();
|
||||
session().downloaderTaskFinished(
|
||||
) | rpl::start_with_next([=, weak = Ui::MakeWeak(this)] {
|
||||
if (const auto strong = weak.data()) {
|
||||
if (tryToAppend() != Result::MediaLoading) {
|
||||
sharedLifetime->destroy();
|
||||
}
|
||||
} else {
|
||||
sharedLifetime->destroy();
|
||||
}
|
||||
}, *sharedLifetime);
|
||||
}
|
||||
}
|
||||
}, lifetime());
|
||||
|
||||
|
|
|
@ -847,8 +847,8 @@ void DraftOptionsBox(
|
|||
if (state->quote.current().overflown) {
|
||||
show->showToast({
|
||||
.title = tr::lng_reply_quote_long_title(tr::now),
|
||||
.text = tr::lng_reply_quote_long_text(tr::now),
|
||||
});
|
||||
.text = { tr::lng_reply_quote_long_text(tr::now) },
|
||||
});
|
||||
} else {
|
||||
finish(resolveReply(), state->webpage);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue