From e1fe37350470e6fb53a2bd62a434a8d9b6bc8aae Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 20 Aug 2019 12:42:13 +0300 Subject: [PATCH] Fix localtime-related slowmode errors. --- Telegram/SourceFiles/apiwrap.cpp | 8 ++++++-- Telegram/SourceFiles/data/data_channel.cpp | 16 ++++++++++------ Telegram/SourceFiles/data/data_peer.cpp | 9 +++++---- Telegram/SourceFiles/data/data_session.cpp | 6 +----- Telegram/SourceFiles/history/history.cpp | 6 +----- Telegram/SourceFiles/history/history_item.h | 2 ++ Telegram/SourceFiles/history/history_message.cpp | 8 ++++++++ Telegram/SourceFiles/history/history_message.h | 1 + Telegram/SourceFiles/mainwidget.cpp | 4 +--- 9 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 1e3acd64f..3a3fab77d 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -561,8 +561,12 @@ void ApiWrap::sendMessageFail( const auto left = error.type().mid(chop).toInt(); if (const auto channel = peer->asChannel()) { const auto seconds = channel->slowmodeSeconds(); - channel->growSlowmodeLastMessage( - base::unixtime::now() - (left - seconds)); + if (seconds >= left) { + channel->growSlowmodeLastMessage( + base::unixtime::now() - (left - seconds)); + } else { + requestFullPeer(peer); + } } } if (const auto item = _session->data().message(itemId)) { diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index fd67b1460..3f4e16b87 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -596,10 +596,15 @@ TimeId ChannelData::slowmodeLastMessage() const { } void ChannelData::growSlowmodeLastMessage(TimeId when) { - if (_slowmodeLastMessage >= when) { + const auto now = base::unixtime::now(); + accumulate_min(when, now); + if (_slowmodeLastMessage > now) { + _slowmodeLastMessage = when; + } else if (_slowmodeLastMessage >= when) { return; + } else { + _slowmodeLastMessage = when; } - _slowmodeLastMessage = when; Notify::peerUpdatedDelayed(this, UpdateFlag::ChannelSlowmode); } @@ -655,11 +660,10 @@ void ApplyChannelUpdate( channel->setKickedCount(update.vkicked_count().value_or_empty()); channel->setSlowmodeSeconds(update.vslowmode_seconds().value_or_empty()); if (const auto next = update.vslowmode_next_send_date()) { - if (next->v > base::unixtime::now()) { - channel->growSlowmodeLastMessage( - next->v - channel->slowmodeSeconds()); - } + channel->growSlowmodeLastMessage( + next->v - channel->slowmodeSeconds()); } + channel->checkSlowmodeLastMessage(); channel->setInviteLink(update.vexported_invite().match([&]( const MTPDchatInviteExported &data) { return qs(data.vlink()); diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index e19932fa4..3d09f6287 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -713,10 +713,11 @@ bool PeerData::slowmodeApplied() const { int PeerData::slowmodeSecondsLeft() const { if (const auto channel = asChannel()) { - if (const auto last = channel->slowmodeLastMessage()) { - const auto seconds = channel->slowmodeSeconds(); - const auto now = base::unixtime::now(); - return std::max(seconds - (now - last), 0); + if (const auto seconds = channel->slowmodeSeconds()) { + if (const auto last = channel->slowmodeLastMessage()) { + const auto now = base::unixtime::now(); + return std::max(seconds - (now - last), 0); + } } } return 0; diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 10f80df3b..6f0613f67 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -1637,11 +1637,7 @@ bool Session::checkEntitiesAndViewsUpdate(const MTPDmessage &data) { existing->updateForwardedInfo(data.vfwd_from()); existing->setViewsCount(data.vviews().value_or(-1)); existing->indexAsNewItem(); - if (const auto channel = existing->history()->peer->asChannel()) { - if (existing->out()) { - channel->growSlowmodeLastMessage(data.vdate().v); - } - } + existing->contributeToSlowmode(data.vdate().v); requestItemTextRefresh(existing); if (existing->mainView()) { checkSavedGif(existing); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 1c7f40aeb..05d3c0736 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1242,16 +1242,12 @@ void History::newItemAdded(not_null item) { } from->madeAction(item->date()); } + item->contributeToSlowmode(); if (item->out()) { destroyUnreadBar(); if (!item->unread()) { outboxRead(item); } - if (const auto channel = peer->asChannel()) { - if (IsServerMsgId(item->id)) { - channel->growSlowmodeLastMessage(item->date()); - } - } } else if (item->unread()) { if (!isChannel() || peer->asChannel()->amIn()) { _notifications.push_back(item); diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index b6ac486c0..b38623722 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -204,6 +204,8 @@ public: } virtual void updateForwardedInfo(const MTPMessageFwdHeader *fwd) { } + virtual void contributeToSlowmode(TimeId realDate = 0) { + } virtual void addToUnreadMentions(UnreadMentionType type); virtual void eraseFromUnreadMentions() { diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 86517e991..6b3865a6d 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -1098,6 +1098,14 @@ void HistoryMessage::updateForwardedInfo(const MTPMessageFwdHeader *fwd) { }); } +void HistoryMessage::contributeToSlowmode(TimeId realDate) { + if (const auto channel = history()->peer->asChannel()) { + if (out() && IsServerMsgId(id)) { + channel->growSlowmodeLastMessage(realDate ? realDate : date()); + } + } +} + void HistoryMessage::addToUnreadMentions(UnreadMentionType type) { if (IsServerMsgId(id) && isUnreadMention()) { if (history()->addToUnreadMentions(id, type)) { diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index edff53f79..ea2b91d58 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -137,6 +137,7 @@ public: setReplyMarkup(markup); } void updateForwardedInfo(const MTPMessageFwdHeader *fwd) override; + void contributeToSlowmode(TimeId realDate = 0) override; void addToUnreadMentions(UnreadMentionType type) override; void eraseFromUnreadMentions() override; diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index f0f42de7d..7264a3cf6 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -3821,9 +3821,7 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) { sent.text, TextUtilities::EntitiesFromMTP(list.value_or_empty()) }, d.vmedia()); - if (const auto channel = item->history()->peer->asChannel()) { - channel->growSlowmodeLastMessage(d.vdate().v); - } + item->contributeToSlowmode(d.vdate().v); if (!wasAlready) { item->indexAsNewItem(); }