Added support of service actions for premium gifts.

This commit is contained in:
23rd 2022-07-13 20:12:40 +03:00 committed by John Preston
parent ef72c9f7ee
commit e84f5aaa3d
14 changed files with 637 additions and 5 deletions

View File

@ -368,6 +368,8 @@ PRIVATE
chat_helpers/stickers_emoji_image_loader.h
chat_helpers/stickers_emoji_pack.cpp
chat_helpers/stickers_emoji_pack.h
chat_helpers/stickers_gift_box_pack.cpp
chat_helpers/stickers_gift_box_pack.h
chat_helpers/stickers_dice_pack.cpp
chat_helpers/stickers_dice_pack.h
chat_helpers/stickers_list_footer.cpp
@ -644,6 +646,8 @@ PRIVATE
history/view/media/history_view_photo.cpp
history/view/media/history_view_poll.h
history/view/media/history_view_poll.cpp
history/view/media/history_view_service_media_gift.h
history/view/media/history_view_service_media_gift.cpp
history/view/media/history_view_slot_machine.h
history/view/media/history_view_slot_machine.cpp
history/view/media/history_view_sticker.h

View File

@ -1456,6 +1456,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_action_proximity_distance_km#one" = "{count} km";
"lng_action_proximity_distance_km#other" = "{count} km";
"lng_action_webview_data_done" = "You have just successfully transferred data from the «{text}» button to the bot.";
"lng_action_gift_received" = "{user} sent you a gift for {cost}";
"lng_premium_gift_duration_months#one" = "for {count} month";
"lng_premium_gift_duration_months#other" = "for {count} months";
"lng_premium_gift_duration_years#one" = "for {count} year";
"lng_premium_gift_duration_years#other" = "for {count} years";
"lng_ttl_photo_received" = "{from} sent you a self-destructing photo. Please view it on your mobile.";
"lng_ttl_photo_sent" = "You sent a self-destructing photo.";

View File

