From 5bdfd6545c2c116c9260750cbc46ddc37eb455ef Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 9 Jul 2018 14:45:24 +0900 Subject: Room: Revert from visit() to a combination of ifs and eventCasts Easier to read, and also easier compiler diagnostics if things go wrong. Still using visit() for state events processing though - it maintains that all lambdas return some value. --- lib/room.cpp | 146 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index dc9f5ccc..06a0a380 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -1530,92 +1530,92 @@ bool Room::processStateEvent(const RoomEvent& e) void Room::processEphemeralEvent(EventPtr&& event) { QElapsedTimer et; et.start(); - visit(*event - , [this,et] (const TypingEvent& evt) { - d->usersTyping.clear(); - for( const QString& userId: qAsConst(evt.users()) ) + if (auto* evt = eventCast(event)) + { + d->usersTyping.clear(); + for( const QString& userId: qAsConst(evt->users()) ) + { + auto u = user(userId); + if (memberJoinState(u) == JoinState::Join) + d->usersTyping.append(u); + } + if (!evt->users().isEmpty()) + qCDebug(PROFILER) << "*** Room::processEphemeralEvent(typing):" + << evt->users().size() << "users," << et; + emit typingChanged(); + } + if (auto* evt = eventCast(event)) + { + for( const auto &p: qAsConst(evt->eventsWithReceipts()) ) + { { - auto u = user(userId); - if (memberJoinState(u) == JoinState::Join) - d->usersTyping.append(u); + if (p.receipts.size() == 1) + qCDebug(EPHEMERAL) << "Marking" << p.evtId + << "as read for" << p.receipts[0].userId; + else + qCDebug(EPHEMERAL) << "Marking" << p.evtId + << "as read for" << p.receipts.size() << "users"; } - if (!evt.users().isEmpty()) - qCDebug(PROFILER) << "*** Room::processEphemeralEvent(typing):" - << evt.users().size() << "users," << et; - emit typingChanged(); - } - , [this,et] (const ReceiptEvent& evt) { - for( const auto &p: qAsConst(evt.eventsWithReceipts()) ) + const auto newMarker = findInTimeline(p.evtId); + if (newMarker != timelineEdge()) { + for( const Receipt& r: p.receipts ) { - if (p.receipts.size() == 1) - qCDebug(EPHEMERAL) << "Marking" << p.evtId - << "as read for" << p.receipts[0].userId; - else - qCDebug(EPHEMERAL) << "Marking" << p.evtId - << "as read for" << p.receipts.size() << "users"; + if (r.userId == connection()->userId()) + continue; // FIXME, #185 + auto u = user(r.userId); + if (memberJoinState(u) == JoinState::Join) + d->promoteReadMarker(u, newMarker); } - const auto newMarker = findInTimeline(p.evtId); - if (newMarker != timelineEdge()) - { - for( const Receipt& r: p.receipts ) - { - if (r.userId == connection()->userId()) - continue; // FIXME, #185 - auto u = user(r.userId); - if (memberJoinState(u) == JoinState::Join) - d->promoteReadMarker(u, newMarker); - } - } else + } else + { + qCDebug(EPHEMERAL) << "Event" << p.evtId + << "not found; saving read receipts anyway"; + // If the event is not found (most likely, because it's too old + // and hasn't been fetched from the server yet), but there is + // a previous marker for a user, keep the previous marker. + // Otherwise, blindly store the event id for this user. + for( const Receipt& r: p.receipts ) { - qCDebug(EPHEMERAL) << "Event" << p.evtId - << "not found; saving read receipts anyway"; - // If the event is not found (most likely, because it's too old - // and hasn't been fetched from the server yet), but there is - // a previous marker for a user, keep the previous marker. - // Otherwise, blindly store the event id for this user. - for( const Receipt& r: p.receipts ) - { - if (r.userId == connection()->userId()) - continue; // FIXME, #185 - auto u = user(r.userId); - if (memberJoinState(u) == JoinState::Join && - readMarker(u) == timelineEdge()) - d->setLastReadEvent(u, p.evtId); - } + if (r.userId == connection()->userId()) + continue; // FIXME, #185 + auto u = user(r.userId); + if (memberJoinState(u) == JoinState::Join && + readMarker(u) == timelineEdge()) + d->setLastReadEvent(u, p.evtId); } } - if (!evt.eventsWithReceipts().isEmpty()) - qCDebug(PROFILER) << "*** Room::processEphemeralEvent(receipts):" - << evt.eventsWithReceipts().size() - << "events with receipts," << et; } - ); + if (!evt->eventsWithReceipts().isEmpty()) + qCDebug(PROFILER) << "*** Room::processEphemeralEvent(receipts):" + << evt->eventsWithReceipts().size() + << "events with receipts," << et; + } } void Room::processAccountDataEvent(EventPtr&& event) { - visit(*event - , [this] (const TagEvent& evt) { - auto newTags = evt.tags(); - if (newTags == d->tags) - return; - d->tags = newTags; - qCDebug(MAIN) << "Room" << id() << "is tagged with:" - << tagNames().join(", "); - emit tagsChanged(); - } - , [this] (const ReadMarkerEvent& evt) { - auto readEventId = evt.event_id(); - qCDebug(MAIN) << "Server-side read marker at" << readEventId; - d->serverReadMarker = readEventId; - const auto newMarker = findInTimeline(readEventId); - if (newMarker != timelineEdge()) - d->markMessagesAsRead(newMarker); - else - d->setLastReadEvent(localUser(), readEventId); - } - ); + if (auto* evt = eventCast(event)) + { + auto newTags = evt->tags(); + if (newTags == d->tags) + return; + d->tags = newTags; + qCDebug(MAIN) << "Room" << id() << "is tagged with:" + << tagNames().join(", "); + emit tagsChanged(); + } + if (auto* evt = eventCast(event)) + { + auto readEventId = evt->event_id(); + qCDebug(MAIN) << "Server-side read marker at" << readEventId; + d->serverReadMarker = readEventId; + const auto newMarker = findInTimeline(readEventId); + if (newMarker != timelineEdge()) + d->markMessagesAsRead(newMarker); + else + d->setLastReadEvent(localUser(), readEventId); + } // For all account data events auto& currentData = d->accountData[event->matrixType()]; // A polymorphic event-specific comparison might be a bit more -- cgit v1.2.3