Save read receipts

This commit is contained in:
Konstantinos Sideris 2018-04-21 17:14:16 +03:00
parent 2f00fc51bf
commit 4a2f1af090
4 changed files with 17 additions and 21 deletions

View File

@ -170,7 +170,9 @@ public:
//! There should be only one user id present in a receipt list per room.
//! The user id should be removed from any other lists.
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
void updateReadReceipt(const std::string &room_id, const Receipts &receipts);
void updateReadReceipt(lmdb::txn &txn,
const std::string &room_id,
const Receipts &receipts);
//! Retrieve all the read receipts for the given event id and room.
//!

View File

@ -310,7 +310,7 @@ Cache::readReceipts(const QString &event_id, const QString &room_id)
}
void
Cache::updateReadReceipt(const std::string &room_id, const Receipts &receipts)
Cache::updateReadReceipt(lmdb::txn &txn, const std::string &room_id, const Receipts &receipts)
{
for (const auto &receipt : receipts) {
const auto event_id = receipt.first;
@ -320,15 +320,12 @@ Cache::updateReadReceipt(const std::string &room_id, const Receipts &receipts)
nlohmann::json json_key = receipt_key;
try {
auto read_txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
const auto key = json_key.dump();
lmdb::val prev_value;
bool exists = lmdb::dbi_get(
read_txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), prev_value);
read_txn.commit();
txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), prev_value);
std::map<std::string, uint64_t> saved_receipts;
@ -350,14 +347,11 @@ Cache::updateReadReceipt(const std::string &room_id, const Receipts &receipts)
nlohmann::json json_updated_value = saved_receipts;
std::string merged_receipts = json_updated_value.dump();
auto txn = lmdb::txn::begin(env_);
lmdb::dbi_put(txn,
readReceiptsDb_,
lmdb::val(key.data(), key.size()),
lmdb::val(merged_receipts.data(), merged_receipts.size()));
txn.commit();
} catch (const lmdb::error &e) {
qCritical() << "updateReadReceipts:" << e.what();
}
@ -388,6 +382,8 @@ Cache::saveState(const mtx::responses::Sync &res)
lmdb::dbi_put(
txn, roomsDb_, lmdb::val(room.first), lmdb::val(json(updatedInfo).dump()));
updateReadReceipt(txn, room.first, room.second.ephemeral.receipts);
}
saveInvites(txn, res.rooms.invite);

View File

@ -17,6 +17,7 @@
#include <QApplication>
#include <QDebug>
#include <QFuture>
#include <QSettings>
#include <QtConcurrent>
@ -424,6 +425,14 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
connect(this, &ChatPage::syncUI, this, [this](const mtx::responses::Rooms &rooms) {
view_manager_->initialize(rooms);
removeLeftRooms(rooms.leave);
for (const auto &room : rooms.join) {
auto room_id = QString::fromStdString(room.first);
updateTypingUsers(room_id, room.second.ephemeral.typing);
updateRoomNotificationCount(
room_id, room.second.unread_notifications.notification_count);
}
});
connect(this, &ChatPage::syncRoomlist, room_list_, &RoomList::sync);
@ -518,16 +527,7 @@ ChatPage::syncCompleted(const mtx::responses::Sync &response)
{
syncTimeoutTimer_->stop();
// Process ephemeral data per room.
for (const auto &room : response.rooms.join) {
auto room_id = QString::fromStdString(room.first);
updateTypingUsers(room_id, room.second.ephemeral.typing);
updateRoomNotificationCount(room_id,
room.second.unread_notifications.notification_count);
}
QtConcurrent::run([this, res = std::move(response)]() {
auto promise = QtConcurrent::run([this, res = std::move(response)]() {
try {
cache_->saveState(res);
emit syncRoomlist(cache_->roomUpdates(res));

View File

@ -352,8 +352,6 @@ RoomList::paintEvent(QPaintEvent *)
void
RoomList::updateRoom(const QString &room_id, const RoomInfo &info)
{
qDebug() << "updateRoom" << QString::fromStdString(info.name) << room_id;
if (!roomExists(room_id)) {
if (info.is_invite)
addInvitedRoom(room_id, info);