Skip to content

Commit

Permalink
QuaternionRoom::getSingleEvent()
Browse files Browse the repository at this point in the history
Also fixes the current FTBFS due to a part of this functionality
sneaking into a previous commit. Otherwise, it's unused for now, will be
used in subsequent commits.
  • Loading branch information
KitsuneRal committed Jan 17, 2025
1 parent 83052d7 commit a787c57
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
39 changes: 38 additions & 1 deletion client/quaternionroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#include "logging_categories.h"

#include <Quotient/user.h>
#include <Quotient/events/roommessageevent.h>

#include <Quotient/user.h>

#include <QtCore/QRegularExpression>

#include <ranges>
Expand Down Expand Up @@ -88,6 +90,41 @@ bool QuaternionRoom::canRedact(const Quotient::EventId& eventId) const
return false;
}

void QuaternionRoom::onGettingSingleEvent(const QString& evtId)
{
std::erase_if(singleEventRequests, [this, evtId](SingleEventRequest& r) {
if (!r.requestHandle.isFinished())
r.requestHandle.abandon();
std::ranges::for_each(r.eventIdsToRefresh, std::bind_front(&Room::updatedEvent, this));
return r.eventId == evtId;
});
}

const RoomEvent* QuaternionRoom::getSingleEvent(const QString& eventId, const QString& originEventId)
{
if (auto timelineIt = findInTimeline(eventId); timelineIt != historyEdge())
return timelineIt->event();
if (auto cachedIt = cachedEvents.find(eventId); cachedIt != cachedEvents.cend())
return cachedIt->second.get();

auto requestIt = std::ranges::find(singleEventRequests, eventId, &SingleEventRequest::eventId);
if (requestIt == singleEventRequests.cend())
requestIt = singleEventRequests.insert(
requestIt,
{ eventId, connection()
->callApi<GetOneRoomEventJob>(id(), eventId)
.then([this](RoomEventPtr&& pEvt) {
const auto [it, cachedEventInserted] =
cachedEvents.insert_or_assign(pEvt->id(), std::move(pEvt));
if (QUO_ALARM(!cachedEventInserted))
emit updatedEvent(it->first); // At least notify clients...
onGettingSingleEvent(it->first);
}) });
requestIt->eventIdsToRefresh.push_back(originEventId);

return nullptr;
}

void QuaternionRoom::onAddNewTimelineEvents(timeline_iter_t from)
{
std::for_each(from, messageEvents().cend(),
Expand Down
23 changes: 20 additions & 3 deletions client/quaternionroom.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#pragma once

#include <Quotient/csapi/rooms.h>

#include <Quotient/room.h>

#include <QtCore/QDeadlineTimer>
Expand All @@ -16,6 +18,8 @@ class QuaternionRoom: public Quotient::Room
{
Q_OBJECT
public:
using RoomEvent = Quotient::RoomEvent;

QuaternionRoom(Quotient::Connection* connection, QString roomId,
Quotient::JoinState joinState);

Expand Down Expand Up @@ -46,7 +50,12 @@ class QuaternionRoom: public Quotient::Room
//! is not found
Q_INVOKABLE EventFuture ensureHistory(const QString& upToEventId, quint16 maxWaitSeconds = 20);

private:
//! \brief Obtain an arbitrary room event by its id that is available locally
//!
Q_INVOKABLE const Quotient::RoomEvent* getSingleEvent(const QString& eventId,
const QString& originEventId);

private:
using EventPromise = QPromise<void>;
using EventId = Quotient::EventId;

Expand All @@ -57,13 +66,21 @@ class QuaternionRoom: public Quotient::Room
};
std::vector<HistoryRequest> historyRequests;

QSet<const Quotient::RoomEvent*> highlights;
struct SingleEventRequest {
EventId eventId;
Quotient::JobHandle<Quotient::GetOneRoomEventJob> requestHandle;
std::vector<QString> eventIdsToRefresh{};
};
std::vector<SingleEventRequest> singleEventRequests;
std::unordered_map<EventId, Quotient::event_ptr_tt<const RoomEvent>> cachedEvents;

QSet<const RoomEvent*> highlights;
QString m_cachedUserFilter;
int m_requestedEventsCount = 0;

void onAddNewTimelineEvents(timeline_iter_t from) override;
void onAddHistoricalTimelineEvents(rev_iter_t from) override;

void checkForHighlights(const Quotient::TimelineItem& ti);
void checkForRequestedEvents(const rev_iter_t& from);
void onGettingSingleEvent(const QString& evtId);
};

0 comments on commit a787c57

Please sign in to comment.