@ -65,8 +65,7 @@ void DicePack::applySet(const MTPDmessages_stickerSet &data) {
auto index = 0;
auto documents = base::flat_map<DocumentId, not_null<DocumentData*>>();
for (const auto &sticker : data.vdocuments().v) {
const auto document = _session->data().processDocument(
sticker);
const auto document = _session->data().processDocument(sticker);
if (document->sticker()) {
if (isSlotMachine) {
_map.emplace(index++, document);

View File

@ -0,0 +1,86 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "chat_helpers/stickers_gift_box_pack.h"
#include "apiwrap.h"
#include "data/data_document.h"
#include "data/data_session.h"
#include "main/main_session.h"
namespace Stickers {
GiftBoxPack::GiftBoxPack(not_null<Main::Session*> session)
: _session(session)
, _localMonths({ 3, 6, 12 }) {
}
GiftBoxPack::~GiftBoxPack() = default;
DocumentData *GiftBoxPack::lookup(int months) const {
const auto it = ranges::lower_bound(_localMonths, months);
if (it == begin(_localMonths)) {
return _documents.empty() ? nullptr : _documents[0];
}
const auto left = *(it - 1);
const auto right = *it;
const auto shift = (std::abs(months - left) < std::abs(months - right))
? -1
: 0;
const auto index = int(std::distance(begin(_localMonths), it - shift));
return (index >= _documents.size()) ? nullptr : _documents[index];
}
void GiftBoxPack::load() {
if (_requestId || !_documents.empty()) {
return;
}
_requestId = _session->api().request(MTPmessages_GetStickerSet(
MTP_inputStickerSetPremiumGifts(),
MTP_int(0) // Hash.
)).done([=](const MTPmessages_StickerSet &result) {
result.match([&](const MTPDmessages_stickerSet &data) {
applySet(data);
}, [](const MTPDmessages_stickerSetNotModified &) {
LOG(("API Error: Unexpected messages.stickerSetNotModified."));
});
}).fail([=] {
_requestId = 0;
}).send();
}
void GiftBoxPack::applySet(const MTPDmessages_stickerSet &data) {
_setId = data.vset().data().vid().v;
auto documents = base::flat_map<DocumentId, not_null<DocumentData*>>();
for (const auto &sticker : data.vdocuments().v) {
const auto document = _session->data().processDocument(sticker);
if (document->sticker()) {
documents.emplace(document->id, document);
}
}
for (const auto &pack : data.vpacks().v) {
pack.match([&](const MTPDstickerPack &data) {
const auto emoji = qs(data.vemoticon());
if (emoji.isEmpty()) {
return;
}
for (const auto &id : data.vdocuments().v) {
if (const auto document = documents.take(id.v)) {
_documents.push_back(*document);
}
}
});
}
}
bool GiftBoxPack::isGiftSticker(not_null<DocumentData*> document) const {
return document->sticker()
? (document->sticker()->set.id == _setId)
: false;
}
} // namespace Stickers

View File

@ -0,0 +1,41 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
class DocumentData;
namespace Main {
class Session;
} // namespace Main
namespace Stickers {
class GiftBoxPack final {
public:
explicit GiftBoxPack(not_null<Main::Session*> session);
~GiftBoxPack();
void load();
[[nodiscard]] DocumentData *lookup(int months) const;
[[nodiscard]] bool isGiftSticker(not_null<DocumentData*> document) const;
private:
using SetId = uint64;
void applySet(const MTPDmessages_stickerSet &data);
const not_null<Main::Session*> _session;
const std::vector<int> _localMonths;
std::vector<DocumentData*> _documents;
SetId _setId = 0;
mtpRequestId _requestId = 0;
};
} // namespace Stickers

View File

@ -26,6 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/view/media/history_view_theme_document.h"
#include "history/view/media/history_view_slot_machine.h"
#include "history/view/media/history_view_dice.h"
#include "history/view/media/history_view_service_media_gift.h"
#include "dialogs/ui/dialogs_message_view.h"
#include "ui/image/image.h"
#include "ui/text/format_song_document_name.h"
@ -1718,4 +1719,60 @@ ClickHandlerPtr MediaDice::MakeHandler(
});
}
MediaGiftBox::MediaGiftBox(not_null<HistoryItem*> parent, int months)
: Media(parent)
, _months(months) {
}
std::unique_ptr<Media> MediaGiftBox::clone(not_null<HistoryItem*> parent) {
return std::make_unique<MediaGiftBox>(parent, _months);
}
int MediaGiftBox::months() const {
return _months;
}
bool MediaGiftBox::allowsRevoke(TimeId now) const {
return false;
}
TextWithEntities MediaGiftBox::notificationText() const {
return {};
}
QString MediaGiftBox::pinnedTextSubstring() const {
return {};
}
TextForMimeData MediaGiftBox::clipboardText() const {
return {};
}
bool MediaGiftBox::forceForwardedInfo() const {
return false;
}
bool MediaGiftBox::updateInlineResultMedia(const MTPMessageMedia &media) {
return false;
}
bool MediaGiftBox::updateSentMedia(const MTPMessageMedia &media) {
return false;
}
std::unique_ptr<HistoryView::Media> MediaGiftBox::createView(
not_null<HistoryView::Element*> message,
not_null<HistoryItem*> realParent,
HistoryView::Element *replacing) {
return std::make_unique<HistoryView::MediaGift>(message, this);
}
bool MediaGiftBox::activated() const {
return _activated;
}
void MediaGiftBox::setActivated(bool activated) {
_activated = activated;
}
} // namespace Data

View File

@ -475,6 +475,36 @@ private:
};
class MediaGiftBox final : public Media {
public:
MediaGiftBox(not_null<HistoryItem*> parent, int months);
std::unique_ptr<Media> clone(not_null<HistoryItem*> parent) override;
[[nodiscard]] int months() const;
[[nodiscard]] bool activated() const;
void setActivated(bool activated);
bool allowsRevoke(TimeId now) const override;
TextWithEntities notificationText() const override;
QString pinnedTextSubstring() const override;
TextForMimeData clipboardText() const override;
bool forceForwardedInfo() const override;
bool updateInlineResultMedia(const MTPMessageMedia &media) override;
bool updateSentMedia(const MTPMessageMedia &media) override;
std::unique_ptr<HistoryView::Media> createView(
not_null<HistoryView::Element*> message,
not_null<HistoryItem*> realParent,
HistoryView::Element *replacing = nullptr) override;
private:
int _months = 0;
bool _activated = false;
};
[[nodiscard]] TextForMimeData WithCaptionClipboardText(
const QString &attachType,
TextForMimeData &&caption);

