diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index ff5d87bc69..cb92894cba 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -645,6 +645,8 @@ PRIVATE history/history_item.h history/history_item_components.cpp history/history_item_components.h + history/history_item_edition.cpp + history/history_item_edition.h history/history_item_reply_markup.cpp history/history_item_reply_markup.h history/history_item_text.cpp diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index a000929cd5..114ea55762 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -335,7 +335,7 @@ void SendConfirmedFile( isEditing ? file->to.replaceMediaOf : session->data().nextLocalMessageId()); - auto groupId = file->album ? file->album->groupId : uint64(0); + const auto groupId = file->album ? file->album->groupId : uint64(0); if (file->album) { const auto proj = [](const SendingAlbum::Item &item) { return item.taskId; @@ -370,13 +370,6 @@ void SendConfirmedFile( session->user()).flags; TextUtilities::PrepareForSending(caption, prepareFlags); TextUtilities::Trim(caption); - auto localEntities = Api::EntitiesToMTP(session, caption.entities); - - if (itemToEdit) { - if (const auto id = itemToEdit->groupId()) { - groupId = id.value; - } - } auto flags = isEditing ? MessageFlags() : NewMessageFlags(peer); if (file->to.replyTo) { @@ -408,7 +401,7 @@ void SendConfirmedFile( ? session->user()->name : QString(); - const auto media = [&] { + const auto media = MTPMessageMedia([&] { if (file->type == SendMediaType::Photo) { return MTP_messageMediaPhoto( MTP_flags(MTPDmessageMediaPhoto::Flag::f_photo), @@ -427,38 +420,21 @@ void SendConfirmedFile( } else { Unexpected("Type in sendFilesConfirmed."); } - }(); + }()); if (itemToEdit) { itemToEdit->savePreviousMedia(); - itemToEdit->applyEdition(MTP_message( - MTP_flags(MTPDmessage::Flag::f_media - | ((flags & MessageFlag::HideEdited) - ? MTPDmessage::Flag::f_edit_hide - : MTPDmessage::Flag()) - | (localEntities.v.isEmpty() - ? MTPDmessage::Flag() - : MTPDmessage::Flag::f_entities)), - MTP_int(0), // Not used (would've been trimmed to 32 bits). - peerToMTP(messageFromId), - peerToMTP(file->to.peer), - MTPMessageFwdHeader(), - MTPlong(), // via_bot_id - replyHeader, - MTP_int(HistoryItem::NewMessageDate(file->to.options.scheduled)), - MTP_string(caption.text), - media, - MTPReplyMarkup(), - localEntities, - MTPint(), // views - MTPint(), // forwards - MTPMessageReplies(), - MTPint(), // edit_date - MTP_string(messagePostAuthor), - MTP_long(groupId), - //MTPMessageReactions(), - MTPVector(), - MTPint()).c_message()); + auto edition = HistoryMessageEdition(); + edition.isEditHide = (flags & MessageFlag::HideEdited); + edition.editDate = 0; + edition.views = 0; + edition.forwards = 0; + edition.ttl = 0; + edition.mtpMedia = &media; + edition.textWithEntities = caption; + edition.useSameMarkup = true; + edition.useSameReplies = true; + itemToEdit->applyEdition(std::move(edition)); } else { const auto viaBotId = UserId(); history->addNewLocalMessage( diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.cpp b/Telegram/SourceFiles/data/data_scheduled_messages.cpp index 7c9546ca03..fc5e04bb90 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.cpp +++ b/Telegram/SourceFiles/data/data_scheduled_messages.cpp @@ -448,7 +448,7 @@ HistoryItem *ScheduledMessages::append( // so if we receive a flag about it, // probably this message was edited. if (data.is_edit_hide()) { - existing->applyEdition(data); + existing->applyEdition(HistoryMessageEdition(_session, data)); } existing->updateSentContent({ qs(data.vmessage()), diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 37ff099bc6..2fabbf3ac5 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -1826,8 +1826,10 @@ void Session::updateEditedMessage(const MTPMessage &data) { checkEntitiesAndViewsUpdate(data.c_message()); } data.match([](const MTPDmessageEmpty &) { - }, [&](const auto &data) { + }, [&](const MTPDmessageService &data) { existing->applyEdition(data); + }, [&](const auto &data) { + existing->applyEdition(HistoryMessageEdition(_session, data)); }); } diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 695bf9bfee..f6729b90be 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/flags.h" #include "base/value_ordering.h" #include "data/data_media_types.h" +#include "history/history_item_edition.h" #include "history/history_item_reply_markup.h" #include @@ -277,7 +278,7 @@ public: [[nodiscard]] virtual bool serviceMsg() const { return false; } - virtual void applyEdition(const MTPDmessage &message) { + virtual void applyEdition(HistoryMessageEdition &&edition) { } virtual void applyEdition(const MTPDmessageService &message) { } diff --git a/Telegram/SourceFiles/history/history_item_edition.cpp b/Telegram/SourceFiles/history/history_item_edition.cpp new file mode 100644 index 0000000000..78935394ad --- /dev/null +++ b/Telegram/SourceFiles/history/history_item_edition.cpp @@ -0,0 +1,34 @@ +/* +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/history_item_edition.h" + +#include "api/api_text_entities.h" +#include "main/main_session.h" + +HistoryMessageEdition::HistoryMessageEdition( + not_null session, + const MTPDmessage &message) { + isEditHide = message.is_edit_hide(); + editDate = message.vedit_date().value_or(-1); + textWithEntities = TextWithEntities{ + qs(message.vmessage()), + Api::EntitiesFromMTP( + session, + message.ventities().value_or_empty()) + }; + replyMarkup = HistoryMessageMarkupData(message.vreply_markup()); + mtpMedia = message.vmedia(); + views = message.vviews().value_or(-1); + forwards = message.vforwards().value_or(-1); + if (const auto mtpReplies = message.vreplies()) { + replies = HistoryMessageRepliesData(mtpReplies); + } + + const auto period = message.vttl_period(); + ttl = (period && period->v > 0) ? (message.vdate().v + period->v) : 0; +} diff --git a/Telegram/SourceFiles/history/history_item_edition.h b/Telegram/SourceFiles/history/history_item_edition.h new file mode 100644 index 0000000000..8bc25a1d59 --- /dev/null +++ b/Telegram/SourceFiles/history/history_item_edition.h @@ -0,0 +1,33 @@ +/* +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/history_item_reply_markup.h" + +namespace Main { +class Session; +} // namespace Main + +struct HistoryMessageEdition { + explicit HistoryMessageEdition() = default; + HistoryMessageEdition( + not_null session, + const MTPDmessage &message); + + bool isEditHide = false; + int editDate = 0; + int views = -1; + int forwards = -1; + int ttl = 0; + bool useSameReplies = false; + bool useSameMarkup = false; + TextWithEntities textWithEntities; + HistoryMessageMarkupData replyMarkup; + HistoryMessageRepliesData replies; + const MTPMessageMedia *mtpMedia = nullptr; +}; diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.cpp b/Telegram/SourceFiles/history/history_item_reply_markup.cpp index 0a52fb42b8..9950b2696d 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.cpp +++ b/Telegram/SourceFiles/history/history_item_reply_markup.cpp @@ -213,4 +213,5 @@ HistoryMessageRepliesData::HistoryMessageRepliesData( readMaxId = fields.vread_max_id().value_or_empty(); maxId = fields.vmax_id().value_or_empty(); isNull = false; + pts = fields.vreplies_pts().v; } diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.h b/Telegram/SourceFiles/history/history_item_reply_markup.h index db06327804..8418bc50fc 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.h +++ b/Telegram/SourceFiles/history/history_item_reply_markup.h @@ -92,4 +92,5 @@ struct HistoryMessageRepliesData { MsgId maxId = 0; int repliesCount = 0; bool isNull = true; + int pts = 0; }; diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index e316b77528..d2821ec25d 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -1156,15 +1156,13 @@ void HistoryMessage::createComponents(CreateConfig &&config) { _fromNameVersion = from ? from->nameVersion : 1; } -bool HistoryMessage::checkRepliesPts(const MTPMessageReplies &data) const { +bool HistoryMessage::checkRepliesPts( + const HistoryMessageRepliesData &data) const { const auto channel = history()->peer->asChannel(); const auto pts = channel ? channel->pts() : history()->session().updates().pts(); - const auto repliesPts = data.match([&](const MTPDmessageReplies &data) { - return data.vreplies_pts().v; - }); - return (repliesPts >= pts); + return (data.pts >= pts); } void HistoryMessage::setupForwardedComponent(const CreateConfig &config) { @@ -1380,7 +1378,7 @@ void HistoryMessage::replaceBuyWithReceiptInMarkup() { } } -void HistoryMessage::applyEdition(const MTPDmessage &message) { +void HistoryMessage::applyEdition(HistoryMessageEdition &&edition) { int keyboardTop = -1; //if (!pendingResize()) {// #TODO edit bot message // if (auto keyboard = inlineReplyKeyboard()) { @@ -1389,47 +1387,43 @@ void HistoryMessage::applyEdition(const MTPDmessage &message) { // } //} - if (message.is_edit_hide()) { + if (edition.isEditHide) { _flags |= MessageFlag::HideEdited; } else { _flags &= ~MessageFlag::HideEdited; } - if (const auto editDate = message.vedit_date()) { + if (edition.editDate != -1) { //_flags |= MTPDmessage::Flag::f_edit_date; if (!Has()) { AddComponents(HistoryMessageEdited::Bit()); } auto edited = Get(); - edited->date = editDate->v; + edited->date = edition.editDate; } - const auto textWithEntities = TextWithEntities{ - qs(message.vmessage()), - Api::EntitiesFromMTP( - &history()->session(), - message.ventities().value_or_empty()) - }; - setReplyMarkup(HistoryMessageMarkupData(message.vreply_markup())); + if (!edition.useSameMarkup) { + setReplyMarkup(base::take(edition.replyMarkup)); + } if (!isLocalUpdateMedia()) { - refreshMedia(message.vmedia()); + refreshMedia(edition.mtpMedia); } - setViewsCount(message.vviews().value_or(-1)); - setForwardsCount(message.vforwards().value_or(-1)); - setText(_media ? textWithEntities : EnsureNonEmpty(textWithEntities)); - if (const auto replies = message.vreplies()) { - if (checkRepliesPts(*replies)) { - setReplies(HistoryMessageRepliesData(replies)); + setViewsCount(edition.views); + setForwardsCount(edition.forwards); + setText(_media + ? edition.textWithEntities + : EnsureNonEmpty(edition.textWithEntities)); + if (!edition.useSameReplies) { + if (!edition.replies.isNull) { + if (checkRepliesPts(edition.replies)) { + setReplies(base::take(edition.replies)); + } + } else { + clearReplies(); } - } else { - clearReplies(); } - if (const auto period = message.vttl_period(); period && period->v > 0) { - applyTTL(message.vdate().v + period->v); - } else { - applyTTL(0); - } + applyTTL(edition.ttl); finishEdition(keyboardTop); } diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 4f16a8d6d3..3df1944a5d 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -159,7 +159,7 @@ public: // f_forwards // f_replies // f_ttl_period - void applyEdition(const MTPDmessage &message) override; + void applyEdition(HistoryMessageEdition &&edition) override; void applyEdition(const MTPDmessageService &message) override; void updateSentContent( @@ -269,7 +269,8 @@ private: const TextWithEntities &textWithEntities) const; void reapplyText(); - [[nodiscard]] bool checkRepliesPts(const MTPMessageReplies &data) const; + [[nodiscard]] bool checkRepliesPts( + const HistoryMessageRepliesData &data) const; QString _timeText; int _timeWidth = 0;