Fix editing pending messages

This commit is contained in:
Nicolas Werner 2022-02-27 05:02:54 +01:00
parent 4946548997
commit 9f5b647fb3
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
3 changed files with 31 additions and 10 deletions

View File

@ -2931,6 +2931,28 @@ Cache::savePendingMessage(const std::string &room_id,
txn.commit();
}
std::vector<std::string>
Cache::pendingEvents(const std::string &room_id)
{
auto txn = ro_txn(env_);
auto pending = getPendingMessagesDb(txn, room_id);
std::vector<std::string> related_ids;
try {
{
auto pendingCursor = lmdb::cursor::open(txn, pending);
std::string_view tsIgnored, pendingTxn;
while (pendingCursor.get(tsIgnored, pendingTxn, MDB_NEXT)) {
related_ids.emplace_back(pendingTxn.data(), pendingTxn.size());
}
}
} catch (const lmdb::error &e) {
nhlog::db()->error("pending events error: {}", e.what());
}
return related_ids;
}
std::optional<mtx::events::collections::TimelineEvent>
Cache::firstPendingMessage(const std::string &room_id)

View File

@ -218,6 +218,7 @@ public:
uint64_t saveOldMessages(const std::string &room_id, const mtx::responses::Messages &res);
void savePendingMessage(const std::string &room_id,
const mtx::events::collections::TimelineEvent &message);
std::vector<std::string> pendingEvents(const std::string &room_id);
std::optional<mtx::events::collections::TimelineEvent>
firstPendingMessage(const std::string &room_id);
void removePendingStatus(const std::string &room_id, const std::string &txn_id);

View File

@ -187,15 +187,13 @@ EventStore::EventStore(std::string room_id, QObject *)
// FIXME (introduced by balsoft): this doesn't work for encrypted events, but
// allegedly it's hard to fix so I'll leave my first contribution at that
for (const auto &related_event_id : cache::client()->relatedEvents(room_id_, txn_id)) {
if (cache::client()->getEvent(room_id_, related_event_id)) {
auto related_event =
cache::client()->getEvent(room_id_, related_event_id).value();
auto relations = mtx::accessors::relations(related_event.data);
for (const auto &pending_event_id : cache::client()->pendingEvents(room_id_)) {
if (auto pending_event = cache::client()->getEvent(room_id_, pending_event_id)) {
auto relations = mtx::accessors::relations(pending_event->data);
// Replace the blockquote in fallback reply
auto related_text = std::get_if<mtx::events::RoomEvent<mtx::events::msg::Text>>(
&related_event.data);
&pending_event->data);
if (related_text && relations.reply_to() == txn_id) {
size_t index = related_text->content.formatted_body.find(txn_id);
if (index != std::string::npos) {
@ -209,13 +207,13 @@ EventStore::EventStore(std::string room_id, QObject *)
rel.event_id = event_id;
}
mtx::accessors::set_relations(related_event.data, std::move(relations));
mtx::accessors::set_relations(pending_event->data, std::move(relations));
cache::client()->replaceEvent(room_id_, related_event_id, related_event);
cache::client()->replaceEvent(room_id_, pending_event_id, *pending_event);
auto idx = idToIndex(related_event_id);
auto idx = idToIndex(pending_event_id);
events_by_id_.remove({room_id_, related_event_id});
events_by_id_.remove({room_id_, pending_event_id});
events_.remove({room_id_, toInternalIdx(*idx)});
}
}