From 15254599e20c01f52e0120408504a0194e019763 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 11 Jan 2021 00:10:29 +0300 Subject: [PATCH] Unified checking for editable message. --- .../data/data_scheduled_messages.cpp | 12 +++++++---- .../data/data_scheduled_messages.h | 3 ++- Telegram/SourceFiles/history/history.cpp | 5 +++-- Telegram/SourceFiles/history/history.h | 2 +- Telegram/SourceFiles/history/history_item.cpp | 21 ------------------- Telegram/SourceFiles/history/history_item.h | 2 -- .../SourceFiles/history/history_widget.cpp | 3 +-- .../view/history_view_replies_section.cpp | 2 +- .../view/history_view_scheduled_section.cpp | 5 +++-- 9 files changed, 19 insertions(+), 36 deletions(-) diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.cpp b/Telegram/SourceFiles/data/data_scheduled_messages.cpp index c77f95ca3..a8bb8b8cb 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.cpp +++ b/Telegram/SourceFiles/data/data_scheduled_messages.cpp @@ -543,7 +543,8 @@ int32 ScheduledMessages::countListHash(const List &list) const { return HashFinalize(hash); } -HistoryItem *ScheduledMessages::lastSentMessage(not_null history) { +HistoryItem *ScheduledMessages::lastEditableMessage( + not_null history) { const auto i = _data.find(history); if (i == end(_data)) { return nullptr; @@ -552,9 +553,12 @@ HistoryItem *ScheduledMessages::lastSentMessage(not_null history) { sort(list); const auto items = ranges::view::reverse(list.items); - const auto it = ranges::find_if( - items, - &HistoryItem::canBeEditedFromHistory); + + const auto now = base::unixtime::now(); + auto proj = [&](const OwnedItem &item) { + return item->allowsEdit(now); + }; + const auto it = ranges::find_if(items, std::move(proj)); return (it == end(items)) ? nullptr : (*it).get(); } diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.h b/Telegram/SourceFiles/data/data_scheduled_messages.h index 3391cd382..650434c2c 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.h +++ b/Telegram/SourceFiles/data/data_scheduled_messages.h @@ -32,7 +32,8 @@ public: [[nodiscard]] HistoryItem *lookupItem(PeerId peer, MsgId msg) const; [[nodiscard]] HistoryItem *lookupItem(FullMsgId itemId) const; [[nodiscard]] int count(not_null history) const; - [[nodiscard]] HistoryItem *lastSentMessage(not_null history); + [[nodiscard]] HistoryItem *lastEditableMessage( + not_null history); void checkEntitiesAndUpdate(const MTPDmessage &data); void apply(const MTPDupdateNewScheduledMessage &update); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 753b3abfd..907bb0c10 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -2704,14 +2704,15 @@ MsgId History::msgIdForRead() const { : result; } -HistoryItem *History::lastSentMessage() const { +HistoryItem *History::lastEditableMessage() const { if (!loadedAtBottom()) { return nullptr; } + const auto now = base::unixtime::now(); for (const auto &block : ranges::view::reverse(blocks)) { for (const auto &message : ranges::view::reverse(block->messages)) { const auto item = message->data(); - if (item->canBeEditedFromHistory()) { + if (item->allowsEdit(now)) { return item; } } diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 9c9f92055..b715e7733 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -259,7 +259,7 @@ public: MsgId minMsgId() const; MsgId maxMsgId() const; MsgId msgIdForRead() const; - HistoryItem *lastSentMessage() const; + HistoryItem *lastEditableMessage() const; void resizeToWidth(int newWidth); void forceFullResize(); diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 517ed9a1c..12b4b2c3b 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -762,27 +762,6 @@ bool HistoryItem::canUpdateDate() const { return isScheduled(); } -bool HistoryItem::canBeEditedFromHistory() const { - // Skip if message is editing media. - if (isEditingMedia()) { - return false; - } - // Skip if message is video message or sticker. - if (const auto m = media()) { - // Skip only if media is not webpage. - if (!m->webpage() && !m->allowsEditCaption()) { - return false; - } - } - if ((IsServerMsgId(id) || isScheduled()) - && !serviceMsg() - && (out() || history()->peer->isSelf()) - && !Has()) { - return true; - } - return false; -} - void HistoryItem::sendFailed() { Expects(_clientFlags & MTPDmessage_ClientFlag::f_sending); Expects(!(_clientFlags & MTPDmessage_ClientFlag::f_failed)); diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 280afbed2..008cb72cd 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -390,8 +390,6 @@ public: void updateDate(TimeId newDate); [[nodiscard]] bool canUpdateDate() const; - [[nodiscard]] bool canBeEditedFromHistory() const; - virtual ~HistoryItem(); MsgId id; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 47f89ecd1..8772e78ac 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -5022,10 +5022,9 @@ void HistoryWidget::keyPressEvent(QKeyEvent *e) { _scroll->keyPressEvent(e); } else if (e->key() == Qt::Key_Up && !commonModifiers) { const auto item = _history - ? _history->lastSentMessage() + ? _history->lastEditableMessage() : nullptr; if (item - && item->allowsEdit(base::unixtime::now()) && _field->empty() && !_editMsgId && !_replyToId) { diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index 89ef5e3d0..d6df6439b 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -494,7 +494,7 @@ void RepliesWidget::setupComposeControls() { if (!_composeControls->isEditingMessage()) { // #TODO replies edit last sent message //auto &messages = session().data().scheduledMessages(); - //if (const auto item = messages.lastSentMessage(_history)) { + //if (const auto item = messages.lastEditableMessage(_history)) { // _inner->editMessageRequestNotify(item->fullId()); //} else { _scroll->keyPressEvent(e); diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index 9191bd357..9d72215a5 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -235,8 +235,9 @@ void ScheduledWidget::setupComposeControls() { ) | rpl::start_with_next([=](not_null e) { if (e->key() == Qt::Key_Up) { if (!_composeControls->isEditingMessage()) { - auto &messages = session().data().scheduledMessages(); - if (const auto item = messages.lastSentMessage(_history)) { + const auto item = session().data().scheduledMessages() + .lastEditableMessage(_history); + if (item) { _inner->editMessageRequestNotify(item->fullId()); } else { _scroll->keyPressEvent(e);