View File

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "history/history_service.h"
#include "chat_helpers/stickers_gift_box_pack.h"
#include "lang/lang_keys.h"
#include "mainwidget.h"
#include "main/main_session.h"
@ -614,8 +615,21 @@ void HistoryService::setMessageByAction(const MTPmessageAction &action) {
return result;
};
auto prepareGiftPremium = [](const MTPDmessageActionGiftPremium &action) {
return PreparedText{ .text = { "gift premium" }, }; // #TODO gifts
auto prepareGiftPremium = [&](
const MTPDmessageActionGiftPremium &action) {
history()->session().giftBoxStickersPacks().load();
const auto amount = action.vamount().v;
const auto currency = qs(action.vcurrency());
auto result = PreparedText{};
result.links.push_back(fromLink());
result.text = tr::lng_action_gift_received(
tr::now,
lt_user,
fromLinkText(), // Link 1.
lt_cost,
{ Ui::FillAmountAndCurrency(amount, currency) },
Ui::Text::WithEntities);
return result;
};
const auto messageText = action.match([&](
@ -745,6 +759,8 @@ void HistoryService::applyAction(const MTPMessageAction &action) {
channel->mgInfo->joinedMessageFound = true;
}
}
}, [&](const MTPDmessageActionGiftPremium &data) {
_media = std::make_unique<Data::MediaGiftBox>(this, data.vmonths().v);
}, [](const auto &) {
});
}

View File

