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. //! There should be only one user id present in a receipt list per room.
//! The user id should be removed from any other lists. //! The user id should be removed from any other lists.
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>; 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. //! 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 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) { for (const auto &receipt : receipts) {
const auto event_id = receipt.first; 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; nlohmann::json json_key = receipt_key;
try { try {
auto read_txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
const auto key = json_key.dump(); const auto key = json_key.dump();
lmdb::val prev_value; lmdb::val prev_value;
bool exists = lmdb::dbi_get( bool exists = lmdb::dbi_get(
read_txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), prev_value); txn, readReceiptsDb_, lmdb::val(key.data(), key.size()), prev_value);
read_txn.commit();
std::map<std::string, uint64_t> saved_receipts; 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; nlohmann::json json_updated_value = saved_receipts;
std::string merged_receipts = json_updated_value.dump(); std::string merged_receipts = json_updated_value.dump();
auto txn = lmdb::txn::begin(env_);
lmdb::dbi_put(txn, lmdb::dbi_put(txn,
readReceiptsDb_, readReceiptsDb_,
lmdb::val(key.data(), key.size()), lmdb::val(key.data(), key.size()),
lmdb::val(merged_receipts.data(), merged_receipts.size())); lmdb::val(merged_receipts.data(), merged_receipts.size()));
txn.commit();
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {
qCritical() << "updateReadReceipts:" << e.what(); qCritical() << "updateReadReceipts:" << e.what();
} }
@ -388,6 +382,8 @@ Cache::saveState(const mtx::responses::Sync &res)
lmdb::dbi_put( lmdb::dbi_put(
txn, roomsDb_, lmdb::val(room.first), lmdb::val(json(updatedInfo).dump())); txn, roomsDb_, lmdb::val(room.first), lmdb::val(json(updatedInfo).dump()));
updateReadReceipt(txn, room.first, room.second.ephemeral.receipts);
} }
saveInvites(txn, res.rooms.invite); saveInvites(txn, res.rooms.invite);

View file

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

View file

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