Merge pull request #713 from Thulinma/noMoreDuplicates

Fixed duplicate messages appearing in timeline if server sent them
This commit is contained in:
DeepBlueV7.X 2021-09-05 22:24:16 +00:00 committed by GitHub
commit d30401c3c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 23 deletions

View File

@ -2999,12 +2999,15 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
eventsDb.put(txn, redaction->redacts, event.dump()); eventsDb.put(txn, redaction->redacts, event.dump());
eventsDb.put(txn, redaction->event_id, json(*redaction).dump()); eventsDb.put(txn, redaction->event_id, json(*redaction).dump());
} else { } else {
eventsDb.put(txn, event_id, event.dump());
++index;
first = false; first = false;
// This check protects against duplicates in the timeline. If the event_id
// is already in the DB, we skip putting it (again) in ordered DBs, and only
// update the event itself and its relations.
std::string_view unused_read;
if (!eventsDb.get(txn, event_id, unused_read)) {
++index;
nhlog::db()->debug("saving '{}'", orderEntry.dump()); nhlog::db()->debug("saving '{}'", orderEntry.dump());
cursor.put(lmdb::to_sv(index), orderEntry.dump(), MDB_APPEND); cursor.put(lmdb::to_sv(index), orderEntry.dump(), MDB_APPEND);
@ -3017,6 +3020,10 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex)); msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex));
} }
} else {
nhlog::db()->warn("duplicate event '{}'", orderEntry.dump());
}
eventsDb.put(txn, event_id, event.dump());
auto relations = mtx::accessors::relations(e); auto relations = mtx::accessors::relations(e);
if (!relations.relations.empty()) { if (!relations.relations.empty()) {
@ -3078,8 +3085,12 @@ Cache::saveOldMessages(const std::string &room_id, const mtx::responses::Message
auto event = mtx::accessors::serialize_event(e); auto event = mtx::accessors::serialize_event(e);
event_id_val = event["event_id"].get<std::string>(); event_id_val = event["event_id"].get<std::string>();
std::string_view event_id = event_id_val; std::string_view event_id = event_id_val;
eventsDb.put(txn, event_id, event.dump());
// This check protects against duplicates in the timeline. If the event_id is
// already in the DB, we skip putting it (again) in ordered DBs, and only update the
// event itself and its relations.
std::string_view unused_read;
if (!eventsDb.get(txn, event_id, unused_read)) {
--index; --index;
json orderEntry = json::object(); json orderEntry = json::object();
@ -3095,6 +3106,8 @@ Cache::saveOldMessages(const std::string &room_id, const mtx::responses::Message
msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex)); msg2orderDb.put(txn, event_id, lmdb::to_sv(msgIndex));
} }
}
eventsDb.put(txn, event_id, event.dump());
auto relations = mtx::accessors::relations(e); auto relations = mtx::accessors::relations(e);
if (!relations.relations.empty()) { if (!relations.relations.empty()) {