From 7b5985445c735a886d7cc0ccb663b8e83c08fe96 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 5 Mar 2017 18:41:58 +0300 Subject: [PATCH] Payment service messages supported. --- Telegram/Resources/langs/lang.strings | 2 + .../history/history_media_types.cpp | 6 +-- .../SourceFiles/history/history_media_types.h | 7 ++- .../SourceFiles/history/history_message.cpp | 54 ++++++++++++++++--- .../SourceFiles/history/history_message.h | 7 +++ 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index c3ca3752ca..f25966002a 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -670,6 +670,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org "lng_action_call_incoming_duration" = "Incoming call ({duration}) at {time}"; "lng_action_call_incoming_missed" = "Missed call at {time}"; "lng_action_call_outgoing_missed" = "Cancelled call at {time}"; +"lng_action_payment_done" = "You have just successfully transferred {amount} to {user}"; +"lng_action_payment_done_for" = "You have just successfully transferred {amount} to {user} for {invoice}"; "lng_profile_migrate_reached" = "{count:_not_used_|# member|# members} limit reached"; "lng_profile_migrate_body" = "To get over this limit, you can upgrade your group to a supergroup."; diff --git a/Telegram/SourceFiles/history/history_media_types.cpp b/Telegram/SourceFiles/history/history_media_types.cpp index 2314a565d8..4b039d33e5 100644 --- a/Telegram/SourceFiles/history/history_media_types.cpp +++ b/Telegram/SourceFiles/history/history_media_types.cpp @@ -3441,9 +3441,7 @@ HistoryInvoice::HistoryInvoice(HistoryItem *parent, const HistoryInvoice &other) , _status(other._status) { } -namespace { - -QString fillAmountAndCurrency(int amount, const QString ¤cy) { +QString HistoryInvoice::fillAmountAndCurrency(int amount, const QString ¤cy) { static auto shortCurrencyNames = QMap { { qsl("USD"), qsl("$") }, { qsl("GBP"), qsl("£") }, @@ -3457,8 +3455,6 @@ QString fillAmountAndCurrency(int amount, const QString ¤cy) { return currencyText + amountText; } -} // namespace - void HistoryInvoice::fillFromData(const MTPDmessageMediaInvoice &data) { // init attach if (data.has_photo()) { diff --git a/Telegram/SourceFiles/history/history_media_types.h b/Telegram/SourceFiles/history/history_media_types.h index 16f4269c0b..be472a3300 100644 --- a/Telegram/SourceFiles/history/history_media_types.h +++ b/Telegram/SourceFiles/history/history_media_types.h @@ -876,7 +876,7 @@ public: HistoryInvoice(HistoryItem *parent, const MTPDmessageMediaInvoice &data); HistoryInvoice(HistoryItem *parent, const HistoryInvoice &other); HistoryMediaType type() const override { - return MediaTypeGame; + return MediaTypeInvoice; } std::unique_ptr clone(HistoryItem *newParent) const override { return std::make_unique(newParent, *this); @@ -885,6 +885,11 @@ public: void initDimensions() override; int resizeGetHeight(int width) override; + QString getTitle() const { + return _title.originalText(); + } + static QString fillAmountAndCurrency(int amount, const QString ¤cy); + void draw(Painter &p, const QRect &r, TextSelection selection, TimeMs ms) const override; HistoryTextState getState(int x, int y, HistoryStateRequest request) const override; diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 695683b11f..db1ebace09 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -2011,6 +2011,7 @@ void HistoryService::setMessageByAction(const MTPmessageAction &action) { case mtpc_messageActionPinMessage: messageText = preparePinnedText(); break; case mtpc_messageActionGameScore: messageText = prepareGameScoreText(); break; case mtpc_messageActionPhoneCall: messageText = preparePhoneCallText(action.c_messageActionPhoneCall()); break; + case mtpc_messageActionPaymentSent: messageText = preparePaymentSentText(); break; default: messageText.text = lang(lng_message_empty); break; } @@ -2151,16 +2152,16 @@ HistoryService::PreparedText HistoryService::preparePinnedText() { } HistoryService::PreparedText HistoryService::prepareGameScoreText() { - auto result = PreparedText { QString(), { peerOpenClickHandler(_from) } }; + auto result = PreparedText {}; auto gamescore = Get(); - auto gameTitle = ([gamescore, &result]() -> QString { + auto computeGameTitle = [gamescore, &result]() -> QString { if (gamescore && gamescore->msg) { if (auto media = gamescore->msg->getMedia()) { if (media->type() == MediaTypeGame) { result.links.push_back(MakeShared(gamescore->msg, 0, 0)); auto titleText = static_cast(media)->game()->title; - return textcmdLink(2, titleText); + return textcmdLink(result.links.size(), titleText); } } return lang(lng_deleted_message); @@ -2168,26 +2169,54 @@ HistoryService::PreparedText HistoryService::prepareGameScoreText() { return lang(lng_contacts_loading); } return QString(); - })(); + }; auto scoreNumber = gamescore ? gamescore->score : 0; if (_from->isSelf()) { + auto gameTitle = computeGameTitle(); if (gameTitle.isEmpty()) { result.text = lng_action_game_you_scored_no_game(lt_count, scoreNumber); } else { result.text = lng_action_game_you_scored(lt_count, scoreNumber, lt_game, gameTitle); } } else { - auto from = textcmdLink(1, _from->name); + result.links.push_back(fromLink()); + auto gameTitle = computeGameTitle(); if (gameTitle.isEmpty()) { - result.text = lng_action_game_score_no_game(lt_from, from, lt_count, scoreNumber); + result.text = lng_action_game_score_no_game(lt_from, fromLinkText(), lt_count, scoreNumber); } else { - result.text = lng_action_game_score(lt_from, from, lt_count, scoreNumber, lt_game, gameTitle); + result.text = lng_action_game_score(lt_from, fromLinkText(), lt_count, scoreNumber, lt_game, gameTitle); } } return result; } +HistoryService::PreparedText HistoryService::preparePaymentSentText() { + auto result = PreparedText {}; + auto payment = Get(); + + auto invoiceTitle = ([payment]() -> QString { + if (payment && payment->msg) { + if (auto media = payment->msg->getMedia()) { + if (media->type() == MediaTypeInvoice) { + return static_cast(media)->getTitle(); + } + } + return lang(lng_deleted_message); + } else if (payment && payment->msgId) { + return lang(lng_contacts_loading); + } + return QString(); + })(); + + if (invoiceTitle.isEmpty()) { + result.text = lng_action_payment_done(lt_amount, payment->amount, lt_user, history()->peer->name); + } else { + result.text = lng_action_payment_done_for(lt_amount, payment->amount, lt_user, history()->peer->name, lt_invoice, invoiceTitle); + } + return result; +} + HistoryService::HistoryService(History *history, const MTPDmessageService &msg) : HistoryItem(history, msg.vid.v, mtpCastFlags(msg.vflags.v), ::date(msg.vdate), msg.has_from_id() ? msg.vfrom_id.v : 0) { createFromMtp(msg); @@ -2366,6 +2395,10 @@ HistoryTextState HistoryService::getState(int x, int y, HistoryStateRequest requ if (!result.link && result.cursor == HistoryInTextCursorState && outer.contains(x, y)) { result.link = gamescore->lnk; } + } else if (auto payment = Get()) { + if (!result.link && result.cursor == HistoryInTextCursorState && outer.contains(x, y)) { + result.link = payment->lnk; + } } } else if (_media) { result = _media->getState(x - st::msgServiceMargin.left() - (width - _media->maxWidth()) / 2, y - st::msgServiceMargin.top() - height - st::msgServiceMargin.top(), request); @@ -2377,6 +2410,11 @@ void HistoryService::createFromMtp(const MTPDmessageService &message) { if (message.vaction.type() == mtpc_messageActionGameScore) { UpdateComponents(HistoryServiceGameScore::Bit()); Get()->score = message.vaction.c_messageActionGameScore().vscore.v; + } else if (message.vaction.type() == mtpc_messageActionPaymentSent) { + UpdateComponents(HistoryServicePayment::Bit()); + auto amount = message.vaction.c_messageActionPaymentSent().vtotal_amount.v; + auto currency = qs(message.vaction.c_messageActionPaymentSent().vcurrency); + Get()->amount = HistoryInvoice::fillAmountAndCurrency(amount, currency); } if (message.has_reply_to_msg_id()) { if (message.vaction.type() == mtpc_messageActionPinMessage) { @@ -2447,6 +2485,8 @@ void HistoryService::updateDependentText() { text = preparePinnedText(); } else if (Has()) { text = prepareGameScoreText(); + } else if (Has()) { + text = preparePaymentSentText(); } else { return; } diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index f3fcc25870..540a13c492 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -250,6 +250,10 @@ struct HistoryServiceGameScore : public RuntimeComponent, public HistoryServiceDependentData { + QString amount; +}; + namespace HistoryLayout { class ServiceMessagePainter; } // namespace HistoryLayout @@ -344,6 +348,8 @@ private: return pinned; } else if (auto gamescore = Get()) { return gamescore; + } else if (auto payment = Get()) { + return payment; } return nullptr; } @@ -359,6 +365,7 @@ private: PreparedText preparePinnedText(); PreparedText prepareGameScoreText(); + PreparedText preparePaymentSentText(); };