diff --git a/td/telegram/InlineQueriesManager.h b/td/telegram/InlineQueriesManager.h index f4e736361..5640074a7 100644 --- a/td/telegram/InlineQueriesManager.h +++ b/td/telegram/InlineQueriesManager.h @@ -21,7 +21,6 @@ #include "td/telegram/MessageEntity.h" #include "td/telegram/net/NetQuery.h" #include "td/telegram/Photo.h" -#include "td/telegram/ReplyMarkup.h" #include "td/telegram/UserId.h" #include "td/utils/common.h" diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 81764a1fd..0955b4ec1 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -4551,6 +4551,24 @@ bool need_reget_message_content(const MessageContent *content) { } } +bool need_delay_message_content_notification(const MessageContent *content, UserId my_user_id) { + switch (content->get_type()) { + case MessageContentType::ChatChangeTitle: + case MessageContentType::ChatChangePhoto: + case MessageContentType::ChatDeletePhoto: + case MessageContentType::ChatJoinedByLink: + return true; + case MessageContentType::ChatAddUsers: { + auto &added_user_ids = static_cast(content)->user_ids; + return std::find(added_user_ids.begin(), added_user_ids.end(), my_user_id) == added_user_ids.end(); + } + case MessageContentType::ChatDeleteUser: + return static_cast(content)->user_id != my_user_id; + default: + return false; + } +} + void update_expired_message_content(unique_ptr &content) { switch (content->get_type()) { case MessageContentType::Photo: diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 412f081a4..5eac3de75 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -220,6 +220,8 @@ string get_message_content_search_text(const Td *td, const MessageContent *conte bool need_reget_message_content(const MessageContent *content); +bool need_delay_message_content_notification(const MessageContent *content, UserId my_user_id); + void update_expired_message_content(unique_ptr &content); void add_message_content_dependencies(Dependencies &dependencies, const MessageContent *message_content); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index cccbefa50..c841822fa 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -18352,9 +18352,11 @@ bool MessagesManager::add_new_message_notification(Dialog *d, Message *m, bool f } VLOG(notifications) << "Create " << m->notification_id << " with " << m->message_id << " in " << group_info.group_id << '/' << d->dialog_id; + int32 min_delay_ms = + need_delay_message_content_notification(m->content.get(), td_->contacts_manager_->get_my_id()) ? 3000 : 0; send_closure_later(G()->notification_manager(), &NotificationManager::add_notification, notification_group_id, from_mentions ? NotificationGroupType::Mentions : NotificationGroupType::Messages, d->dialog_id, - m->date, settings_dialog_id, m->disable_notification, m->notification_id, + m->date, settings_dialog_id, m->disable_notification, min_delay_ms, m->notification_id, create_new_message_notification(m->message_id)); return true; } @@ -22107,7 +22109,7 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source VLOG(notifications) << "Create " << d->new_secret_chat_notification_id << " with " << secret_chat_id; send_closure_later(G()->notification_manager(), &NotificationManager::add_notification, notification_group_id, NotificationGroupType::SecretChat, dialog_id, date, dialog_id, - false, d->new_secret_chat_notification_id, create_new_secret_chat_notification()); + false, 0, d->new_secret_chat_notification_id, create_new_secret_chat_notification()); } } } diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 4ce4e82ff..261af61fa 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -578,16 +578,16 @@ NotificationGroupKey NotificationManager::get_last_updated_group_key() const { return it->first; } -int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, - const PendingNotification ¬ification) const { - auto delay_ms = [&]() { - if (dialog_id.get_type() == DialogType::SecretChat) { - return 0; // there is no reason to delay notifications in secret chats - } - if (!notification.type->can_be_delayed()) { - return 0; - } +int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, const PendingNotification ¬ification, + int32 min_delay_ms) const { + if (dialog_id.get_type() == DialogType::SecretChat) { + return MIN_NOTIFICATION_DELAY_MS; // there is no reason to delay notifications in secret chats + } + if (!notification.type->can_be_delayed()) { + return MIN_NOTIFICATION_DELAY_MS; + } + auto delay_ms = [&]() { auto online_info = td_->contacts_manager_->get_my_online_status(); if (!online_info.is_online_local && online_info.is_online_remote) { // If we are offline, but online from some other client then delay notification @@ -613,12 +613,12 @@ int32 NotificationManager::get_notification_delay_ms(DialogId dialog_id, }(); auto passed_time_ms = max(0, static_cast((G()->server_time_cached() - notification.date - 1) * 1000)); - return max(delay_ms - passed_time_ms, MIN_NOTIFICATION_DELAY_MS); + return max(max(min_delay_ms, delay_ms) - passed_time_ms, MIN_NOTIFICATION_DELAY_MS); } void NotificationManager::add_notification(NotificationGroupId group_id, NotificationGroupType group_type, DialogId dialog_id, int32 date, DialogId notification_settings_dialog_id, - bool is_silent, NotificationId notification_id, + bool is_silent, int32 min_delay_ms, NotificationId notification_id, unique_ptr type) { if (is_disabled() || max_notification_group_count_ == 0) { return; @@ -649,7 +649,7 @@ void NotificationManager::add_notification(NotificationGroupId group_id, Notific notification.notification_id = notification_id; notification.type = std::move(type); - auto delay_ms = get_notification_delay_ms(dialog_id, notification); + auto delay_ms = get_notification_delay_ms(dialog_id, notification, min_delay_ms); VLOG(notifications) << "Delay " << notification_id << " for " << delay_ms << " milliseconds"; auto flush_time = delay_ms * 0.001 + Time::now(); @@ -1769,7 +1769,7 @@ void NotificationManager::add_call_notification(DialogId dialog_id, CallId call_ } active_notifications.push_back(ActiveCallNotification{call_id, notification_id}); - add_notification(group_id, NotificationGroupType::Calls, dialog_id, G()->unix_time() + 120, dialog_id, false, + add_notification(group_id, NotificationGroupType::Calls, dialog_id, G()->unix_time() + 120, dialog_id, false, 0, notification_id, create_new_call_notification(call_id)); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 3d4cd8db9..3b90e810a 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -59,8 +59,8 @@ class NotificationManager : public Actor { void load_group_force(NotificationGroupId group_id); void add_notification(NotificationGroupId group_id, NotificationGroupType group_type, DialogId dialog_id, int32 date, - DialogId notification_settings_dialog_id, bool is_silent, NotificationId notification_id, - unique_ptr type); + DialogId notification_settings_dialog_id, bool is_silent, int32 min_delay_ms, + NotificationId notification_id, unique_ptr type); void edit_notification(NotificationGroupId group_id, NotificationId notification_id, unique_ptr type); @@ -195,7 +195,8 @@ class NotificationManager : public Actor { void send_add_group_update(const NotificationGroupKey &group_key, const NotificationGroup &group); - int32 get_notification_delay_ms(DialogId dialog_id, const PendingNotification ¬ification) const; + int32 get_notification_delay_ms(DialogId dialog_id, const PendingNotification ¬ification, + int32 min_delay_ms) const; void do_flush_pending_notifications(NotificationGroupKey &group_key, NotificationGroup &group, vector &pending_notifications);