From fef7cd5b8378a73de686688eb136faeb6246feb0 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 18 Mar 2018 13:29:21 +0200 Subject: [PATCH] Don't count m.room.member or m.room.reaction events as viewable --- include/timeline/TimelineView.h | 6 ++++++ src/timeline/TimelineView.cc | 30 ++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/include/timeline/TimelineView.h b/include/timeline/TimelineView.h index 78000a16..bec23cca 100644 --- a/include/timeline/TimelineView.h +++ b/include/timeline/TimelineView.h @@ -150,10 +150,16 @@ private: void updateLastSender(const QString &user_id, TimelineDirection direction); void notifyForLastEvent(); void notifyForLastEvent(const TimelineEvent &event); + + TimelineEvent findFirstViewableEvent(const std::vector &events); + TimelineEvent findLastViewableEvent(const std::vector &events); + void readLastEvent() const; bool isScrollbarActivated() { return scroll_area_->verticalScrollBar()->value() != 0; } QString getLastEventId() const; QString getEventSender(const mtx::events::collections::TimelineEvents &event) const; + mtx::events::EventType getEventType( + const mtx::events::collections::TimelineEvents &event) const; template TimelineItem *processMessageEvent(const Event &event, TimelineDirection direction); diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc index 2fef0952..16b57df4 100644 --- a/src/timeline/TimelineView.cc +++ b/src/timeline/TimelineView.cc @@ -170,7 +170,7 @@ TimelineView::addBackwardsEvents(const QString &room_id, const mtx::responses::M // is the first batch of messages received through /messages // i.e there are no other messages currently present. if (!topMessages_.empty() && scroll_layout_->count() == 1) - notifyForLastEvent(topMessages_.at(0)); + notifyForLastEvent(findFirstViewableEvent(topMessages_)); if (isVisible()) { renderTopEvents(topMessages_); @@ -313,7 +313,7 @@ TimelineView::addEvents(const mtx::responses::Timeline &timeline) bottomMessages_.push_back(e); if (!bottomMessages_.empty()) - notifyForLastEvent(bottomMessages_[bottomMessages_.size() - 1]); + notifyForLastEvent(findLastViewableEvent(bottomMessages_)); // If the current timeline is open and there are messages to be rendered. if (isVisible() && !bottomMessages_.empty()) { @@ -755,3 +755,29 @@ TimelineView::relativeWidget(TimelineItem *item, int dt) const return isOutOfBounds ? nullptr : scroll_layout_->itemAt(pos)->widget(); } + +TimelineEvent +TimelineView::findFirstViewableEvent(const std::vector &events) +{ + auto it = std::find_if(events.begin(), events.end(), [this](const auto &event) { + return mtx::events::EventType::RoomMessage == getEventType(event); + }); + + return (it == std::end(events)) ? events.front() : *it; +} + +TimelineEvent +TimelineView::findLastViewableEvent(const std::vector &events) +{ + auto it = std::find_if(events.rbegin(), events.rend(), [this](const auto &event) { + return mtx::events::EventType::RoomMessage == getEventType(event); + }); + + return (it == std::rend(events)) ? events.back() : *it; +} + +inline mtx::events::EventType +TimelineView::getEventType(const mtx::events::collections::TimelineEvents &event) const +{ + return mpark::visit([](auto msg) { return msg.type; }, event); +}