Fix localtime-related slowmode errors.

This commit is contained in:
John Preston 2019-08-20 12:42:13 +03:00
parent 1a06714f3a
commit e1fe373504
9 changed files with 35 additions and 25 deletions

View File

@ -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)) {

View File

@ -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());

View File

@ -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;

View File

@ -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);

View File

@ -1242,16 +1242,12 @@ void History::newItemAdded(not_null<HistoryItem*> 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);

View File

@ -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() {

View File

@ -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)) {

View File

@ -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;

View File

@ -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();
}