Return to redacted messages instead of just storing the redaction

This commit is contained in:
Nicolas Werner 2020-07-25 14:08:13 +02:00
parent 147ae68c31
commit b294430fe5
2 changed files with 44 additions and 21 deletions

View file

@ -2242,11 +2242,17 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
auto event = mtx::accessors::serialize_event(e); auto event = mtx::accessors::serialize_event(e);
auto txn_id = mtx::accessors::transaction_id(e); auto txn_id = mtx::accessors::transaction_id(e);
std::string event_id_val = event.value("event_id", "");
if (event_id_val.empty()) {
nhlog::db()->error("Event without id!");
continue;
}
lmdb::val event_id = event_id_val;
lmdb::val txn_order; lmdb::val txn_order;
if (!txn_id.empty() && if (!txn_id.empty() &&
lmdb::dbi_get(txn, evToOrderDb, lmdb::val(txn_id), txn_order)) { lmdb::dbi_get(txn, evToOrderDb, lmdb::val(txn_id), txn_order)) {
std::string event_id_val = event["event_id"].get<std::string>();
lmdb::val event_id = event_id_val;
lmdb::dbi_put(txn, eventsDb, event_id, lmdb::val(event.dump())); lmdb::dbi_put(txn, eventsDb, event_id, lmdb::val(event.dump()));
lmdb::dbi_del(txn, eventsDb, lmdb::val(txn_id)); lmdb::dbi_del(txn, eventsDb, lmdb::val(txn_id));
@ -2291,28 +2297,40 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
if (redaction->redacts.empty()) if (redaction->redacts.empty())
continue; continue;
lmdb::val ev{}; lmdb::val oldEvent;
lmdb::dbi_put( bool success =
txn, eventsDb, lmdb::val(redaction->redacts), lmdb::val(event.dump())); lmdb::dbi_get(txn, eventsDb, lmdb::val(redaction->redacts), oldEvent);
lmdb::dbi_put( if (!success)
txn, eventsDb, lmdb::val(redaction->event_id), lmdb::val(event.dump())); continue;
lmdb::val oldIndex{}; mtx::events::collections::TimelineEvent te;
if (lmdb::dbi_get( try {
txn, msg2orderDb, lmdb::val(redaction->redacts), oldIndex)) { mtx::events::collections::from_json(
lmdb::dbi_put( json::parse(std::string_view(oldEvent.data(), oldEvent.size())),
txn, order2msgDb, oldIndex, lmdb::val(redaction->event_id)); te);
lmdb::dbi_put( // overwrite the content and add redation data
txn, msg2orderDb, lmdb::val(redaction->event_id), oldIndex); std::visit(
} [redaction](auto &ev) {
} else { ev.unsigned_data.redacted_because = *redaction;
std::string event_id_val = event.value("event_id", ""); ev.unsigned_data.redacted_by = redaction->event_id;
if (event_id_val.empty()) { },
nhlog::db()->error("Event without id!"); te.data);
event = mtx::accessors::serialize_event(te.data);
event["content"].clear();
} catch (std::exception &e) {
nhlog::db()->error("Failed to parse message from cache {}",
e.what());
continue; continue;
} }
lmdb::val event_id = event_id_val; lmdb::dbi_put(
txn, eventsDb, lmdb::val(redaction->redacts), lmdb::val(event.dump()));
lmdb::dbi_put(txn,
eventsDb,
lmdb::val(redaction->event_id),
lmdb::val(json(*redaction).dump()));
} else {
lmdb::dbi_put(txn, eventsDb, event_id, lmdb::val(event.dump())); lmdb::dbi_put(txn, eventsDb, event_id, lmdb::val(event.dump()));
++index; ++index;

View file

@ -211,6 +211,7 @@ EventStore::handleSync(const mtx::responses::Timeline &events)
if (idx) { if (idx) {
events_by_id_.remove( events_by_id_.remove(
{room_id_, redaction->redacts}); {room_id_, redaction->redacts});
events_.remove({room_id_, toInternalIdx(*idx)});
emit dataChanged(*idx, *idx); emit dataChanged(*idx, *idx);
} }
} }
@ -227,8 +228,12 @@ EventStore::handleSync(const mtx::responses::Timeline &events)
if (!relates_to.empty()) { if (!relates_to.empty()) {
auto idx = cache::client()->getTimelineIndex(room_id_, relates_to); auto idx = cache::client()->getTimelineIndex(room_id_, relates_to);
if (idx) if (idx) {
events_by_id_.remove({room_id_, relates_to});
decryptedEvents_.remove({room_id_, relates_to});
events_.remove({room_id_, *idx});
emit dataChanged(toExternalIdx(*idx), toExternalIdx(*idx)); emit dataChanged(toExternalIdx(*idx), toExternalIdx(*idx));
}
} }
if (auto txn_id = mtx::accessors::transaction_id(event); !txn_id.empty()) { if (auto txn_id = mtx::accessors::transaction_id(event); !txn_id.empty()) {