Skip to content

Commit

Permalink
Changed behavior for sponsored messages to display them only when ready.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Sep 30, 2024
1 parent 7fdc31b commit b9d2b5c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 26 deletions.
78 changes: 59 additions & 19 deletions Telegram/SourceFiles/data/components/sponsored_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ For license and copyright information please follow this link:
#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"
Expand Down Expand Up @@ -69,36 +72,48 @@ 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) {
return e.item == nullptr;
});
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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 &) {
});
}
Expand All @@ -290,7 +304,7 @@ void SponsoredMessages::append(
|| d->isSilentVideo()
|| d->isAnimation()
|| d->isGifv()) {
mediaDocumentId = d->id;
mediaDocument = d->createMediaView();
}
}, [](const MTPDdocumentEmpty &) {
});
Expand All @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion Telegram/SourceFiles/data/components/sponsored_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Session;

namespace Data {

class DocumentMedia;
class PhotoMedia;

struct SponsoredReportResult final {
using Id = QByteArray;
struct Option final {
Expand Down Expand Up @@ -65,6 +68,11 @@ struct SponsoredMessage {

class SponsoredMessages final {
public:
enum class AppendResult {
None,
Appended,
MediaLoading,
};
enum class State {
None,
AppendToEnd,
Expand Down Expand Up @@ -92,7 +100,7 @@ class SponsoredMessages final {
[[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,
Expand All @@ -114,6 +122,8 @@ class SponsoredMessages final {
OwnedItem item;
FullMsgId itemFullId;
SponsoredMessage sponsored;
std::shared_ptr<Data::PhotoMedia> photoMedia;
std::shared_ptr<Data::DocumentMedia> documentMedia;
};
struct List {
std::vector<Entry> entries;
Expand Down
26 changes: 22 additions & 4 deletions Telegram/SourceFiles/history/history_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b9d2b5c

Please sign in to comment.