Fix timestamps after loading

This commit is contained in:
Nicolas Werner 2022-06-26 01:48:16 +02:00
parent dc4a06517c
commit 1145610694
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
4 changed files with 41 additions and 2 deletions

View file

@ -1815,7 +1815,7 @@ Cache::saveState(const mtx::responses::Sync &res)
if (!std::visit([](const auto &e) -> bool { return isMessage(e); }, e)) if (!std::visit([](const auto &e) -> bool { return isMessage(e); }, e))
continue; continue;
updatedInfo.approximate_last_modification_ts = updatedInfo.approximate_last_modification_ts =
mtx::accessors::origin_server_ts(e).toMSecsSinceEpoch(); std::visit([](const auto &e) -> bool { return e.origin_server_ts; }, e);
} }
roomsDb_.put(txn, room.first, nlohmann::json(updatedInfo).dump()); roomsDb_.put(txn, room.first, nlohmann::json(updatedInfo).dump());
@ -2019,6 +2019,35 @@ Cache::singleRoomInfo(const std::string &room_id)
return RoomInfo(); return RoomInfo();
} }
void
Cache::updateLastMessageTimestamp(const std::string &room_id, uint64_t ts)
{
auto txn = lmdb::txn::begin(env_);
try {
auto statesdb = getStatesDb(txn, room_id);
std::string_view data;
// Check if the room is joined.
if (roomsDb_.get(txn, room_id, data)) {
try {
RoomInfo tmp = nlohmann::json::parse(data).get<RoomInfo>();
tmp.approximate_last_modification_ts = ts;
roomsDb_.put(txn, room_id, nlohmann::json(tmp).dump());
txn.commit();
return;
} catch (const nlohmann::json::exception &e) {
nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}",
room_id,
std::string(data.data(), data.size()),
e.what());
}
}
} catch (const lmdb::error &e) {
nhlog::db()->warn("failed to read room info from db: room_id ({}), {}", room_id, e.what());
}
}
std::map<QString, RoomInfo> std::map<QString, RoomInfo>
Cache::getRoomInfo(const std::vector<std::string> &rooms) Cache::getRoomInfo(const std::vector<std::string> &rooms)
@ -4820,7 +4849,7 @@ from_json(const nlohmann::json &j, RoomInfo &info)
info.join_rule = j.at("join_rule").get<mtx::events::state::JoinRule>(); info.join_rule = j.at("join_rule").get<mtx::events::state::JoinRule>();
info.guest_access = j.at("guest_access").get<bool>(); info.guest_access = j.at("guest_access").get<bool>();
info.approximate_last_modification_ts = j.value("app_l_ts", 0); info.approximate_last_modification_ts = j.value<uint64_t>("app_l_ts", 0);
info.notification_count = j.value("notification_count", 0); info.notification_count = j.value("notification_count", 0);
info.highlight_count = j.value("highlight_count", 0); info.highlight_count = j.value("highlight_count", 0);

View file

@ -165,6 +165,7 @@ public:
RoomInfo singleRoomInfo(const std::string &room_id); RoomInfo singleRoomInfo(const std::string &room_id);
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res); std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms); std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
void updateLastMessageTimestamp(const std::string &room_id, uint64_t ts);
//! Calculates which the read status of a room. //! Calculates which the read status of a room.
//! Whether all the events in the timeline have been read. //! Whether all the events in the timeline have been read.

View file

@ -298,6 +298,7 @@ RoomlistModel::addRoom(const QString &room_id, bool suppressInsertNotification)
{ {
Roles::HasLoudNotification, Roles::HasLoudNotification,
Roles::LastMessage, Roles::LastMessage,
Roles::Time,
Roles::Timestamp, Roles::Timestamp,
Roles::NotificationCount, Roles::NotificationCount,
Qt::DisplayRole, Qt::DisplayRole,

View file

@ -1055,6 +1055,10 @@ TimelineModel::updateLastMessage()
ts, ts,
time}; time};
if (description != lastMessage_) { if (description != lastMessage_) {
if (lastMessage_.timestamp == 0) {
cache::client()->updateLastMessageTimestamp(room_id_.toStdString(),
description.timestamp);
}
lastMessage_ = description; lastMessage_ = description;
emit lastMessageChanged(); emit lastMessageChanged();
} }
@ -1068,6 +1072,10 @@ TimelineModel::updateLastMessage()
QString::fromStdString(http::client()->user_id().to_string()), QString::fromStdString(http::client()->user_id().to_string()),
cache::displayName(room_id_, QString::fromStdString(mtx::accessors::sender(*event)))); cache::displayName(room_id_, QString::fromStdString(mtx::accessors::sender(*event))));
if (description != lastMessage_) { if (description != lastMessage_) {
if (lastMessage_.timestamp == 0) {
cache::client()->updateLastMessageTimestamp(room_id_.toStdString(),
description.timestamp);
}
lastMessage_ = description; lastMessage_ = description;
emit lastMessageChanged(); emit lastMessageChanged();
} }