Skip to content
Snippets Groups Projects
Commit 9f698097 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Move state processing to rooms

parent 51c00039
No related branches found
No related tags found
No related merge requests found
......@@ -3,5 +3,21 @@
#include <QMetaType>
#include <QSharedPointer>
#include <QDebug>
Q_DECLARE_METATYPE(Room);
Q_DECLARE_METATYPE(QSharedPointer<Room>);
void Room::applyEvents(const std::vector<mtx::events::collections::TimelineEvent> &events) {
using namespace mtx::events;
for (const mtx::events::collections::TimelineEvent &e : events) {
if (const auto ev = boost::get<StateEvent<state::Name>>(&e.data))
this->name = ev->content.name;
else if (const auto ev = boost::get<RoomEvent<msg::Text>>(&e.data)) {
this->msgInfo.body = QString::fromStdString(ev->content.body);
this->lastMessage = ev->content.body;
this->msgInfo.userid = QString::fromStdString(ev->sender);
}
}
this->events.insert(this->events.end(), events.begin(), events.end());
}
......@@ -40,6 +40,8 @@ struct Room {
//! The list of tags associated with this room
std::vector<std::string> tags;
void applyEvents(const std::vector<mtx::events::collections::TimelineEvent> &events);
private:
std::vector<mtx::events::collections::TimelineEvent> events;
std::string prev_batch;
......
......@@ -26,6 +26,15 @@ void Sync::stopSync() {
void Sync::run() { emit nextSync(); }
template <class T> auto strippedToStateEv(const mtx::events::StrippedEvent<T> &stripped) -> mtx::events::StateEvent<T> {
mtx::events::StateEvent<T> ev;
ev.state_key = stripped.state_key;
ev.sender = stripped.sender;
ev.content = stripped.content;
ev.type = stripped.type;
return ev;
}
void Sync::sync() {
qDebug() << "sync";
......@@ -65,21 +74,22 @@ void Sync::sync() {
room->id = join.first;
qDebug() << QString::fromStdString(room->id);
std::vector<mtx::events::collections::TimelineEvent> timeline;
for (const auto &e : r.state.events) {
// if (const auto n = boost::get<mtx::events::StateEvent<states::Name>>(&e))
if (const auto n = boost::get<mtx::events::StateEvent<mtx::events::state::Name>>(&e)) {
room->name = n->content.name;
qDebug() << "Name event: " << QString::fromStdString(n->content.name);
}
boost::apply_visitor(
[&timeline](const auto &ev) { timeline.push_back(mtx::events::collections::TimelineEvent{ev}); },
e);
}
for (const auto &e : r.timeline.events) {
if (const auto n = boost::get<mtx::events::StateEvent<mtx::events::state::Name>>(&e)) {
room->name = n->content.name;
qDebug() << "Name event: " << QString::fromStdString(n->content.name);
}
boost::apply_visitor(
[&timeline](const auto &ev) { timeline.push_back(mtx::events::collections::TimelineEvent{ev}); },
e);
}
room->applyEvents(timeline);
room->unreadNotifications = r.unread_notifications.highlight_count;
room->unreadNotifications = r.unread_notifications.notification_count;
if (createRoom)
emit newRoom(room);
else
......@@ -101,11 +111,15 @@ void Sync::sync() {
room->is_invite = true;
qDebug() << QString::fromStdString(room->id);
std::vector<mtx::events::collections::TimelineEvent> timeline;
for (const auto &e : r.invite_state) {
if (const auto n = boost::get<mtx::events::StrippedEvent<mtx::events::state::Name>>(&e)) {
room->name = n->content.name;
qDebug() << "Name event: " << QString::fromStdString(n->content.name);
}
boost::apply_visitor(
[&timeline](const auto &ev) {
mtx::events::collections::TimelineEvent tev;
tev.data = strippedToStateEv(ev);
timeline.push_back(tev);
},
e);
}
if (createRoom)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment