Implement loading of history, when timeline is displayed

remotes/origin/0.7.0-dev
Nicolas Werner 5 years ago
parent 47fbfd3f44
commit 699fd7b38e
  1. 6
      resources/qml/TimelineView.qml
  2. 65
      src/timeline2/TimelineModel.cpp
  3. 21
      src/timeline2/TimelineModel.h
  4. 3
      src/timeline2/TimelineViewManager.cpp

@ -9,12 +9,6 @@ Rectangle {
text: qsTr("No room open") text: qsTr("No room open")
font.pointSize: 24 font.pointSize: 24
} }
Text {
visible: timelineManager.timeline != null
anchors.centerIn: parent
text: qsTr("room open")
font.pointSize: 24
}
ListView { ListView {
visible: timelineManager.timeline != null visible: timelineManager.timeline != null

@ -24,6 +24,14 @@ senderId(const T &event)
} }
} }
TimelineModel::TimelineModel(QString room_id, QObject *parent)
: QAbstractListModel(parent)
, room_id_(room_id)
{
connect(
this, &TimelineModel::oldMessagesRetrieved, this, &TimelineModel::addBackwardsEvents);
}
QHash<int, QByteArray> QHash<int, QByteArray>
TimelineModel::roleNames() const TimelineModel::roleNames() const
{ {
@ -65,6 +73,11 @@ TimelineModel::data(const QModelIndex &index, int role) const
void void
TimelineModel::addEvents(const mtx::responses::Timeline &events) TimelineModel::addEvents(const mtx::responses::Timeline &events)
{ {
if (isInitialSync) {
prev_batch_token_ = QString::fromStdString(events.prev_batch);
isInitialSync = false;
}
nhlog::ui()->info("add {} events", events.events.size()); nhlog::ui()->info("add {} events", events.events.size());
std::vector<QString> ids; std::vector<QString> ids;
for (const auto &e : events.events) { for (const auto &e : events.events) {
@ -83,6 +96,58 @@ TimelineModel::addEvents(const mtx::responses::Timeline &events)
endInsertRows(); endInsertRows();
} }
void
TimelineModel::fetchHistory()
{
if (paginationInProgress) {
nhlog::ui()->warn("Already loading older messages");
return;
}
paginationInProgress = true;
mtx::http::MessagesOpts opts;
opts.room_id = room_id_.toStdString();
opts.from = prev_batch_token_.toStdString();
nhlog::ui()->info("Paginationg room {}", opts.room_id);
http::client()->messages(
opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
if (err) {
nhlog::net()->error("failed to call /messages ({}): {} - {}",
opts.room_id,
mtx::errors::to_string(err->matrix_error.errcode),
err->matrix_error.error);
return;
}
emit oldMessagesRetrieved(std::move(res));
});
}
void
TimelineModel::addBackwardsEvents(const mtx::responses::Messages &msgs)
{
nhlog::ui()->info("add {} backwards events", msgs.chunk.size());
std::vector<QString> ids;
for (const auto &e : msgs.chunk) {
QString id =
boost::apply_visitor([](const auto &e) -> QString { return eventId(e); }, e);
this->events.insert(id, e);
ids.push_back(id);
nhlog::ui()->info("add event {}", id.toStdString());
}
beginInsertRows(QModelIndex(), 0, static_cast<int>(ids.size() - 1));
this->eventOrder.insert(this->eventOrder.begin(), ids.rbegin(), ids.rend());
endInsertRows();
prev_batch_token_ = QString::fromStdString(msgs.end);
paginationInProgress = false;
}
QColor QColor
TimelineModel::userColor(QString id, QColor background) TimelineModel::userColor(QString id, QColor background)
{ {

@ -11,9 +11,7 @@ class TimelineModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
public: public:
explicit TimelineModel(QObject *parent = 0) explicit TimelineModel(QString room_id, QObject *parent = 0);
: QAbstractListModel(parent)
{}
enum Roles enum Roles
{ {
@ -31,12 +29,29 @@ public:
Q_INVOKABLE QColor userColor(QString id, QColor background); Q_INVOKABLE QColor userColor(QString id, QColor background);
void addEvents(const mtx::responses::Timeline &events); void addEvents(const mtx::responses::Timeline &events);
public slots:
void fetchHistory();
private slots:
// Add old events at the top of the timeline.
void addBackwardsEvents(const mtx::responses::Messages &msgs);
signals:
void oldMessagesRetrieved(const mtx::responses::Messages &res);
private: private:
QHash<QString, mtx::events::collections::TimelineEvents> events; QHash<QString, mtx::events::collections::TimelineEvents> events;
std::vector<QString> eventOrder; std::vector<QString> eventOrder;
QString room_id_;
QString prev_batch_token_;
bool isInitialSync = true;
bool paginationInProgress = false;
QHash<QString, QColor> userColors; QHash<QString, QColor> userColors;
}; };

@ -27,7 +27,7 @@ void
TimelineViewManager::addRoom(const QString &room_id) TimelineViewManager::addRoom(const QString &room_id)
{ {
if (!models.contains(room_id)) if (!models.contains(room_id))
models.insert(room_id, QSharedPointer<TimelineModel>(new TimelineModel())); models.insert(room_id, QSharedPointer<TimelineModel>(new TimelineModel(room_id)));
} }
void void
@ -38,6 +38,7 @@ TimelineViewManager::setHistoryView(const QString &room_id)
auto room = models.find(room_id); auto room = models.find(room_id);
if (room != models.end()) { if (room != models.end()) {
timeline_ = room.value().get(); timeline_ = room.value().get();
timeline_->fetchHistory();
emit activeTimelineChanged(timeline_); emit activeTimelineChanged(timeline_);
nhlog::ui()->info("Activated room {}", room_id.toStdString()); nhlog::ui()->info("Activated room {}", room_id.toStdString());
} }

Loading…
Cancel
Save