Add MessagesManager::get_message_notifications_from_database.

GitOrigin-RevId: 1eaea2fcc61814c5c87b3c7b2ad90ce749c152c0
This commit is contained in:
levlam 2018-11-28 23:19:30 +03:00
parent 07f4d0d94e
commit 9e3171ca7a
7 changed files with 84 additions and 4 deletions

View File

@ -193,6 +193,9 @@ class MessagesDbImpl : public MessagesDbSyncInterface {
"message_id > ?2 ORDER BY message_id ASC LIMIT ?3"));
TRY_RESULT(get_messages_desc_stmt, db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 "
"AND message_id < ?2 ORDER BY message_id DESC LIMIT ?3"));
TRY_RESULT(get_messages_from_notification_id_stmt,
db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 "
"AND notification_id < ?2 ORDER BY notification_id DESC LIMIT ?3"));
TRY_RESULT(
get_messages_fts_stmt,
db_.get_statement("SELECT dialog_id, data, search_id FROM messages WHERE search_id IN (SELECT rowid FROM "
@ -239,10 +242,12 @@ class MessagesDbImpl : public MessagesDbSyncInterface {
get_messages_stmt_.asc_stmt_ = std::move(get_messages_asc_stmt);
get_messages_stmt_.desc_stmt_ = std::move(get_messages_desc_stmt);
get_messages_from_notification_id_stmt_ = std::move(get_messages_from_notification_id_stmt);
get_messages_fts_stmt_ = std::move(get_messages_fts_stmt);
// LOG(ERROR) << get_message_stmt_.explain().ok();
// LOG(ERROR) << get_messages_from_notification_id_stmt.explain().ok();
// LOG(ERROR) << get_message_by_random_id_stmt_.explain().ok();
// LOG(ERROR) << get_message_by_unique_message_id_stmt_.explain().ok();
@ -529,6 +534,28 @@ class MessagesDbImpl : public MessagesDbSyncInterface {
return get_messages_impl(get_messages_stmt_, query.dialog_id, query.from_message_id, query.offset, query.limit);
}
Result<vector<BufferSlice>> get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id,
int32 limit) {
auto &stmt = get_messages_from_notification_id_stmt_;
SCOPE_EXIT {
stmt.reset();
};
stmt.bind_int64(1, dialog_id.get()).ensure();
stmt.bind_int32(2, from_notification_id.get()).ensure();
stmt.bind_int32(3, limit).ensure();
std::vector<BufferSlice> result;
stmt.step().ensure();
while (stmt.has_row()) {
auto data_slice = stmt.view_blob(0);
result.emplace_back(data_slice);
auto message_id = stmt.view_int64(1);
LOG(INFO) << "Load " << MessageId(message_id) << " in " << dialog_id << " from database";
stmt.step().ensure();
}
return std::move(result);
}
static string prepare_query(Slice query) {
auto is_word_character = [](uint32 a) {
switch (get_unicode_simple_category(a)) {
@ -717,6 +744,7 @@ class MessagesDbImpl : public MessagesDbSyncInterface {
SqliteStatement desc_stmt_;
};
GetMessagesStmt get_messages_stmt_;
SqliteStatement get_messages_from_notification_id_stmt_;
std::array<GetMessagesStmt, MESSAGES_DB_INDEX_COUNT> get_messages_from_index_stmts_;
std::array<SqliteStatement, 2> get_calls_stmts_;
@ -875,6 +903,11 @@ class MessagesDbAsync : public MessagesDbAsyncInterface {
void get_messages(MessagesDbMessagesQuery query, Promise<MessagesDbMessagesResult> promise) override {
send_closure_later(impl_, &Impl::get_messages, std::move(query), std::move(promise));
}
void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
Promise<vector<BufferSlice>> promise) override {
send_closure_later(impl_, &Impl::get_messages_from_notification_id, dialog_id, from_notification_id, limit,
std::move(promise));
}
void get_calls(MessagesDbCallsQuery query, Promise<MessagesDbCallsResult> promise) override {
send_closure_later(impl_, &Impl::get_calls, std::move(query), std::move(promise));
}
@ -947,6 +980,11 @@ class MessagesDbAsync : public MessagesDbAsyncInterface {
add_read_query();
promise.set_result(sync_db_->get_messages(std::move(query)));
}
void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
Promise<vector<BufferSlice>> promise) {
add_read_query();
promise.set_result(sync_db_->get_messages_from_notification_id(dialog_id, from_notification_id, limit));
}
void get_calls(MessagesDbCallsQuery query, Promise<MessagesDbCallsResult> promise) {
add_read_query();
promise.set_result(sync_db_->get_calls(std::move(query)));

View File

@ -106,6 +106,9 @@ class MessagesDbSyncInterface {
MessageId last_message_id, int32 date) = 0;
virtual Result<MessagesDbMessagesResult> get_messages(MessagesDbMessagesQuery query) = 0;
virtual Result<vector<BufferSlice>> get_messages_from_notification_id(DialogId dialog_id,
NotificationId from_notification_id,
int32 limit) = 0;
virtual Result<std::pair<std::vector<std::pair<DialogId, BufferSlice>>, int32>> get_expiring_messages(
int32 expire_from, int32 expire_till, int32 limit) = 0;
@ -148,9 +151,11 @@ class MessagesDbAsyncInterface {
virtual void get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id, MessageId last_message_id,
int32 date, Promise<BufferSlice> promise) = 0;
virtual void get_messages(MessagesDbMessagesQuery query, Promise<MessagesDbMessagesResult>) = 0;
virtual void get_messages(MessagesDbMessagesQuery query, Promise<MessagesDbMessagesResult> promise) = 0;
virtual void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
Promise<vector<BufferSlice>> promise) = 0;
virtual void get_calls(MessagesDbCallsQuery, Promise<MessagesDbCallsResult>) = 0;
virtual void get_calls(MessagesDbCallsQuery, Promise<MessagesDbCallsResult> promise) = 0;
virtual void get_messages_fts(MessagesDbFtsQuery query, Promise<MessagesDbFtsResult> promise) = 0;
virtual void get_expiring_messages(

View File

@ -17456,12 +17456,35 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat
VLOG(notifications) << "Found " << d->dialog_id << " by " << group_id;
result.dialog_id = d->dialog_id;
result.total_count = get_dialog_pending_notification_count(d);
// TODO load result.notifications
result.notifications = get_message_notifications_from_database(
d, NotificationId::max(), td_->notification_manager_->get_max_notification_group_size());
}
return result;
}
vector<Notification> MessagesManager::get_message_notifications_from_database(Dialog *d,
NotificationId from_notification_id,
int32 limit) {
CHECK(d != nullptr);
auto result = G()->td_db()->get_messages_db_sync()->get_messages_from_notification_id(d->dialog_id,
from_notification_id, limit);
if (result.is_error()) {
return {};
}
auto messages = result.move_as_ok();
vector<Notification> res;
res.reserve(messages.size());
for (auto &message : messages) {
auto m = on_get_message_from_database(d->dialog_id, d, std::move(message));
if (m != nullptr && m->notification_id.is_valid()) {
res.emplace_back(m->notification_id, m->date, create_new_message_notification(m->message_id));
}
}
return res;
}
int32 MessagesManager::get_dialog_pending_notification_count(Dialog *d) {
CHECK(d != nullptr);
if (is_dialog_muted(d)) {

View File

@ -1463,6 +1463,9 @@ class MessagesManager : public Actor {
NotificationGroupId get_dialog_message_notification_group_id(Dialog *d);
vector<Notification> get_message_notifications_from_database(Dialog *d, NotificationId from_notification_id,
int32 limit);
int32 get_dialog_pending_notification_count(Dialog *d);
bool add_new_message_notification(Dialog *d, Message *m, bool force);

View File

@ -10,6 +10,7 @@
#include "td/utils/StringBuilder.h"
#include <functional>
#include <limits>
#include <type_traits>
namespace td {
@ -18,12 +19,16 @@ class NotificationId {
public:
NotificationId() = default;
explicit NotificationId(int32 notification_id) : id(notification_id) {
explicit constexpr NotificationId(int32 notification_id) : id(notification_id) {
}
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
NotificationId(T notification_id) = delete;
static constexpr NotificationId max() {
return NotificationId(std::numeric_limits<int32>::max());
}
bool is_valid() const {
return id > 0;
}

View File

@ -131,6 +131,10 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group
return groups_.emplace(std::move(group_key), std::move(group)).first;
}
int32 NotificationManager::get_max_notification_group_size() const {
return max_notification_group_size_;
}
NotificationId NotificationManager::get_max_notification_id() const {
return current_notification_id_;
}

View File

@ -40,6 +40,8 @@ class NotificationManager : public Actor {
NotificationManager(Td *td, ActorShared<> parent);
int32 get_max_notification_group_size() const;
NotificationId get_max_notification_id() const;
NotificationId get_next_notification_id();