Skip to content

Commit

Permalink
Room::getCurrentState()
Browse files Browse the repository at this point in the history
  • Loading branch information
KitsuneRal committed Oct 1, 2019
1 parent d0a0f8c commit f71d16b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,31 @@ class Room::Private {

void getPreviousContent(int limit = 10);

template <typename EventT>
const EventT* getCurrentState(const QString& stateKey = {}) const
const StateEventBase* getCurrentState(const StateEventKey& evtKey) const
{
const StateEventKey evtKey { EventT::matrixTypeId(), stateKey };
const auto* evt = currentState.value(evtKey, nullptr);
if (!evt) {
if (stubbedState.find(evtKey) == stubbedState.end()) {
// In the absence of a real event, make a stub as-if an event
// with empty content has been received. Event classes should be
// prepared for empty/invalid/malicious content anyway.
stubbedState.emplace(evtKey,
loadStateEvent(EventT::matrixTypeId(), {},
stateKey));
stubbedState.emplace(evtKey, loadStateEvent(evtKey.first, {},
evtKey.second));
qCDebug(STATE) << "A new stub event created for key {"
<< evtKey.first << evtKey.second << "}";
}
evt = stubbedState[evtKey].get();
Q_ASSERT(evt);
}
Q_ASSERT(evt->matrixType() == evtKey.first
&& evt->stateKey() == evtKey.second);
return evt;
}

template <typename EventT>
const EventT* getCurrentState(const QString& stateKey = {}) const
{
const auto* evt = getCurrentState({ EventT::matrixTypeId(), stateKey });
Q_ASSERT(evt->type() == EventT::typeId()
&& evt->matrixType() == EventT::matrixTypeId());
return static_cast<const EventT*>(evt);
Expand Down Expand Up @@ -1105,6 +1111,12 @@ bool Room::usesEncryption() const
return !d->getCurrentState<EncryptionEvent>()->algorithm().isEmpty();
}

const StateEventBase* Room::getCurrentState(const QString& evtType,
const QString& stateKey) const
{
return d->getCurrentState({ evtType, stateKey });
}

RoomEventPtr Room::decryptMessage(EncryptedEvent* encryptedEvent)
{
if (encryptedEvent->algorithm() == OlmV1Curve25519AesSha2AlgoKey) {
Expand Down
18 changes: 18 additions & 0 deletions lib/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,24 @@ class Room : public QObject {

Q_INVOKABLE bool supportsCalls() const;

/// Get a state event with the given event type and state key
/*! This method returns a (potentially empty) state event corresponding
* to the pair of event type \p evtType and state key \p stateKey.
*/
Q_INVOKABLE const StateEventBase*
getCurrentState(const QString& evtType, const QString& stateKey = {}) const;

template <typename EvT>
const EvT* getCurrentState(const QString& stateKey = {}) const
{
const auto* evt =
eventCast<const EvT>(getCurrentState(EvT::matrixTypeId(), stateKey));
Q_ASSERT(evt);
Q_ASSERT(evt->matrixTypeId() == EvT::matrixTypeId()
&& evt->stateKey() == stateKey);
return evt;
}

template <typename EvT, typename... ArgTs>
auto setState(ArgTs&&... args) const
{
Expand Down

0 comments on commit f71d16b

Please sign in to comment.