Skip to content

Commit

Permalink
Render edits
Browse files Browse the repository at this point in the history
  • Loading branch information
deepbluev7 committed Jan 27, 2021
1 parent 5968d47 commit c208620
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ if(USE_BUNDLED_MTXCLIENT)
FetchContent_Declare(
MatrixClient
GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git
GIT_TAG 4eb5b280541e45906bb53c26755f8da17cb7dfc2
GIT_TAG ec71ff24a39e955cd230e812ece83df68ebf0408
)
set(BUILD_LIB_EXAMPLES OFF CACHE INTERNAL "")
set(BUILD_LIB_TESTS OFF CACHE INTERNAL "")
Expand Down
2 changes: 1 addition & 1 deletion io.github.NhekoReborn.Nheko.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
"name": "mtxclient",
"sources": [
{
"commit": "4eb5b280541e45906bb53c26755f8da17cb7dfc2",
"commit": "ec71ff24a39e955cd230e812ece83df68ebf0408",
"type": "git",
"url": "https://github.com/Nheko-Reborn/mtxclient.git"
}
Expand Down
30 changes: 30 additions & 0 deletions src/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ Cache::isHiddenEvent(lmdb::txn &txn,
const std::string &room_id)
{
using namespace mtx::events;

// Always hide edits
if (mtx::accessors::relations(e).replaces())
return true;

if (auto encryptedEvent = std::get_if<EncryptedEvent<msg::Encrypted>>(&e)) {
MegolmSessionIndex index;
index.room_id = room_id;
Expand Down Expand Up @@ -1891,6 +1896,31 @@ Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id)
return *val.data<uint64_t>();
}

std::optional<uint64_t>
Cache::getArrivalIndex(const std::string &room_id, std::string_view event_id)
{
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);

lmdb::dbi orderDb{0};
try {
orderDb = getEventToOrderDb(txn, room_id);
} catch (lmdb::runtime_error &e) {
nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
room_id,
e.what());
return {};
}

lmdb::val indexVal{event_id.data(), event_id.size()}, val;

bool success = lmdb::dbi_get(txn, orderDb, indexVal, val);
if (!success) {
return {};
}

return *val.data<uint64_t>();
}

std::optional<std::string>
Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Cache_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class Cache : public QObject
std::optional<uint64_t> getTimelineIndex(const std::string &room_id,
std::string_view event_id);
std::optional<std::string> getTimelineEventId(const std::string &room_id, uint64_t index);
std::optional<uint64_t> getArrivalIndex(const std::string &room_id,
std::string_view event_id);

std::string previousBatchToken(const std::string &room_id);
uint64_t saveOldMessages(const std::string &room_id, const mtx::responses::Messages &res);
Expand Down
54 changes: 51 additions & 3 deletions src/timeline/EventStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,41 @@ EventStore::handle_room_verification(mtx::events::collections::TimelineEvents ev
event);
}

std::vector<mtx::events::collections::TimelineEvents>
EventStore::edits(const std::string &event_id)
{
auto event_ids = cache::client()->relatedEvents(room_id_, event_id);

auto original_event = get(event_id, "", false, false);
if (!original_event)
return {};

auto original_sender = mtx::accessors::sender(*original_event);

std::vector<mtx::events::collections::TimelineEvents> edits;
for (const auto &id : event_ids) {
auto related_event = get(id, event_id, false, false);
if (!related_event)
continue;

auto edit_rel = mtx::accessors::relations(*related_event);
if (edit_rel.replaces() == event_id &&
original_sender == mtx::accessors::sender(*related_event))
edits.push_back(*related_event);
}

auto c = cache::client();
std::sort(edits.begin(),
edits.end(),
[this, c](const mtx::events::collections::TimelineEvents &a,
const mtx::events::collections::TimelineEvents &b) {
return c->getArrivalIndex(this->room_id_, mtx::accessors::event_id(a)) <
c->getArrivalIndex(this->room_id_, mtx::accessors::event_id(b));
});

return edits;
}

QVariantList
EventStore::reactions(const std::string &event_id)
{
Expand Down Expand Up @@ -484,7 +519,13 @@ EventStore::get(int idx, bool decrypt)
if (!event_id)
return nullptr;

auto event = cache::client()->getEvent(room_id_, *event_id);
std::optional<mtx::events::collections::TimelineEvent> event;
auto edits_ = edits(*event_id);
if (edits_.empty())
event = cache::client()->getEvent(room_id_, *event_id);
else
event = {edits_.back()};

if (!event)
return nullptr;
else
Expand Down Expand Up @@ -711,15 +752,22 @@ EventStore::decryptEvent(const IdIndex &idx,
}

mtx::events::collections::TimelineEvents *
EventStore::get(std::string_view id, std::string_view related_to, bool decrypt)
EventStore::get(std::string_view id, std::string_view related_to, bool decrypt, bool resolve_edits)
{
if (this->thread() != QThread::currentThread())
nhlog::db()->warn("{} called from a different thread!", __func__);

if (id.empty())
return nullptr;

IdIndex index{room_id_, std::string(id.data(), id.size())};
std::string id_ = std::string(id);
if (resolve_edits) {
auto edits_ = edits(id_);
if (!edits_.empty())
id_ = mtx::accessors::event_id(edits_.back());
}

IdIndex index{room_id_, id_};

auto event_ptr = events_by_id_.object(index);
if (!event_ptr) {
Expand Down
4 changes: 3 additions & 1 deletion src/timeline/EventStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class EventStore : public QObject
// relatedFetched event
mtx::events::collections::TimelineEvents *get(std::string_view id,
std::string_view related_to,
bool decrypt = true);
bool decrypt = true,
bool resolve_edits = true);
// always returns a proper event as long as the idx is valid
mtx::events::collections::TimelineEvents *get(int idx, bool decrypt = true);

Expand Down Expand Up @@ -110,6 +111,7 @@ public slots:
void clearTimeline();

private:
std::vector<mtx::events::collections::TimelineEvents> edits(const std::string &event_id);
mtx::events::collections::TimelineEvents *decryptEvent(
const IdIndex &idx,
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &e);
Expand Down

0 comments on commit c208620

Please sign in to comment.