@ -0,0 +1,281 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "history/view/media/history_view_service_media_gift.h"
#include "chat_helpers/stickers_gift_box_pack.h"
#include "core/click_handler_types.h" // ClickHandlerContext
#include "data/data_document.h"
#include "data/data_media_types.h"
#include "history/history.h"
#include "history/history_item.h"
#include "history/history_item_components.h"
#include "history/view/history_view_cursor_state.h"
#include "history/view/history_view_element.h"
#include "lang/lang_keys.h"
#include "lottie/lottie_common.h"
#include "lottie/lottie_single_player.h"
#include "main/main_session.h"
#include "settings/settings_premium.h" // Settings::ShowPremium
#include "ui/chat/chat_style.h"
#include "ui/effects/ripple_animation.h"
#include "window/window_session_controller.h"
#include "styles/style_chat.h"
#include "styles/style_premium.h"
#include "styles/style_settings.h"
namespace HistoryView {
namespace {
[[nodiscard]] QString FormatGiftMonths(int months) {
return (months < 12)
? tr::lng_premium_gift_duration_months(tr::now, lt_count, months)
: tr::lng_premium_gift_duration_years(
tr::now,
lt_count,
std::round(months / 12.));
}
} // namespace
MediaGift::MediaGift(
not_null<Element*> parent,
not_null<Data::MediaGiftBox*> gift)
: Media(parent)
, _parent(parent)
, _gift(gift)
, _size(st::msgServiceGiftBoxSize)
, _innerSize(_size - QSize(0, st::msgServiceGiftBoxTopSkip))
, _button([&] {
auto result = Button();
result.repaint = [=] { repaint(); };
result.text.setText(
st::semiboldTextStyle,
tr::lng_sticker_premium_view(tr::now));
const auto height = st::msgServiceGiftBoxButtonHeight;
const auto &margins = st::msgServiceGiftBoxButtonMargins;
result.size = QSize(
result.text.maxWidth()
+ height
+ margins.left()
+ margins.right(),
height);
result.link = std::make_shared<LambdaClickHandler>([=](
ClickContext context) {
const auto my = context.other.value<ClickHandlerContext>();
if (const auto controller = my.sessionWindow.get()) {
Settings::ShowPremium(controller, QString());
}
});
return result;
}())
, _title(
st::settingsSubsectionTitle.style,
tr::lng_premium_summary_title(tr::now))
, _subtitle(
st::premiumPreviewAbout.style,
FormatGiftMonths(gift->months())) {
}
MediaGift::~MediaGift() = default;
QSize MediaGift::countOptimalSize() {
return _size;
}
QSize MediaGift::countCurrentSize(int newWidth) {
return _size;
}
void MediaGift::draw(Painter &p, const PaintContext &context) const {
p.translate(0, st::msgServiceGiftBoxTopSkip);
PainterHighQualityEnabler hq(p);
const auto radius = st::msgServiceGiftBoxRadius;
p.setPen(Qt::NoPen);
p.setBrush(context.st->msgServiceBg());
p.drawRoundedRect(QRect(QPoint(), _innerSize), radius, radius);
{
p.setPen(context.st->msgServiceFg());
const auto &padding = st::msgServiceGiftBoxTitlePadding;
const auto titleTop = padding.top();
_title.draw(p, 0, titleTop, _innerSize.width(), style::al_top);
const auto subtitleTop = titleTop
+ _title.minHeight()
+ padding.bottom();
_subtitle.draw(p, 0, subtitleTop, _innerSize.width(), style::al_top);
}
{
const auto position = buttonRect().topLeft();
p.translate(position);
p.setPen(Qt::NoPen);
p.setBrush(context.st->msgServiceBg()); // ?
_button.drawBg(p);
p.setPen(context.st->msgServiceFg());
if (_button.ripple) {
const auto opacity = p.opacity();
p.setOpacity(st::historyPollRippleOpacity);
_button.ripple->paint(
p,
0,
0,
width(),
&context.messageStyle()->msgWaveformInactive->c);
p.setOpacity(opacity);
}
_button.text.draw(
p,
0,
(_button.size.height() - _button.text.minHeight()) / 2,
_button.size.width(),
style::al_top);
p.translate(-position);
}
if (_sticker) {
_sticker->draw(p, context, stickerRect());
} else {
ensureStickerCreated();
}
p.translate(0, -st::msgServiceGiftBoxTopSkip);
}
TextState MediaGift::textState(QPoint point, StateRequest request) const {
auto result = TextState(_parent);
{
const auto rect = buttonRect();
if (rect.contains(point)) {
result.link = _button.link;
_button.lastPoint = point - rect.topLeft();
}
}
return result;
}
bool MediaGift::toggleSelectionByHandlerClick(
const ClickHandlerPtr &p) const {
return false;
}
bool MediaGift::dragItemByHandler(const ClickHandlerPtr &p) const {
return false;
}
void MediaGift::clickHandlerPressedChanged(
const ClickHandlerPtr &handler,
bool pressed) {
if (!handler) {
return;
}
if (handler == _button.link) {
_button.toggleRipple(pressed);
}
}
DocumentData *MediaGift::getDocument() const {
return _sticker ? _sticker->document() : nullptr;
}
void MediaGift::stickerClearLoopPlayed() {
if (_sticker) {
_sticker->stickerClearLoopPlayed();
}
}
std::unique_ptr<Lottie::SinglePlayer> MediaGift::stickerTakeLottie(
not_null<DocumentData*> data,
const Lottie::ColorReplacements *replacements) {
return _sticker
? _sticker->stickerTakeLottie(data, replacements)
: nullptr;
}
bool MediaGift::needsBubble() const {
return false;
}
bool MediaGift::customInfoLayout() const {
return false;
}
bool MediaGift::hasHeavyPart() const {
return (_sticker ? _sticker->hasHeavyPart() : false);
}
void MediaGift::unloadHeavyPart() {
if (_sticker) {
_sticker->unloadHeavyPart();
}
}
void MediaGift::ensureStickerCreated() const {
if (_sticker) {
return;
}
const auto &session = _parent->data()->history()->session();
auto &packs = session.giftBoxStickersPacks();
if (const auto document = packs.lookup(_gift->months())) {
if (const auto sticker = document->sticker()) {
const auto skipPremiumEffect = false;
_sticker.emplace(_parent, document, skipPremiumEffect, _parent);
_sticker->setDiceIndex(sticker->alt, 1);
_sticker->initSize();
}
}
}
QRect MediaGift::buttonRect() const {
const auto &padding = st::msgServiceGiftBoxButtonPadding;
const auto position = QPoint(
(width() - _button.size.width()) / 2,
height() - padding.bottom() - _button.size.height());
return QRect(position, _button.size);
}
QRect MediaGift::stickerRect() const {
const auto &padding = st::msgServiceGiftBoxButtonPadding;
const auto &size = st::msgServiceGiftBoxStickerSize;
const auto top = st::msgServiceGiftBoxStickerTop;
return QRect(QPoint((width() - size.width()) / 2, top), size);
}
void MediaGift::Button::toggleRipple(bool pressed) {
if (pressed) {
const auto linkWidth = size.width();
const auto linkHeight = size.height();
if (!ripple) {
const auto drawMask = [&](QPainter &p) { drawBg(p); };
auto mask = Ui::RippleAnimation::maskByDrawer(
QSize(linkWidth, linkHeight),
false,
drawMask);
ripple = std::make_unique<Ui::RippleAnimation>(
st::defaultRippleAnimation,
std::move(mask),
repaint);
}
ripple->add(lastPoint);
} else if (ripple) {
ripple->lastStop();
}
}
void MediaGift::Button::drawBg(QPainter &p) const {
const auto radius = size.height() / 2.;
p.drawRoundedRect(0, 0, size.width(), size.height(), radius, radius);
}
} // namespace HistoryView

