Handle some more edge cases in timeline

This commit is contained in:
Konstantinos Sideris 2018-08-31 10:47:27 +03:00
parent d608950bea
commit 641364c105
3 changed files with 37 additions and 5 deletions

View file

@ -1110,9 +1110,15 @@ ChatPage::createRoom(const mtx::requests::CreateRoom &req)
http::client()->create_room( http::client()->create_room(
req, [this](const mtx::responses::CreateRoom &res, mtx::http::RequestErr err) { req, [this](const mtx::responses::CreateRoom &res, mtx::http::RequestErr err) {
if (err) { if (err) {
const auto err_code = mtx::errors::to_string(err->matrix_error.errcode);
const auto error = err->matrix_error.error;
const int status_code = static_cast<int>(err->status_code);
nhlog::net()->warn(
"failed to create room: {} {} ({})", error, err_code, status_code);
emit showNotification( emit showNotification(
tr("Room creation failed: %1") tr("Room creation failed: %1").arg(QString::fromStdString(error)));
.arg(QString::fromStdString(err->matrix_error.error)));
return; return;
} }

View file

@ -164,11 +164,19 @@ TimelineView::sliderMoved(int position)
} }
} }
bool
TimelineView::isStartOfTimeline(const mtx::responses::Messages &msgs)
{
return (msgs.chunk.size() == 0 && (msgs.end.empty() || msgs.end == msgs.start));
}
void void
TimelineView::addBackwardsEvents(const mtx::responses::Messages &msgs) TimelineView::addBackwardsEvents(const mtx::responses::Messages &msgs)
{ {
// We've reached the start of the timline and there're no more messages. // We've reached the start of the timline and there're no more messages.
if (msgs.end.empty() || ((msgs.end == msgs.start) && msgs.chunk.size() == 0)) { if (isStartOfTimeline(msgs)) {
nhlog::ui()->info("[{}] start of timeline reached, no more messages to fetch",
room_id_.toStdString());
isTimelineFinished = true; isTimelineFinished = true;
return; return;
} }
@ -562,6 +570,13 @@ TimelineView::init()
void void
TimelineView::getMessages() TimelineView::getMessages()
{ {
if (prev_batch_token_.isEmpty()) {
nhlog::ui()->info("[{}] start of timeline reached, prev_batch token is empty",
room_id_.toStdString());
isTimelineFinished = true;
return;
}
mtx::http::MessagesOpts opts; mtx::http::MessagesOpts opts;
opts.room_id = room_id_.toStdString(); opts.room_id = room_id_.toStdString();
opts.from = prev_batch_token_.toStdString(); opts.from = prev_batch_token_.toStdString();
@ -829,13 +844,22 @@ TimelineView::sendNextPendingMessage()
void void
TimelineView::notifyForLastEvent() TimelineView::notifyForLastEvent()
{ {
auto lastItem = scroll_layout_->itemAt(scroll_layout_->count() - 1); if (scroll_layout_->count() == 0) {
nhlog::ui()->error("notifyForLastEvent called with empty timeline");
return;
}
auto lastItem = scroll_layout_->itemAt(scroll_layout_->count() - 1);
if (!lastItem)
return;
auto *lastTimelineItem = qobject_cast<TimelineItem *>(lastItem->widget()); auto *lastTimelineItem = qobject_cast<TimelineItem *>(lastItem->widget());
if (lastTimelineItem) if (lastTimelineItem)
emit updateLastTimelineMessage(room_id_, lastTimelineItem->descriptionMessage()); emit updateLastTimelineMessage(room_id_, lastTimelineItem->descriptionMessage());
else else
nhlog::ui()->warn("cast to TimelineView failed: {}", room_id_.toStdString()); nhlog::ui()->warn("cast to TimelineItem failed: {}", room_id_.toStdString());
} }
void void

View file

@ -169,6 +169,8 @@ private:
//! Mark our own widgets as read if they have more than one receipt. //! Mark our own widgets as read if they have more than one receipt.
void displayReadReceipts(std::vector<TimelineEvent> events); void displayReadReceipts(std::vector<TimelineEvent> events);
//! Determine if the start of the timeline is reached from the response of /messages.
bool isStartOfTimeline(const mtx::responses::Messages &msgs);
QWidget *relativeWidget(QWidget *item, int dt) const; QWidget *relativeWidget(QWidget *item, int dt) const;