Implement separate mention notification group.

GitOrigin-RevId: 036eed8c87e5f60da0c24cd02df11cd48f977019
This commit is contained in:
levlam 2018-12-22 23:24:18 +03:00
parent 9dbe8ab9d0
commit 36ab6b3cca
5 changed files with 586 additions and 420 deletions

View File

@ -125,8 +125,8 @@ class DialogDbImpl : public DialogDbSyncInterface {
return Status::OK();
}
Status add_dialog_new(DialogId dialog_id, int64 order, BufferSlice data,
std::vector<NotificationGroupKey> notification_groups) override {
Status add_dialog(DialogId dialog_id, int64 order, BufferSlice data,
vector<NotificationGroupKey> notification_groups) override {
SCOPE_EXIT {
add_dialog_stmt_.reset();
};
@ -178,7 +178,7 @@ class DialogDbImpl : public DialogDbSyncInterface {
get_notification_group_stmt_.view_int32(1));
}
Result<std::vector<BufferSlice>> get_dialogs(int64 order, DialogId dialog_id, int32 limit) override {
Result<vector<BufferSlice>> get_dialogs(int64 order, DialogId dialog_id, int32 limit) override {
SCOPE_EXIT {
get_dialogs_stmt_.reset();
};
@ -187,7 +187,7 @@ class DialogDbImpl : public DialogDbSyncInterface {
get_dialogs_stmt_.bind_int64(2, dialog_id.get()).ensure();
get_dialogs_stmt_.bind_int32(3, limit).ensure();
std::vector<BufferSlice> dialogs;
vector<BufferSlice> dialogs;
TRY_STATUS(get_dialogs_stmt_.step());
while (get_dialogs_stmt_.has_row()) {
BufferSlice data(get_dialogs_stmt_.view_blob(0));
@ -200,7 +200,7 @@ class DialogDbImpl : public DialogDbSyncInterface {
return std::move(dialogs);
}
Result<std::vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
NotificationGroupKey notification_group_key, int32 limit) override {
auto &stmt = get_notification_groups_by_last_notification_date_stmt_;
SCOPE_EXIT {
@ -212,7 +212,7 @@ class DialogDbImpl : public DialogDbSyncInterface {
stmt.bind_int32(3, notification_group_key.group_id.get()).ensure();
stmt.bind_int32(4, limit).ensure();
std::vector<NotificationGroupKey> notification_groups;
vector<NotificationGroupKey> notification_groups;
TRY_STATUS(stmt.step());
while (stmt.has_row()) {
notification_groups.emplace_back(NotificationGroupId(stmt.view_int32(0)), DialogId(stmt.view_int64(1)),
@ -265,14 +265,14 @@ class DialogDbAsync : public DialogDbAsyncInterface {
impl_ = create_actor_on_scheduler<Impl>("DialogDbActor", scheduler_id, std::move(sync_db));
}
void add_dialog_new(DialogId dialog_id, int64 order, BufferSlice data,
std::vector<NotificationGroupKey> notification_groups, Promise<> promise) override {
send_closure(impl_, &Impl::add_dialog_new, dialog_id, order, std::move(data), std::move(notification_groups),
void add_dialog(DialogId dialog_id, int64 order, BufferSlice data, vector<NotificationGroupKey> notification_groups,
Promise<> promise) override {
send_closure(impl_, &Impl::add_dialog, dialog_id, order, std::move(data), std::move(notification_groups),
std::move(promise));
}
void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit,
Promise<std::vector<NotificationGroupKey>> promise) override {
Promise<vector<NotificationGroupKey>> promise) override {
send_closure(impl_, &Impl::get_notification_groups_by_last_notification_date, notification_group_key, limit,
std::move(promise));
}
@ -285,7 +285,7 @@ class DialogDbAsync : public DialogDbAsyncInterface {
void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) override {
send_closure_later(impl_, &Impl::get_dialog, dialog_id, std::move(promise));
}
void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<std::vector<BufferSlice>> promise) override {
void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<vector<BufferSlice>> promise) override {
send_closure_later(impl_, &Impl::get_dialogs, order, dialog_id, limit, std::move(promise));
}
void close(Promise<> promise) override {
@ -297,16 +297,16 @@ class DialogDbAsync : public DialogDbAsyncInterface {
public:
explicit Impl(std::shared_ptr<DialogDbSyncSafeInterface> sync_db_safe) : sync_db_safe_(std::move(sync_db_safe)) {
}
void add_dialog_new(DialogId dialog_id, int64 order, BufferSlice data,
std::vector<NotificationGroupKey> notification_groups, Promise<> promise) {
void add_dialog(DialogId dialog_id, int64 order, BufferSlice data, vector<NotificationGroupKey> notification_groups,
Promise<> promise) {
add_write_query([=, promise = std::move(promise), data = std::move(data),
notification_groups = std::move(notification_groups)](Unit) mutable {
promise.set_result(sync_db_->add_dialog_new(dialog_id, order, std::move(data), std::move(notification_groups)));
promise.set_result(sync_db_->add_dialog(dialog_id, order, std::move(data), std::move(notification_groups)));
});
}
void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit,
Promise<std::vector<NotificationGroupKey>> promise) {
Promise<vector<NotificationGroupKey>> promise) {
add_read_query();
promise.set_result(sync_db_->get_notification_groups_by_last_notification_date(notification_group_key, limit));
}
@ -319,7 +319,7 @@ class DialogDbAsync : public DialogDbAsyncInterface {
add_read_query();
promise.set_result(sync_db_->get_dialog(dialog_id));
}
void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<std::vector<BufferSlice>> promise) {
void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<vector<BufferSlice>> promise) {
add_read_query();
promise.set_result(sync_db_->get_dialogs(order, dialog_id, limit));
}
@ -337,7 +337,7 @@ class DialogDbAsync : public DialogDbAsyncInterface {
static constexpr size_t MAX_PENDING_QUERIES_COUNT{50};
static constexpr double MAX_PENDING_QUERIES_DELAY{1};
std::vector<Promise<>> pending_writes_;
vector<Promise<>> pending_writes_;
double wakeup_at_ = 0;
template <class F>
void add_write_query(F &&f) {

View File

@ -29,35 +29,20 @@ class DialogDbSyncInterface {
DialogDbSyncInterface &operator=(const DialogDbSyncInterface &) = delete;
virtual ~DialogDbSyncInterface() = default;
virtual Status add_dialog_new(DialogId dialog_id, int64 order, BufferSlice data,
std::vector<NotificationGroupKey> notification_groups) = 0;
virtual Status add_dialog(DialogId dialog_id, int64 order, BufferSlice data,
vector<NotificationGroupKey> notification_groups) = 0;
virtual Result<BufferSlice> get_dialog(DialogId dialog_id) = 0;
virtual Result<std::vector<BufferSlice>> get_dialogs(int64 order, DialogId dialog_id, int32 limit) = 0;
virtual Result<vector<BufferSlice>> get_dialogs(int64 order, DialogId dialog_id, int32 limit) = 0;
virtual Result<std::vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
virtual Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
NotificationGroupKey notification_group_key, int32 limit) = 0;
virtual Result<NotificationGroupKey> get_notification_group(NotificationGroupId notification_group_id) = 0;
virtual Status begin_transaction() = 0;
virtual Status commit_transaction() = 0;
virtual Status add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date,
NotificationGroupId notification_group_id, BufferSlice data) {
LOG(FATAL) << "method is removed";
return Status::Error();
}
virtual Result<std::vector<BufferSlice>> get_dialogs_by_last_notification_date(int32 last_notification_date,
DialogId dialog_id, int32 limit) {
LOG(FATAL) << "method is removed";
return Status::Error();
}
virtual Result<BufferSlice> get_dialog_by_notification_group_id(NotificationGroupId notification_group_id) {
LOG(FATAL) << "method is removed";
return Status::Error();
}
};
class DialogDbSyncSafeInterface {
@ -77,33 +62,21 @@ class DialogDbAsyncInterface {
DialogDbAsyncInterface &operator=(const DialogDbAsyncInterface &) = delete;
virtual ~DialogDbAsyncInterface() = default;
virtual void add_dialog_new(DialogId dialog_id, int64 order, BufferSlice data,
std::vector<NotificationGroupKey> notification_groups, Promise<> promise) = 0;
virtual void add_dialog(DialogId dialog_id, int64 order, BufferSlice data,
vector<NotificationGroupKey> notification_groups, Promise<> promise) = 0;
virtual void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) = 0;
virtual void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<std::vector<BufferSlice>> promise) = 0;
virtual void get_dialogs(int64 order, DialogId dialog_id, int32 limit, Promise<vector<BufferSlice>> promise) = 0;
virtual void get_notification_groups_by_last_notification_date(
NotificationGroupKey notification_group_key, int32 limit, Promise<std::vector<NotificationGroupKey>> promise) = 0;
virtual void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key,
int32 limit,
Promise<vector<NotificationGroupKey>> promise) = 0;
virtual void get_notification_group(NotificationGroupId notification_group_id,
Promise<NotificationGroupKey> promise) = 0;
virtual void close(Promise<> promise) = 0;
virtual void add_dialog(DialogId dialog_id, int64 order, int32 last_notification_date,
NotificationGroupId notification_group_id, BufferSlice data, Promise<> promise) {
LOG(FATAL) << "method removed";
}
virtual void get_dialogs_by_last_notification_date(int32 last_notification_date, DialogId dialog_id, int32 limit,
Promise<std::vector<BufferSlice>> promise) {
LOG(FATAL) << "method removed";
}
virtual void get_dialog_by_notification_group_id(NotificationGroupId notification_group_id,
Promise<BufferSlice> promise) {
LOG(FATAL) << "method removed";
}
};
Status init_dialog_db(SqliteDb &db, int version, bool &was_created) TD_WARN_UNUSED_RESULT;

File diff suppressed because it is too large Load Diff

View File

@ -660,16 +660,17 @@ class MessagesManager : public Actor {
};
MessageNotificationGroup get_message_notification_group_force(NotificationGroupId group_id);
vector<NotificationGroupKey> get_message_notification_group_keys_from_database(int32 from_last_notification_date,
DialogId from_dialog_id, int32 limit);
vector<NotificationGroupKey> get_message_notification_group_keys_from_database(NotificationGroupKey from_group_key,
int32 limit);
void get_message_notifications_from_database(DialogId dialog_id, NotificationId from_notification_id,
MessageId from_message_id, int32 limit,
Promise<vector<Notification>> promise);
void get_message_notifications_from_database(DialogId dialog_id, NotificationGroupId group_id,
NotificationId from_notification_id, MessageId from_message_id,
int32 limit, Promise<vector<Notification>> promise);
void remove_message_notification(DialogId dialog_id, NotificationId notification_id);
void remove_message_notification(DialogId dialog_id, NotificationGroupId group_id, NotificationId notification_id);
void remove_message_notifications(DialogId dialog_id, NotificationId max_notification_id);
void remove_message_notifications(DialogId dialog_id, NotificationGroupId group_id,
NotificationId max_notification_id);
void on_binlog_events(vector<BinlogEvent> &&events);
@ -835,6 +836,21 @@ class MessagesManager : public Actor {
void parse(ParserT &parser);
};
struct NotificationGroupInfo {
NotificationGroupId group_id;
int32 last_notification_date = 0; // date of last notification in the group
NotificationId last_notification_id; // identifier of last notification in the group
NotificationId max_removed_notification_id; // notification identifier, up to which all notifications are removed
bool is_changed = false; // true, if the group needs to be saved to database
bool try_reuse = false; // true, if the group needs to be deleted from database and tried to be reused
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
struct Dialog {
DialogId dialog_id;
MessageId last_new_message_id; // identifier of the last known server message received from update, there should be
@ -883,10 +899,8 @@ class MessagesManager : public Actor {
MessageId max_added_message_id;
NotificationGroupId message_notification_group_id;
int32 last_notification_date = 0; // last known date of last notification in the dialog
NotificationId last_notification_id; // last known identifier of last notification in the dialog
NotificationId max_removed_notification_id; // notification identifier, up to which all notifications are removed
NotificationGroupInfo message_notification_group;
NotificationGroupInfo mention_notification_group;
NotificationId new_secret_chat_notification_id; // secret chats only
bool has_contact_registered_message = false;
@ -926,6 +940,7 @@ class MessagesManager : public Actor {
std::unordered_set<MessageId, MessageIdHash> deleted_message_ids;
std::vector<std::pair<DialogId, MessageId>> pending_new_message_notifications;
std::vector<std::pair<DialogId, MessageId>> pending_new_mention_notifications;
std::unordered_map<NotificationId, MessageId, NotificationIdHash> notification_id_to_message_id;
@ -1426,6 +1441,8 @@ class MessagesManager : public Actor {
void on_save_dialog_to_database(DialogId dialog_id, bool success);
void try_reuse_notification_group(NotificationGroupInfo &group_info);
void load_dialog_list(int32 limit, Promise<Unit> &&promise);
void load_dialog_list_from_database(int32 limit, Promise<Unit> &&promise);
@ -1448,7 +1465,7 @@ class MessagesManager : public Actor {
void add_message_to_database(const Dialog *d, const Message *m, const char *source);
void delete_all_dialog_messages_from_database(Dialog *d, MessageId message_id, const char *source);
void delete_all_dialog_messages_from_database(Dialog *d, MessageId max_message_id, const char *source);
void delete_message_from_database(Dialog *d, MessageId message_id, const Message *m, bool is_permanently_deleted);
@ -1468,10 +1485,11 @@ class MessagesManager : public Actor {
void remove_new_secret_chat_notification(Dialog *d, bool is_permanent);
void fix_dialog_last_notification_id(Dialog *d, MessageId message_id);
void fix_dialog_last_notification_id(Dialog *d, bool from_mentions, MessageId message_id);
void do_fix_dialog_last_notification_id(DialogId dialog_id, NotificationId prev_last_notification_id,
Result<vector<BufferSlice>> result);
void do_fix_dialog_last_notification_id(DialogId dialog_id, bool from_mentions,
NotificationId prev_last_notification_id,
Result<vector<Notification>> result);
void do_delete_message_logevent(const DeleteMessageLogEvent &logevent) const;
@ -1489,34 +1507,42 @@ class MessagesManager : public Actor {
void send_update_new_message(const Dialog *d, const Message *m);
static bool is_message_has_active_notification(const Dialog *d, const Message *m);
static bool is_from_mention_notification_group(const Dialog *d, const Message *m);
NotificationGroupId get_dialog_message_notification_group_id(Dialog *d);
static bool is_message_notification_active(const Dialog *d, const Message *m);
NotificationId get_next_notification_id(Dialog *d, MessageId message_id);
static NotificationGroupInfo &get_notification_group_info(Dialog *d, const Message *m);
vector<Notification> get_message_notifications_from_database_force(Dialog *d, int32 limit);
NotificationGroupId get_dialog_notification_group_id(DialogId dialog_id, NotificationGroupInfo &group_info);
Result<vector<BufferSlice>> do_get_message_notifications_from_database_force(Dialog *d,
NotificationId get_next_notification_id(Dialog *d, NotificationGroupId notification_group_id, MessageId message_id);
vector<Notification> get_message_notifications_from_database_force(Dialog *d, bool from_mentions, int32 limit);
Result<vector<BufferSlice>> do_get_message_notifications_from_database_force(Dialog *d, bool from_mentions,
NotificationId from_notification_id,
MessageId from_message_id, int32 limit);
void do_get_message_notifications_from_database(Dialog *d, NotificationId from_notification_id,
void do_get_message_notifications_from_database(Dialog *d, bool from_mentions, NotificationId from_notification_id,
MessageId from_message_id, int32 limit,
Promise<vector<Notification>> promise);
void on_get_message_notifications_from_database(DialogId dialog_id, int32 limit, Result<vector<BufferSlice>> result,
void on_get_message_notifications_from_database(DialogId dialog_id, bool from_mentions, int32 limit,
Result<vector<BufferSlice>> result,
Promise<vector<Notification>> promise);
void do_remove_message_notification(DialogId dialog_id, NotificationId notification_id, vector<BufferSlice> result);
void do_remove_message_notification(DialogId dialog_id, bool from_mentions, NotificationId notification_id,
vector<BufferSlice> result);
int32 get_dialog_pending_notification_count(Dialog *d);
int32 get_dialog_pending_notification_count(Dialog *d, bool from_mentions);
bool add_new_message_notification(Dialog *d, Message *m, bool force);
void flush_pending_new_message_notifications(DialogId dialog_id, DialogId settings_dialog_id);
void flush_pending_new_message_notifications(DialogId dialog_id, bool from_mentions, DialogId settings_dialog_id);
void remove_dialog_message_notifications(Dialog *d);
void remove_all_dialog_notifications(DialogId dialog_id, NotificationGroupInfo &group_info);
void remove_all_dialog_notifications(Dialog *d, MessageId max_message_id, NotificationGroupInfo &group_info);
void send_update_message_send_succeeded(Dialog *d, MessageId old_message_id, const Message *m) const;
@ -1604,8 +1630,8 @@ class MessagesManager : public Actor {
void try_restore_dialog_reply_markup(Dialog *d, const Message *m);
bool set_dialog_last_notification(Dialog *d, int32 last_notification_date, NotificationId last_notification_id,
const char *source);
bool set_dialog_last_notification(DialogId dialog_id, NotificationGroupInfo &group_info, int32 last_notification_date,
NotificationId last_notification_id, const char *source);
static string get_notification_settings_scope_database_key(NotificationSettingsScope scope);

View File

@ -260,7 +260,7 @@ int32 NotificationManager::load_message_notification_groups_from_database(int32
}
vector<NotificationGroupKey> group_keys = td_->messages_manager_->get_message_notification_group_keys_from_database(
last_loaded_notification_group_key_.last_notification_date, last_loaded_notification_group_key_.dialog_id, limit);
last_loaded_notification_group_key_, limit);
last_loaded_notification_group_key_ =
group_keys.size() == static_cast<size_t>(limit) ? group_keys.back() : NotificationGroupKey();
@ -322,7 +322,7 @@ void NotificationManager::load_message_notifications_from_database(const Notific
auto first_message_id = get_first_message_id(group);
auto from_message_id = first_message_id.is_valid() ? first_message_id : MessageId::max();
send_closure(G()->messages_manager(), &MessagesManager::get_message_notifications_from_database, group_key.dialog_id,
from_notification_id, from_message_id, static_cast<int32>(limit),
group_key.group_id, from_notification_id, from_message_id, static_cast<int32>(limit),
PromiseCreator::lambda([actor_id = actor_id(this), group_id = group_key.group_id,
limit](Result<vector<Notification>> r_notifications) {
send_closure_later(actor_id, &NotificationManager::on_get_message_notifications_from_database,
@ -669,7 +669,7 @@ void NotificationManager::add_update(int32 group_id, td_api::object_ptr<td_api::
if (!running_get_difference_ && running_get_chat_difference_.count(group_id) == 0) {
flush_pending_updates_timeout_.add_timeout_in(group_id, MIN_UPDATE_DELAY_MS * 1e-3);
} else {
flush_pending_updates_timeout_.set_timeout_in(group_id, MAX_UPDATE_DELAY_MS * 1e-3);
flush_pending_updates_timeout_.set_timeout_in(group_id, 3 * MAX_UPDATE_DELAY_MS * 1e-3);
}
}
@ -1256,7 +1256,7 @@ void NotificationManager::on_notifications_removed(
vector<int32> &&removed_notification_ids) {
VLOG(notifications) << "In on_notifications_removed for " << group_it->first.group_id << " with "
<< added_notifications.size() << " added notifications and " << removed_notification_ids.size()
<< " removed notifications";
<< " removed notifications, new total_count = " << group_it->second.total_count;
auto group_key = group_it->first;
auto final_group_key = group_key;
final_group_key.last_notification_date = 0;
@ -1384,7 +1384,7 @@ void NotificationManager::remove_notification(NotificationGroupId group_id, Noti
}
if (!is_permanent && group_it->second.contains_messages) {
td_->messages_manager_->remove_message_notification(group_it->first.dialog_id, notification_id);
td_->messages_manager_->remove_message_notification(group_it->first.dialog_id, group_id, notification_id);
}
for (auto it = group_it->second.pending_notifications.begin(); it != group_it->second.pending_notifications.end();
@ -1479,7 +1479,7 @@ void NotificationManager::remove_notification_group(NotificationGroupId group_id
max_notification_id = current_notification_id_;
}
if (group_it->second.contains_messages) {
td_->messages_manager_->remove_message_notifications(group_it->first.dialog_id, max_notification_id);
td_->messages_manager_->remove_message_notifications(group_it->first.dialog_id, group_id, max_notification_id);
}
}