View File

@ -0,0 +1,89 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "history/view/media/history_view_media.h"
#include "history/view/media/history_view_media_unwrapped.h"
#include "history/view/media/history_view_sticker.h"
namespace Data {
class MediaGiftBox;
} // namespace Data
namespace Ui {
class RippleAnimation;
} // namespace Ui
namespace HistoryView {
class MediaGift final : public Media {
public:
MediaGift(not_null<Element*> parent, not_null<Data::MediaGiftBox*> gift);
~MediaGift();
QSize countOptimalSize() override;
QSize countCurrentSize(int newWidth) override;
void draw(Painter &p, const PaintContext &context) const override;
TextState textState(QPoint point, StateRequest request) const override;
[[nodiscard]] bool toggleSelectionByHandlerClick(
const ClickHandlerPtr &p) const override;
[[nodiscard]] bool dragItemByHandler(
const ClickHandlerPtr &p) const override;
void clickHandlerPressedChanged(
const ClickHandlerPtr &handler,
bool pressed) override;
DocumentData *getDocument() const override;
void stickerClearLoopPlayed() override;
std::unique_ptr<Lottie::SinglePlayer> stickerTakeLottie(
not_null<DocumentData*> data,
const Lottie::ColorReplacements *replacements) override;
[[nodiscard]] bool needsBubble() const override;
[[nodiscard]] bool customInfoLayout() const override;
bool hasHeavyPart() const override;
void unloadHeavyPart() override;
private:
void ensureStickerCreated() const;
[[nodiscard]] QRect buttonRect() const;
[[nodiscard]] QRect stickerRect() const;
const not_null<Element*> _parent;
const not_null<Data::MediaGiftBox*> _gift;
const QSize &_size;
const QSize _innerSize;
struct Button {
void drawBg(QPainter &p) const;
void toggleRipple(bool pressed);
Fn<void()> repaint;
Ui::Text::String text;
QSize size;
ClickHandlerPtr link;
std::unique_ptr<Ui::RippleAnimation> ripple;
mutable QPoint lastPoint;
} _button;
Ui::Text::String _title;
Ui::Text::String _subtitle;
mutable std::optional<Sticker> _sticker;
};
} // namespace HistoryView

