Implement remove_message_notifications.

GitOrigin-RevId: bd85166d4fef4d7912300ffee1aa2d6ff2dc0717
This commit is contained in:
levlam 2018-11-30 00:02:33 +03:00
parent 31873111c7
commit a7e8bd166c
4 changed files with 79 additions and 19 deletions

View File

@ -3803,6 +3803,7 @@ void MessagesManager::Dialog::store(StorerT &storer) const {
bool has_message_notification_group_id = message_notification_group_id.is_valid(); bool has_message_notification_group_id = message_notification_group_id.is_valid();
bool has_last_notification_date = last_notification_date > 0; bool has_last_notification_date = last_notification_date > 0;
bool has_last_notification_id = last_notification_id.is_valid(); bool has_last_notification_id = last_notification_id.is_valid();
bool has_max_removed_notification_id = max_removed_notification_id.is_valid();
BEGIN_STORE_FLAGS(); BEGIN_STORE_FLAGS();
STORE_FLAG(has_draft_message); STORE_FLAG(has_draft_message);
STORE_FLAG(has_last_database_message); STORE_FLAG(has_last_database_message);
@ -3829,7 +3830,11 @@ void MessagesManager::Dialog::store(StorerT &storer) const {
STORE_FLAG(is_marked_as_unread); STORE_FLAG(is_marked_as_unread);
STORE_FLAG(has_message_notification_group_id); STORE_FLAG(has_message_notification_group_id);
STORE_FLAG(has_last_notification_date); STORE_FLAG(has_last_notification_date);
STORE_FLAG(has_last_notification_id); // 25 STORE_FLAG(has_last_notification_id);
STORE_FLAG(has_max_removed_notification_id); // 26
//
//
//STORE_FLAG(has_flags2);
END_STORE_FLAGS(); END_STORE_FLAGS();
store(dialog_id, storer); // must be stored at offset 4 store(dialog_id, storer); // must be stored at offset 4
@ -3897,6 +3902,9 @@ void MessagesManager::Dialog::store(StorerT &storer) const {
if (has_last_notification_id) { if (has_last_notification_id) {
store(last_notification_id, storer); store(last_notification_id, storer);
} }
if (has_max_removed_notification_id) {
store(max_removed_notification_id, storer);
}
} }
// do not forget to resolve dialog dependencies including dependencies of last_message // do not forget to resolve dialog dependencies including dependencies of last_message
@ -3919,6 +3927,7 @@ void MessagesManager::Dialog::parse(ParserT &parser) {
bool has_message_notification_group_id; bool has_message_notification_group_id;
bool has_last_notification_date; bool has_last_notification_date;
bool has_last_notification_id; bool has_last_notification_id;
bool has_max_removed_notification_id;
BEGIN_PARSE_FLAGS(); BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_draft_message); PARSE_FLAG(has_draft_message);
PARSE_FLAG(has_last_database_message); PARSE_FLAG(has_last_database_message);
@ -3946,6 +3955,7 @@ void MessagesManager::Dialog::parse(ParserT &parser) {
PARSE_FLAG(has_message_notification_group_id); PARSE_FLAG(has_message_notification_group_id);
PARSE_FLAG(has_last_notification_date); PARSE_FLAG(has_last_notification_date);
PARSE_FLAG(has_last_notification_id); PARSE_FLAG(has_last_notification_id);
PARSE_FLAG(has_max_removed_notification_id);
END_PARSE_FLAGS(); END_PARSE_FLAGS();
parse(dialog_id, parser); // must be stored at offset 4 parse(dialog_id, parser); // must be stored at offset 4
@ -4037,6 +4047,9 @@ void MessagesManager::Dialog::parse(ParserT &parser) {
if (has_last_notification_id) { if (has_last_notification_id) {
parse(last_notification_id, parser); parse(last_notification_id, parser);
} }
if (has_max_removed_notification_id) {
parse(max_removed_notification_id, parser);
}
} }
template <class StorerT> template <class StorerT>
@ -7759,6 +7772,7 @@ void MessagesManager::delete_all_dialog_messages(Dialog *d, bool remove_from_dia
set_dialog_last_database_message_id(d, MessageId(), "delete_all_dialog_messages"); set_dialog_last_database_message_id(d, MessageId(), "delete_all_dialog_messages");
set_dialog_last_clear_history_date(d, last_message_date, last_clear_history_message_id, "delete_all_dialog_messages"); set_dialog_last_clear_history_date(d, last_message_date, last_clear_history_message_id, "delete_all_dialog_messages");
d->last_read_all_mentions_message_id = MessageId(); // it is not needed anymore d->last_read_all_mentions_message_id = MessageId(); // it is not needed anymore
d->max_removed_notification_id = NotificationId(); // it is not needed anymore
std::fill(d->message_count_by_index.begin(), d->message_count_by_index.end(), 0); std::fill(d->message_count_by_index.begin(), d->message_count_by_index.end(), 0);
d->notification_id_to_message_id.clear(); d->notification_id_to_message_id.clear();
@ -8262,9 +8276,13 @@ void MessagesManager::set_dialog_last_read_inbox_message_id(Dialog *d, MessageId
if (message_id != MessageId::min()) { if (message_id != MessageId::min()) {
if (d->last_read_inbox_message_id.is_valid() && d->message_notification_group_id.is_valid() && if (d->last_read_inbox_message_id.is_valid() && d->message_notification_group_id.is_valid() &&
d->order != DEFAULT_ORDER && d->order != SPONSORED_DIALOG_ORDER) { d->order != DEFAULT_ORDER && d->order != SPONSORED_DIALOG_ORDER) {
auto total_count = get_dialog_pending_notification_count(d);
if (total_count == 0) {
set_dialog_last_notification(d, 0, NotificationId(), "set_dialog_last_read_inbox_message_id");
}
send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group,
d->message_notification_group_id, NotificationId(), d->last_read_inbox_message_id, d->message_notification_group_id, NotificationId(), d->last_read_inbox_message_id, total_count,
get_dialog_pending_notification_count(d), Promise<Unit>()); Promise<Unit>());
} }
} }
@ -17609,6 +17627,7 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat
bool MessagesManager::is_message_has_active_notification(const Dialog *d, const Message *m) { bool MessagesManager::is_message_has_active_notification(const Dialog *d, const Message *m) {
return m != nullptr && m->notification_id.is_valid() && return m != nullptr && m->notification_id.is_valid() &&
m->notification_id.get() > d->max_removed_notification_id.get() &&
(m->message_id.get() > d->last_read_inbox_message_id.get() || m->contains_unread_mention); (m->message_id.get() > d->last_read_inbox_message_id.get() || m->contains_unread_mention);
} }
@ -17725,6 +17744,25 @@ void MessagesManager::do_remove_message_notification(DialogId dialog_id, Notific
} }
} }
void MessagesManager::remove_message_notifications(DialogId dialog_id, NotificationId max_notification_id) {
Dialog *d = get_dialog_force(dialog_id);
if (d == nullptr) {
LOG(ERROR) << "Can't find " << dialog_id;
return;
}
if (!d->message_notification_group_id.is_valid()) {
LOG(ERROR) << "There is no message notiication group in " << dialog_id;
return;
}
if (d->max_removed_notification_id.get() >= max_notification_id.get()) {
return;
}
VLOG(notifications) << "Set max_removed_notification_id in " << dialog_id << " to " << max_notification_id;
d->max_removed_notification_id = max_notification_id;
on_dialog_updated(dialog_id, "remove_message_notifications");
}
int32 MessagesManager::get_dialog_pending_notification_count(Dialog *d) { int32 MessagesManager::get_dialog_pending_notification_count(Dialog *d) {
CHECK(d != nullptr); CHECK(d != nullptr);
if (is_dialog_muted(d)) { if (is_dialog_muted(d)) {
@ -17859,11 +17897,15 @@ void MessagesManager::flush_pending_new_message_notifications(DialogId dialog_id
} }
} }
void MessagesManager::remove_dialog_message_notifications(const Dialog *d) const { void MessagesManager::remove_dialog_message_notifications(Dialog *d) {
if (d->message_notification_group_id.is_valid()) { if (d->message_notification_group_id.is_valid() && d->last_notification_id.is_valid() &&
d->max_removed_notification_id != d->last_notification_id) {
VLOG(notifications) << "Set max_removed_notification_id in " << d->dialog_id << " to " << d->last_notification_id;
d->max_removed_notification_id = d->last_notification_id;
send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group,
d->message_notification_group_id, td_->notification_manager_->get_max_notification_id(), d->message_notification_group_id, d->last_notification_id, MessageId(), 0, Promise<Unit>());
MessageId(), 0, Promise<Unit>()); bool is_changed = set_dialog_last_notification(d, 0, NotificationId(), "remove_dialog_message_notifications");
CHECK(is_changed);
} }
} }
@ -20873,12 +20915,21 @@ void MessagesManager::add_message_to_database(const Dialog *d, const Message *m,
Auto()); // TODO Promise Auto()); // TODO Promise
} }
void MessagesManager::delete_all_dialog_messages_from_database(const Dialog *d, MessageId message_id, void MessagesManager::delete_all_dialog_messages_from_database(Dialog *d, MessageId message_id, const char *source) {
const char *source) {
CHECK(d != nullptr); CHECK(d != nullptr);
if (d->message_notification_group_id.is_valid()) { if (d->message_notification_group_id.is_valid()) {
auto max_notification_message_id = message_id;
if (d->last_message_id.is_valid() && max_notification_message_id.get() > d->last_message_id.get()) {
max_notification_message_id = d->last_message_id;
set_dialog_last_notification(d, 0, NotificationId(), "delete_all_dialog_messages_from_database 1");
}
if (max_notification_message_id == MessageId::max()) {
max_notification_message_id = get_next_local_message_id(d);
set_dialog_last_notification(d, 0, NotificationId(), "delete_all_dialog_messages_from_database 2");
}
send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group, send_closure_later(G()->notification_manager(), &NotificationManager::remove_notification_group,
d->message_notification_group_id, NotificationId(), message_id, 0, Promise<Unit>()); d->message_notification_group_id, NotificationId(), max_notification_message_id, 0,
Promise<Unit>());
} }
if (!G()->parameters().use_message_db) { if (!G()->parameters().use_message_db) {

View File

@ -670,6 +670,8 @@ class MessagesManager : public Actor {
void remove_message_notification(DialogId dialog_id, NotificationId notification_id); void remove_message_notification(DialogId dialog_id, NotificationId notification_id);
void remove_message_notifications(DialogId dialog_id, NotificationId max_notification_id);
void on_binlog_events(vector<BinlogEvent> &&events); void on_binlog_events(vector<BinlogEvent> &&events);
void get_payment_form(FullMessageId full_message_id, Promise<tl_object_ptr<td_api::paymentForm>> &&promise); void get_payment_form(FullMessageId full_message_id, Promise<tl_object_ptr<td_api::paymentForm>> &&promise);
@ -883,6 +885,7 @@ class MessagesManager : public Actor {
NotificationGroupId message_notification_group_id; NotificationGroupId message_notification_group_id;
int32 last_notification_date = 0; int32 last_notification_date = 0;
NotificationId last_notification_id; NotificationId last_notification_id;
NotificationId max_removed_notification_id;
bool has_contact_registered_message = false; bool has_contact_registered_message = false;
@ -1443,7 +1446,7 @@ class MessagesManager : public Actor {
void add_message_to_database(const Dialog *d, const Message *m, const char *source); void add_message_to_database(const Dialog *d, const Message *m, const char *source);
void delete_all_dialog_messages_from_database(const Dialog *d, MessageId message_id, const char *source); void delete_all_dialog_messages_from_database(Dialog *d, MessageId message_id, const char *source);
void delete_message_from_database(Dialog *d, MessageId message_id, const Message *m, bool is_permanently_deleted); void delete_message_from_database(Dialog *d, MessageId message_id, const Message *m, bool is_permanently_deleted);
@ -1497,7 +1500,7 @@ class MessagesManager : public Actor {
void flush_pending_new_message_notifications(DialogId dialog_id, DialogId settings_dialog_id); void flush_pending_new_message_notifications(DialogId dialog_id, DialogId settings_dialog_id);
void remove_dialog_message_notifications(const Dialog *d) const; void remove_dialog_message_notifications(Dialog *d);
void send_update_message_send_succeeded(Dialog *d, MessageId old_message_id, const Message *m) const; void send_update_message_send_succeeded(Dialog *d, MessageId old_message_id, const Message *m) const;

View File

@ -33,7 +33,7 @@ inline td_api::object_ptr<td_api::notification> get_notification_object(DialogId
notification.type->get_notification_type_object(dialog_id)); notification.type->get_notification_type_object(dialog_id));
} }
inline StringBuilder &operator<<(StringBuilder &sb, const Notification notification) { inline StringBuilder &operator<<(StringBuilder &sb, const Notification &notification) {
return sb << "notification[" << notification.notification_id << ", " << notification.date << ", " return sb << "notification[" << notification.notification_id << ", " << notification.date << ", "
<< *notification.type << ']'; << *notification.type << ']';
} }

View File

@ -133,6 +133,9 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group
group.total_count = message_group.total_count; group.total_count = message_group.total_count;
group.notifications = std::move(message_group.notifications); group.notifications = std::move(message_group.notifications);
VLOG(notifications) << "Finish to load " << group_id << " with total_count " << message_group.total_count
<< " and notifications " << group.notifications;
// TODO send update about the new group, if needed // TODO send update about the new group, if needed
return groups_.emplace(std::move(group_key), std::move(group)).first; return groups_.emplace(std::move(group_key), std::move(group)).first;
@ -679,7 +682,7 @@ void NotificationManager::do_flush_pending_notifications(NotificationGroupKey &g
return; return;
} }
VLOG(notifications) << "Flush " << pending_notifications.size() << " pending notifications in " << group_key VLOG(notifications) << "Do flush " << pending_notifications.size() << " pending notifications in " << group_key
<< " with known " << group.notifications.size() << " from total of " << group.total_count << " with known " << group.notifications.size() << " from total of " << group.total_count
<< " notifications"; << " notifications";
@ -1102,16 +1105,19 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id
VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id << " or " << max_message_id VLOG(notifications) << "Remove " << group_id << " up to " << max_notification_id << " or " << max_message_id
<< " with new_total_count = " << new_total_count; << " with new_total_count = " << new_total_count;
if (max_notification_id.is_valid()) {
// TODO remove notifications from database by max_notification_id, save that they are removed
}
auto group_it = get_group_force(group_id); auto group_it = get_group_force(group_id);
if (group_it == groups_.end()) { if (group_it == groups_.end()) {
VLOG(notifications) << "Can't find " << group_id; VLOG(notifications) << "Can't find " << group_id;
return promise.set_value(Unit()); return promise.set_value(Unit());
} }
if (max_notification_id.is_valid()) {
if (max_notification_id.get() > current_notification_id_.get()) {
max_notification_id = current_notification_id_;
}
td_->messages_manager_->remove_message_notifications(group_it->first.dialog_id, max_notification_id);
}
auto pending_delete_end = group_it->second.pending_notifications.begin(); auto pending_delete_end = group_it->second.pending_notifications.begin();
for (auto it = group_it->second.pending_notifications.begin(); it != group_it->second.pending_notifications.end(); for (auto it = group_it->second.pending_notifications.begin(); it != group_it->second.pending_notifications.end();
++it) { ++it) {
@ -1374,7 +1380,7 @@ void NotificationManager::after_get_chat_difference_impl(NotificationGroupId gro
return; return;
} }
VLOG(notifications) << "After get chat difference in " << group_id; VLOG(notifications) << "Flush updates after get chat difference in " << group_id;
CHECK(group_id.is_valid()); CHECK(group_id.is_valid());
if (!running_get_difference_ && pending_updates_.count(group_id.get()) == 1) { if (!running_get_difference_ && pending_updates_.count(group_id.get()) == 1) {
flush_pending_updates_timeout_.cancel_timeout(group_id.get()); flush_pending_updates_timeout_.cancel_timeout(group_id.get());