From 0c1312419a5c40301cf72cc4ec8f7e32aa23568b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 7 Oct 2020 13:08:54 +0300 Subject: [PATCH] Fixed crash when user schedules message with elapsed date. Fixed #8764. --- .../data/data_scheduled_messages.cpp | 32 +++++++++++++------ Telegram/SourceFiles/history/history_item.cpp | 3 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.cpp b/Telegram/SourceFiles/data/data_scheduled_messages.cpp index 2c54b7e27..9970c811a 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.cpp +++ b/Telegram/SourceFiles/data/data_scheduled_messages.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "data/data_scheduled_messages.h" +#include "base/unixtime.h" #include "data/data_peer.h" #include "data/data_session.h" #include "api/api_hash.h" @@ -26,6 +27,11 @@ constexpr auto kRequestTimeLimit = 60 * crl::time(1000); return (received > 0) && (received + kRequestTimeLimit > crl::now()); } +[[nodiscard]] bool HasScheduledDate(not_null item) { + return (item->date() != ScheduledMessages::kScheduledUntilOnlineTimestamp) + && (item->date() > base::unixtime::now()); +} + MTPMessage PrepareMessage(const MTPMessage &message, MsgId id) { return message.match([&](const MTPDmessageEmpty &) { return MTP_messageEmpty(MTP_int(id)); @@ -147,7 +153,11 @@ void ScheduledMessages::sendNowSimpleMessage( not_null local) { Expects(local->isSending()); Expects(local->isScheduled()); - Expects(local->date() == kScheduledUntilOnlineTimestamp); + if (HasScheduledDate(local)) { + LOG(("Error: trying to put to history a new local message, " + "that has scheduled date.")); + return; + } // When the user sends a text message scheduled until online // while the recipient is already online, the server sends @@ -243,16 +253,18 @@ void ScheduledMessages::checkEntitiesAndUpdate(const MTPDmessage &data) { } const auto existing = j->second; - Assert(existing->date() == kScheduledUntilOnlineTimestamp); - existing->updateSentContent({ - qs(data.vmessage()), - Api::EntitiesFromMTP(_session, data.ventities().value_or_empty()) - }, data.vmedia()); - existing->updateReplyMarkup(data.vreply_markup()); - existing->updateForwardedInfo(data.vfwd_from()); - _session->data().requestItemTextRefresh(existing); + if (!HasScheduledDate(existing)) { + // Destroy a local message, that should be in history. + existing->updateSentContent({ + qs(data.vmessage()), + Api::EntitiesFromMTP(_session, data.ventities().value_or_empty()) + }, data.vmedia()); + existing->updateReplyMarkup(data.vreply_markup()); + existing->updateForwardedInfo(data.vfwd_from()); + _session->data().requestItemTextRefresh(existing); - existing->destroy(); + existing->destroy(); + } } void ScheduledMessages::apply( diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 9f82ff36a..ad038352a 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -191,8 +191,7 @@ TimeId HistoryItem::date() const { } TimeId HistoryItem::NewMessageDate(TimeId scheduled) { - const auto now = base::unixtime::now(); - return scheduled ? std::max(scheduled, now + 60) : now; + return scheduled ? scheduled : base::unixtime::now(); } void HistoryItem::finishEdition(int oldKeyboardTop) {