diff --git a/src/harbour-spoon.cpp b/src/harbour-spoon.cpp index 8d4abb3be139950ed316a39a398a2a4acb799974..e9cc62c7ef5b462d658b3d2ac1fdc2195db2de9d 100644 --- a/src/harbour-spoon.cpp +++ b/src/harbour-spoon.cpp @@ -44,6 +44,8 @@ int main(int argc, char *argv[]) { qmlRegisterUncreatableType<EventType>("dev.neko.spoon", 1, 0, "EventType", "Can't instantiate EventType"); qRegisterMetaType<std::vector<mtx::events::collections::TimelineEvents>>( "std::vector<mtx::events::collections::TimelineEvents>"); + qRegisterMetaType<std::vector<mtx::events::collections::StateEvents>>( + "std::vector<mtx::events::collections::StateEvents>"); qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>"); qRegisterMetaType<QSharedPointer<Room>>("QSharedPointer<Room>"); qRegisterMetaType<std::string>("std::string"); diff --git a/src/models/roommodel.cpp b/src/models/roommodel.cpp index 6c2a25155db07ab9df9fd9236f82f2ca8fb95afd..a69cf8ec5cfee1a4811498109b95295d545b9fe4 100644 --- a/src/models/roommodel.cpp +++ b/src/models/roommodel.cpp @@ -12,6 +12,7 @@ #include "debug_out.h" Q_DECLARE_METATYPE(std::vector<mtx::events::collections::TimelineEvents>); +Q_DECLARE_METATYPE(std::vector<mtx::events::collections::StateEvents>); Q_DECLARE_METATYPE(QSharedPointer<Room>); using namespace mtx::events; @@ -171,11 +172,9 @@ std::string Room::name() const { return QCoreApplication::translate("Room", "Empty Room").toStdString(); } -void Room::addEvents(std::vector<mtx::events::collections::TimelineEvents> events, std::string prev_batch, +void Room::addEvents(std::vector<mtx::events::collections::StateEvents> state, + std::vector<mtx::events::collections::TimelineEvents> timeline, std::string prev_batch, std::string next_batch) { - if (events.empty()) - return; - qDebug() << "p: " << QString::fromStdString(prev_batch); qDebug() << "n: " << QString::fromStdString(next_batch); qDebug() << "tp: " << QString::fromStdString(this->prev_batch); @@ -184,7 +183,7 @@ void Room::addEvents(std::vector<mtx::events::collections::TimelineEvents> event using namespace mtx::events; if (this->prev_batch == next_batch) { qDebug() << "New events at beginning of timeline"; - for (const auto &event : events) { + for (const auto &event : timeline) { std::string event_id = boost::apply_visitor([](const auto &e) -> std::string { return eventEventId(e); }, event); if (!event_id.empty() && eventIds.count(event_id)) { @@ -200,12 +199,14 @@ void Room::addEvents(std::vector<mtx::events::collections::TimelineEvents> event } } - beginInsertRows(QModelIndex(), (int)0, (int)events.size() - 1); - this->events.insert(this->events.begin(), events.begin(), events.end()); + if (!timeline.empty()) { + beginInsertRows(QModelIndex(), (int)0, (int)timeline.size() - 1); + this->events.insert(this->events.begin(), timeline.begin(), timeline.end()); + endInsertRows(); + } this->prev_batch = prev_batch; - endInsertRows(); } else { - for (const mtx::events::collections::TimelineEvents &e : events) { + auto applyEvent = [this](const mtx::events::collections::TimelineEvents &e) { this->eventIds.insert( boost::apply_visitor([](const auto &e) -> std::string { return eventEventId(e); }, e)); @@ -273,13 +274,20 @@ void Room::addEvents(std::vector<mtx::events::collections::TimelineEvents> event this->msgInfo.datetime = QDateTime::fromMSecsSinceEpoch( boost::apply_visitor([](const auto &e) -> uint64_t { return eventTimestamp(e); }, e)); } + }; + + for (const mtx::events::collections::StateEvents &e : state) + applyEvent(e); + for (const mtx::events::collections::TimelineEvents &e : timeline) + applyEvent(e); + + if (!timeline.empty()) { + qDebug() << "New events at end of timeline"; + beginInsertRows(QModelIndex(), (int)this->events.size(), (int)this->events.size() + timeline.size() - 1); + this->events.insert(this->events.end(), timeline.begin(), timeline.end()); + endInsertRows(); } - - qDebug() << "New events at end of timeline"; - beginInsertRows(QModelIndex(), (int)this->events.size(), (int)this->events.size() + events.size() - 1); - this->events.insert(this->events.end(), events.begin(), events.end()); this->next_batch = next_batch; - endInsertRows(); } if (this->prev_batch.empty()) this->prev_batch = prev_batch; @@ -303,7 +311,7 @@ void Room::fetchOlderMessages() { return; } - emit newEvents({res.chunk.rbegin(), res.chunk.rend()}, res.end, res.start); + emit newEvents({}, {res.chunk.rbegin(), res.chunk.rend()}, res.end, res.start); this->loadingOlderMessages = false; }); } diff --git a/src/models/roommodel.h b/src/models/roommodel.h index d5359682d358bcbb41bada6756f2037a49bc34bd..d77194afe2c8d1cef6effaf10ae2a60188e9ec3c 100644 --- a/src/models/roommodel.h +++ b/src/models/roommodel.h @@ -133,16 +133,18 @@ struct Room : public QAbstractListModel { std::set<std::string> eventIds; std::map<std::string, MemberInfo> memberInfos; - void applyEvents(const std::vector<mtx::events::collections::TimelineEvents> &events, std::string prev_batch, + void applyEvents(const std::vector<mtx::events::collections::StateEvents> &state, + const std::vector<mtx::events::collections::TimelineEvents> &timeline, std::string prev_batch, std::string next_batch) { - emit newEvents(events, prev_batch, next_batch); + emit newEvents(state, timeline, prev_batch, next_batch); } void applyTags(const std::vector<std::string> &tags) { emit setTags(tags); } void applyUnreadNotifications(int unread) { emit newUnreadState(unread); } std::string name() const; signals: - void newEvents(std::vector<mtx::events::collections::TimelineEvents> events, std::string prev_batch, + void newEvents(std::vector<mtx::events::collections::StateEvents> state, + std::vector<mtx::events::collections::TimelineEvents> events, std::string prev_batch, std::string next_batch); void newTags(std::vector<std::string> tags); void newUnreadState(int); @@ -151,7 +153,8 @@ struct Room : public QAbstractListModel { void inviteStatusChanged(bool isInvite); public slots: - void addEvents(std::vector<mtx::events::collections::TimelineEvents> events, std::string prev_batch, + void addEvents(std::vector<mtx::events::collections::StateEvents> state, + std::vector<mtx::events::collections::TimelineEvents> timeline, std::string prev_batch, std::string next_batch); void setTags(std::vector<std::string> tags) { this->tags = tags; } void setUnread(int unread) { this->unreadNotifications = unread; } diff --git a/src/sync.cpp b/src/sync.cpp index 321d39a30021e447df41da391c3fb27e2a6358bd..b2bc243b3129446386d724a5c4f4818d7cc569f1 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -141,7 +141,7 @@ void Sync::sync() { e); } - room->applyEvents(timeline, r.timeline.prev_batch, res.next_batch); + room->applyEvents(r.state.events, r.timeline.events, r.timeline.prev_batch, res.next_batch); room->applyUnreadNotifications(r.unread_notifications.notification_count); if (createRoom) @@ -168,17 +168,17 @@ void Sync::sync() { qDebug() << QString::fromStdString(room->id); - std::vector<mtx::events::collections::TimelineEvents> timeline; + std::vector<mtx::events::collections::StateEvents> state; for (const auto &e : r.invite_state) { boost::apply_visitor( - [&timeline](const auto &ev) { - mtx::events::collections::TimelineEvents tev = strippedToStateEv(ev); - timeline.push_back(tev); + [&state](const auto &ev) { + mtx::events::collections::StateEvents sev = strippedToStateEv(ev); + state.push_back(sev); }, e); } - room->applyEvents(timeline, "", res.next_batch); + room->applyEvents(state, {}, "", res.next_batch); if (createRoom) emit newRoom(room);