Fetch missing events

pull/251/head
Nicolas Werner 4 years ago
parent 530c531c4b
commit 3421728898
  1. 11
      src/Cache.cpp
  2. 3
      src/Cache_p.h
  3. 37
      src/timeline/EventStore.cpp
  4. 3
      src/timeline/EventStore.h
  5. 21
      src/timeline/TimelineModel.cpp

@ -1324,6 +1324,17 @@ Cache::getEvent(const std::string &room_id, const std::string &event_id)
return te; return te;
} }
void
Cache::storeEvent(const std::string &room_id,
const std::string &event_id,
const mtx::events::collections::TimelineEvent &event)
{
auto txn = lmdb::txn::begin(env_);
auto eventsDb = getEventsDb(txn, room_id);
auto event_json = mtx::accessors::serialize_event(event.data);
lmdb::dbi_put(txn, eventsDb, lmdb::val(event_id), lmdb::val(event_json.dump()));
txn.commit();
}
QMap<QString, RoomInfo> QMap<QString, RoomInfo>
Cache::roomInfo(bool withInvites) Cache::roomInfo(bool withInvites)

@ -185,6 +185,9 @@ public:
std::optional<mtx::events::collections::TimelineEvent> getEvent( std::optional<mtx::events::collections::TimelineEvent> getEvent(
const std::string &room_id, const std::string &room_id,
const std::string &event_id); const std::string &event_id);
void storeEvent(const std::string &room_id,
const std::string &event_id,
const mtx::events::collections::TimelineEvent &event);
struct TimelineRange struct TimelineRange
{ {
int64_t first, last; int64_t first, last;

@ -5,6 +5,7 @@
#include "Cache_p.h" #include "Cache_p.h"
#include "EventAccessors.h" #include "EventAccessors.h"
#include "Logging.h" #include "Logging.h"
#include "MatrixClient.h"
#include "Olm.h" #include "Olm.h"
QCache<EventStore::IdIndex, mtx::events::collections::TimelineEvents> EventStore::decryptedEvents_{ QCache<EventStore::IdIndex, mtx::events::collections::TimelineEvents> EventStore::decryptedEvents_{
@ -22,6 +23,23 @@ EventStore::EventStore(std::string room_id, QObject *)
this->first = range->first; this->first = range->first;
this->last = range->last; this->last = range->last;
} }
connect(
this,
&EventStore::eventFetched,
this,
[this](std::string id,
std::string relatedTo,
mtx::events::collections::TimelineEvents timeline) {
cache::client()->storeEvent(room_id_, id, {timeline});
if (!relatedTo.empty()) {
auto idx = idToIndex(id);
if (idx)
emit dataChanged(*idx, *idx);
}
},
Qt::QueuedConnection);
} }
void void
@ -241,8 +259,23 @@ EventStore::event(std::string_view id, std::string_view related_to, bool decrypt
if (!event_ptr) { if (!event_ptr) {
auto event = cache::client()->getEvent(room_id_, index.id); auto event = cache::client()->getEvent(room_id_, index.id);
if (!event) { if (!event) {
// fetch http::client()->get_event(
(void)related_to; room_id_,
index.id,
[this,
relatedTo = std::string(related_to.data(), related_to.size()),
id = index.id](const mtx::events::collections::TimelineEvents &timeline,
mtx::http::RequestErr err) {
if (err) {
nhlog::net()->error(
"Failed to retrieve event with id {}, which was "
"requested to show the replyTo for event {}",
relatedTo,
id);
return;
}
emit eventFetched(id, relatedTo, timeline);
});
return nullptr; return nullptr;
} }
event_ptr = new mtx::events::collections::TimelineEvents(std::move(event->data)); event_ptr = new mtx::events::collections::TimelineEvents(std::move(event->data));

@ -81,6 +81,9 @@ signals:
void endInsertRows(); void endInsertRows();
void dataChanged(int from, int to); void dataChanged(int from, int to);
void newEncryptedImage(mtx::crypto::EncryptedFile encryptionInfo); void newEncryptedImage(mtx::crypto::EncryptedFile encryptionInfo);
void eventFetched(std::string id,
std::string relatedTo,
mtx::events::collections::TimelineEvents timeline);
private: private:
mtx::events::collections::TimelineEvents *decryptEvent( mtx::events::collections::TimelineEvents *decryptEvent(

@ -696,27 +696,6 @@ TimelineModel::internalAddEvents(
emit dataChanged(index(idx, 0), index(idx, 0)); emit dataChanged(index(idx, 0), index(idx, 0));
continue; // don't insert reaction into timeline continue; // don't insert reaction into timeline
} }
// auto replyTo = mtx::accessors::in_reply_to_event(e);
// auto qReplyTo = QString::fromStdString(replyTo);
// if (!replyTo.empty() && !events.contains(qReplyTo)) {
// http::client()->get_event(
// this->room_id_.toStdString(),
// replyTo,
// [this, id, replyTo](
// const mtx::events::collections::TimelineEvents &timeline,
// mtx::http::RequestErr err) {
// if (err) {
// nhlog::net()->error(
// "Failed to retrieve event with id {}, which was "
// "requested to show the replyTo for event {}",
// replyTo,
// id.toStdString());
// return;
// }
// emit eventFetched(id, timeline);
// });
//}
} }
} }

Loading…
Cancel
Save