From 97cdacff7b5e10dd77c102030fe3239288246bae Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 21 Nov 2019 02:41:53 +0300 Subject: [PATCH] Update slow_mode_next_send_date when a message is sent. GitOrigin-RevId: d373e4685a4c6b717272844b50da8fb3139b2f64 --- td/telegram/ContactsManager.cpp | 50 +++++++++++++++++++++++++-------- td/telegram/ContactsManager.h | 3 +- td/telegram/MessagesManager.cpp | 21 ++++++++++---- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 46ff70bdb..8c7896b52 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -8508,15 +8508,17 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c on_update_channel_full_location(channel, channel_id, DialogLocation(std::move(channel_full->location_))); - int32 slow_mode_delay = 0; - int32 slow_mode_next_send_date = 0; - if ((channel_full->flags_ & CHANNEL_FULL_FLAG_HAS_SLOW_MODE_DELAY) != 0) { - slow_mode_delay = channel_full->slowmode_seconds_; + if (c->is_megagroup) { + int32 slow_mode_delay = 0; + int32 slow_mode_next_send_date = 0; + if ((channel_full->flags_ & CHANNEL_FULL_FLAG_HAS_SLOW_MODE_DELAY) != 0) { + slow_mode_delay = channel_full->slowmode_seconds_; + } + if ((channel_full->flags_ & CHANNEL_FULL_FLAG_HAS_SLOW_MODE_NEXT_SEND_DATE) != 0) { + slow_mode_next_send_date = channel_full->slowmode_next_send_date_; + } + on_update_channel_full_slow_mode_delay(channel, channel_id, slow_mode_delay, slow_mode_next_send_date); } - if ((channel_full->flags_ & CHANNEL_FULL_FLAG_HAS_SLOW_MODE_NEXT_SEND_DATE) != 0) { - slow_mode_next_send_date = channel_full->slowmode_next_send_date_; - } - on_update_channel_full_slow_mode_delay(channel, channel_id, slow_mode_delay, slow_mode_next_send_date); ChatId migrated_from_chat_id; MessageId migrated_from_max_message_id; @@ -9744,6 +9746,11 @@ void ContactsManager::on_update_channel_full_location(ChannelFull *channel_full, void ContactsManager::on_update_channel_full_slow_mode_delay(ChannelFull *channel_full, ChannelId channel_id, int32 slow_mode_delay, int32 slow_mode_next_send_date) { + if (slow_mode_delay < 0) { + LOG(ERROR) << "Receive slow mode delay " << slow_mode_delay << " in " << channel_id; + slow_mode_delay = 0; + } + if (channel_full->slow_mode_delay != slow_mode_delay) { channel_full->slow_mode_delay = slow_mode_delay; channel_full->is_changed = true; @@ -9762,12 +9769,23 @@ void ContactsManager::on_update_channel_full_slow_mode_delay(ChannelFull *channe void ContactsManager::on_update_channel_full_slow_mode_next_send_date(ChannelFull *channel_full, int32 slow_mode_next_send_date) { + if (slow_mode_next_send_date < 0) { + LOG(ERROR) << "Receive slow mode next send date " << slow_mode_next_send_date; + slow_mode_next_send_date = 0; + } if (channel_full->slow_mode_delay == 0 && slow_mode_next_send_date > 0) { LOG(ERROR) << "Slow mode is disabled, but next send date is " << slow_mode_next_send_date; slow_mode_next_send_date = 0; } - if (slow_mode_next_send_date <= G()->unix_time()) { - slow_mode_next_send_date = 0; + + if (slow_mode_next_send_date != 0) { + auto now = G()->unix_time(); + if (slow_mode_next_send_date <= now) { + slow_mode_next_send_date = 0; + } + if (slow_mode_next_send_date > now + 3601) { + slow_mode_next_send_date = now + 3601; + } } if (channel_full->slow_mode_next_send_date != slow_mode_next_send_date) { channel_full->slow_mode_next_send_date = slow_mode_next_send_date; @@ -10566,10 +10584,10 @@ void ContactsManager::on_update_channel_slow_mode_delay(ChannelId channel_id, in } } -void ContactsManager::on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 next_send_date) { +void ContactsManager::on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 slow_mode_next_send_date) { auto channel_full = get_channel_full_force(channel_id); if (channel_full != nullptr) { - on_update_channel_full_slow_mode_next_send_date(channel_full, next_send_date); + on_update_channel_full_slow_mode_next_send_date(channel_full, slow_mode_next_send_date); update_channel_full(channel_full, channel_id); } } @@ -11337,6 +11355,14 @@ FileSourceId ContactsManager::get_channel_photo_file_source_id(ChannelId channel return source_id; } +int32 ContactsManager::get_channel_slow_mode_delay(ChannelId channel_id) { + auto channel_full = get_channel_full_force(channel_id); + if (channel_full == nullptr) { + return 0; + } + return channel_full->slow_mode_delay; +} + bool ContactsManager::have_channel(ChannelId channel_id) const { return channels_.count(channel_id) > 0; } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index c7f1db3d8..fcec18326 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -186,7 +186,7 @@ class ContactsManager : public Actor { void on_update_channel_linked_channel_id(ChannelId channel_id, ChannelId group_channel_id); void on_update_channel_location(ChannelId channel_id, const DialogLocation &location); void on_update_channel_slow_mode_delay(ChannelId channel_id, int32 slow_mode_delay); - void on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 next_send_date); + void on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 slow_mode_next_send_date); void on_update_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available); void on_update_channel_default_permissions(ChannelId channel_id, RestrictedRights default_permissions); void on_update_channel_administrator_count(ChannelId channel_id, int32 administrator_count); @@ -437,6 +437,7 @@ class ContactsManager : public Actor { int32 get_channel_participant_count(ChannelId channel_id) const; bool get_channel_sign_messages(ChannelId channel_id) const; FileSourceId get_channel_photo_file_source_id(ChannelId channel_id); + int32 get_channel_slow_mode_delay(ChannelId channel_id); std::pair> search_among_users(const vector &user_ids, const string &query, int32 limit); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 04aaaa81c..40130503c 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -24758,17 +24758,28 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } } } - if (*need_update && dialog_id.get_type() == DialogType::Channel && - message->date < G()->unix_time_cached() - 2 * 86400 && Slice(source) == Slice("updateNewChannelMessage")) { - // if the message is pretty old, we might have missed the update that the message has already been read - repair_channel_server_unread_count(d); - } const Message *m = message.get(); if (!m->from_database && !message_id.is_yet_unsent()) { add_message_to_database(d, m, "add_message_to_dialog"); } + if (*need_update && dialog_id.get_type() == DialogType::Channel) { + auto now = max(G()->unix_time_cached(), m->date); + if (m->date < now - 2 * 86400 && Slice(source) == Slice("updateNewChannelMessage")) { + // if the message is pretty old, we might have missed the update that the message has already been read + repair_channel_server_unread_count(d); + } + if (m->date + 3600 >= now && m->is_outgoing) { + auto channel_id = dialog_id.get_channel_id(); + auto slow_mode_delay = td_->contacts_manager_->get_channel_slow_mode_delay(channel_id); + auto status = td_->contacts_manager_->get_channel_status(dialog_id.get_channel_id()); + if (m->date + slow_mode_delay > now && !status.is_administrator()) { + td_->contacts_manager_->on_update_channel_slow_mode_next_send_date(channel_id, m->date + slow_mode_delay); + } + } + } + if (!is_attached && !m->have_next && !m->have_previous) { MessagesIterator it(d, message_id); if (*it != nullptr && (*it)->have_next) {