View File

@ -31,6 +31,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_file_click_handler.h"
#include "data/data_file_origin.h"
#include "lottie/lottie_single_player.h"
#include "chat_helpers/stickers_gift_box_pack.h"
#include "chat_helpers/stickers_lottie.h"
#include "styles/style_chat.h"
@ -111,7 +112,12 @@ bool Sticker::isEmojiSticker() const {
void Sticker::initSize() {
if (isEmojiSticker() || _diceIndex >= 0) {
_size = EmojiSize();
const auto &session = _data->owner().session();
if (session.giftBoxStickersPacks().isGiftSticker(_data)) {
_size = st::msgServiceGiftBoxStickerSize;
} else {
_size = EmojiSize();
}
if (_diceIndex > 0) {
[[maybe_unused]] bool result = readyToDrawLottie();
}

View File

@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mtproto/mtproto_config.h"
#include "chat_helpers/stickers_emoji_pack.h"
#include "chat_helpers/stickers_dice_pack.h"
#include "chat_helpers/stickers_gift_box_pack.h"
#include "inline_bots/bot_attach_web_view.h"
#include "storage/file_download.h"
#include "storage/download_manager_mtproto.h"
@ -91,6 +92,7 @@ Session::Session(
, _user(_data->processUser(user))
, _emojiStickersPack(std::make_unique<Stickers::EmojiPack>(this))
, _diceStickersPacks(std::make_unique<Stickers::DicePacks>(this))
, _giftBoxStickersPacks(std::make_unique<Stickers::GiftBoxPack>(this))
, _sendAsPeers(std::make_unique<SendAsPeers>(this))
, _attachWebView(std::make_unique<InlineBots::AttachWebView>(this))
, _supportHelper(Support::Helper::Create(this))

View File

@ -51,6 +51,7 @@ struct TermsLock;
namespace Stickers {
class EmojiPack;
class DicePacks;
class GiftBoxPack;
} // namespace Stickers;
namespace InlineBots {
@ -117,6 +118,9 @@ public:
[[nodiscard]] Stickers::DicePacks &diceStickersPacks() const {
return *_diceStickersPacks;
}
[[nodiscard]] Stickers::GiftBoxPack &giftBoxStickersPacks() const {
return *_giftBoxStickersPacks;
}
[[nodiscard]] Data::Session &data() const {
return *_data;
}
@ -205,6 +209,7 @@ private:
// _emojiStickersPack depends on _data.
const std::unique_ptr<Stickers::EmojiPack> _emojiStickersPack;
const std::unique_ptr<Stickers::DicePacks> _diceStickersPacks;
const std::unique_ptr<Stickers::GiftBoxPack> _giftBoxStickersPacks;
const std::unique_ptr<SendAsPeers> _sendAsPeers;
const std::unique_ptr<InlineBots::AttachWebView> _attachWebView;

View File

@ -1077,3 +1077,13 @@ searchInChatPeerListItem: PeerListItem(defaultPeerListItem) {
searchInChatPeerList: PeerList(defaultPeerList) {
item: searchInChatPeerListItem;
}
msgServiceGiftBoxSize: size(206px, 231px); // Plus msgServiceGiftBoxTopSkip.
msgServiceGiftBoxRadius: 12px;
msgServiceGiftBoxTopSkip: 4px;
msgServiceGiftBoxButtonHeight: 32px;
msgServiceGiftBoxButtonMargins: margins(2px, 0px, 2px, 0px);
msgServiceGiftBoxButtonPadding: margins(0px, 17px, 0px, 17px);
msgServiceGiftBoxTitlePadding: margins(0px, 126px, 0px, 2px);
msgServiceGiftBoxStickerTop: -30px;
msgServiceGiftBoxStickerSize: size(150px, 150px);