Update slow_mode_next_send_date when a message is sent.

GitOrigin-RevId: d373e4685a4c6b717272844b50da8fb3139b2f64
This commit is contained in:
levlam 2019-11-21 02:41:53 +03:00
parent 2415d28b4e
commit 97cdacff7b
3 changed files with 56 additions and 18 deletions

View File

@ -8508,15 +8508,17 @@ void ContactsManager::on_get_chat_full(tl_object_ptr<telegram_api::ChatFull> &&c
on_update_channel_full_location(channel, channel_id, DialogLocation(std::move(channel_full->location_))); on_update_channel_full_location(channel, channel_id, DialogLocation(std::move(channel_full->location_)));
int32 slow_mode_delay = 0; if (c->is_megagroup) {
int32 slow_mode_next_send_date = 0; int32 slow_mode_delay = 0;
if ((channel_full->flags_ & CHANNEL_FULL_FLAG_HAS_SLOW_MODE_DELAY) != 0) { int32 slow_mode_next_send_date = 0;
slow_mode_delay = channel_full->slowmode_seconds_; 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; ChatId migrated_from_chat_id;
MessageId migrated_from_max_message_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, 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) { 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) { if (channel_full->slow_mode_delay != slow_mode_delay) {
channel_full->slow_mode_delay = slow_mode_delay; channel_full->slow_mode_delay = slow_mode_delay;
channel_full->is_changed = true; 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, void ContactsManager::on_update_channel_full_slow_mode_next_send_date(ChannelFull *channel_full,
int32 slow_mode_next_send_date) { 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) { 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; LOG(ERROR) << "Slow mode is disabled, but next send date is " << slow_mode_next_send_date;
slow_mode_next_send_date = 0; 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) { 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; 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); auto channel_full = get_channel_full_force(channel_id);
if (channel_full != nullptr) { 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); update_channel_full(channel_full, channel_id);
} }
} }
@ -11337,6 +11355,14 @@ FileSourceId ContactsManager::get_channel_photo_file_source_id(ChannelId channel
return source_id; 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 { bool ContactsManager::have_channel(ChannelId channel_id) const {
return channels_.count(channel_id) > 0; return channels_.count(channel_id) > 0;
} }

View File

@ -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_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_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_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_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_default_permissions(ChannelId channel_id, RestrictedRights default_permissions);
void on_update_channel_administrator_count(ChannelId channel_id, int32 administrator_count); 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; int32 get_channel_participant_count(ChannelId channel_id) const;
bool get_channel_sign_messages(ChannelId channel_id) const; bool get_channel_sign_messages(ChannelId channel_id) const;
FileSourceId get_channel_photo_file_source_id(ChannelId channel_id); FileSourceId get_channel_photo_file_source_id(ChannelId channel_id);
int32 get_channel_slow_mode_delay(ChannelId channel_id);
std::pair<int32, vector<UserId>> search_among_users(const vector<UserId> &user_ids, const string &query, int32 limit); std::pair<int32, vector<UserId>> search_among_users(const vector<UserId> &user_ids, const string &query, int32 limit);

View File

@ -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(); const Message *m = message.get();
if (!m->from_database && !message_id.is_yet_unsent()) { if (!m->from_database && !message_id.is_yet_unsent()) {
add_message_to_database(d, m, "add_message_to_dialog"); 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) { if (!is_attached && !m->have_next && !m->have_previous) {
MessagesIterator it(d, message_id); MessagesIterator it(d, message_id);
if (*it != nullptr && (*it)->have_next) { if (*it != nullptr && (*it)->have_next) {