From 05488022c7b0ff4bf74c31319b0c167cfa08c745 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 12 Feb 2021 19:15:17 +0400 Subject: [PATCH] Use ttl_period from service messages as well. --- Telegram/SourceFiles/history/history_item.cpp | 39 ++++++++++++++++++- Telegram/SourceFiles/history/history_item.h | 13 +++++-- .../SourceFiles/history/history_message.cpp | 28 ++----------- .../SourceFiles/history/history_message.h | 8 ---- .../SourceFiles/history/history_service.cpp | 2 + .../SourceFiles/history/history_widget.cpp | 4 +- 6 files changed, 55 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index e823d29f19..1498221ec1 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -809,6 +809,41 @@ bool HistoryItem::canUpdateDate() const { return isScheduled(); } +void HistoryItem::applyTTL(const MTPDmessage &data) { + if (const auto period = data.vttl_period()) { + if (period->v > 0) { + applyTTL(data.vdate().v + period->v); + } + } +} + +void HistoryItem::applyTTL(const MTPDmessageService &data) { + if (const auto period = data.vttl_period()) { + if (period->v > 0) { + applyTTL(data.vdate().v + period->v); + } + } +} + +void HistoryItem::applyTTL(TimeId destroyAt) { + const auto previousDestroyAt = std::exchange(_ttlDestroyAt, destroyAt); + if (previousDestroyAt) { + history()->owner().unregisterMessageTTL(previousDestroyAt, this); + } + if (!_ttlDestroyAt) { + return; + } else if (base::unixtime::now() >= _ttlDestroyAt) { + const auto session = &history()->session(); + crl::on_main(session, [session, id = fullId()]{ + if (const auto item = session->data().message(id)) { + item->destroy(); + } + }); + } else { + history()->owner().registerMessageTTL(_ttlDestroyAt, this); + } +} + void HistoryItem::sendFailed() { Expects(_clientFlags & MTPDmessage_ClientFlag::f_sending); Expects(!(_clientFlags & MTPDmessage_ClientFlag::f_failed)); @@ -957,7 +992,9 @@ void HistoryItem::drawInDialog( p.restoreTextPalette(); } -HistoryItem::~HistoryItem() = default; +HistoryItem::~HistoryItem() { + applyTTL(0); +} QDateTime ItemDateTime(not_null item) { return base::unixtime::parse(item->date()); diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 778d08165e..9ff851a6d6 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -79,9 +79,6 @@ public: [[nodiscard]] virtual bool notificationReady() const { return true; } - [[nodiscard]] virtual TimeId ttlDestroyAt() const { - return 0; - } [[nodiscard]] PeerData *specialNotificationPeer() const; [[nodiscard]] UserData *viaBot() const; @@ -399,6 +396,10 @@ public: void updateDate(TimeId newDate); [[nodiscard]] bool canUpdateDate() const; + [[nodiscard]] TimeId ttlDestroyAt() const { + return _ttlDestroyAt; + } + virtual ~HistoryItem(); MsgId id; @@ -427,6 +428,10 @@ protected: void setGroupId(MessageGroupId groupId); + void applyTTL(const MTPDmessage &data); + void applyTTL(const MTPDmessageService &data); + void applyTTL(TimeId destroyAt); + Ui::Text::String _text = { st::msgMinWidth }; int _textWidth = -1; int _textHeight = 0; @@ -440,7 +445,9 @@ protected: std::unique_ptr _media; private: + TimeId _date = 0; + TimeId _ttlDestroyAt = 0; HistoryView::Element *_mainView = nullptr; friend class HistoryView::Element; diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 1a0b7e5550..42f4fc544b 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -525,11 +525,7 @@ HistoryMessage::HistoryMessage( MessageGroupId::FromRaw(history->peer->id, groupedId->v)); } - if (const auto period = data.vttl_period()) { - if (period->v > 0) { - applyTTL(data.vdate().v + period->v); - } - } + applyTTL(data); } HistoryMessage::HistoryMessage( @@ -566,6 +562,8 @@ HistoryMessage::HistoryMessage( }, [](const auto &) { Unexpected("Service message action type in HistoryMessage."); }); + + applyTTL(data); } HistoryMessage::HistoryMessage( @@ -1403,25 +1401,6 @@ void HistoryMessage::applyEdition(const MTPDmessageService &message) { } } -void HistoryMessage::applyTTL(TimeId destroyAt) { - const auto previousDestroyAt = std::exchange(_ttlDestroyAt, destroyAt); - if (previousDestroyAt) { - history()->owner().unregisterMessageTTL(previousDestroyAt, this); - } - if (!_ttlDestroyAt) { - return; - } else if (base::unixtime::now() >= _ttlDestroyAt) { - const auto session = &history()->session(); - crl::on_main(session, [session, id = fullId()]{ - if (const auto item = session->data().message(id)) { - item->destroy(); - } - }); - } else { - history()->owner().registerMessageTTL(_ttlDestroyAt, this); - } -} - void HistoryMessage::updateSentContent( const TextWithEntities &textWithEntities, const MTPMessageMedia *media) { @@ -1927,7 +1906,6 @@ std::unique_ptr HistoryMessage::createView( } HistoryMessage::~HistoryMessage() { - applyTTL(0); _media.reset(); clearSavedMedia(); if (auto reply = Get()) { diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 2c832c797c..36e3ac6539 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -194,10 +194,6 @@ public: const MTPDupdateShortSentMessage &data, bool wasAlready) override; - [[nodiscard]] TimeId ttlDestroyAt() const override { - return _ttlDestroyAt; - } - // dynamic_cast optimization. [[nodiscard]] HistoryMessage *toHistoryMessage() override { return this; @@ -250,8 +246,6 @@ private: const TextWithEntities &textWithEntities) const; void reapplyText(); - void applyTTL(TimeId destroyAt); - [[nodiscard]] bool checkRepliesPts(const MTPMessageReplies &data) const; QString _timeText; @@ -259,8 +253,6 @@ private: mutable int32 _fromNameVersion = 0; - TimeId _ttlDestroyAt = 0; - friend class HistoryView::Element; friend class HistoryView::Message; diff --git a/Telegram/SourceFiles/history/history_service.cpp b/Telegram/SourceFiles/history/history_service.cpp index 8f7f733fe0..a4b0988c80 100644 --- a/Telegram/SourceFiles/history/history_service.cpp +++ b/Telegram/SourceFiles/history/history_service.cpp @@ -749,6 +749,7 @@ HistoryService::HistoryService( data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { createFromMtp(data); + applyTTL(data); } HistoryService::HistoryService( @@ -763,6 +764,7 @@ HistoryService::HistoryService( data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { createFromMtp(data); + applyTTL(data); } HistoryService::HistoryService( diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 60725698b3..5190581a6c 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -3716,8 +3716,8 @@ bool HistoryWidget::isJoinChannel() const { bool HistoryWidget::isMuteUnmute() const { return _peer - && ((_peer->isBroadcast() - && !_peer->asChannel()->canPublish()) + && ((_peer->isBroadcast() && !_peer->asChannel()->canPublish()) + || (_peer->isGigagroup() && !_peer->asChannel()->canWrite()) || _peer->isRepliesChat()); }