Devtools: Implement changing room state
This commit is contained in:
@@ -77,12 +77,14 @@ void StateKeysModel::setEventType(const QString &eventType)
|
||||
loadState();
|
||||
}
|
||||
|
||||
QByteArray StateKeysModel::stateEventJson(const QModelIndex &index)
|
||||
QByteArray StateKeysModel::stateEventJson(const QString &type, const QString &stateKey)
|
||||
{
|
||||
const auto row = index.row();
|
||||
const auto event = m_stateKeys[row];
|
||||
const auto json = event->fullJson();
|
||||
return QJsonDocument(json).toJson();
|
||||
return QJsonDocument(m_room->currentState().get(type, stateKey)->fullJson()).toJson();
|
||||
}
|
||||
|
||||
QByteArray StateKeysModel::stateEventContentJson(const QString &type, const QString &stateKey)
|
||||
{
|
||||
return QJsonDocument(m_room->currentState().get(type, stateKey)->contentJson()).toJson();
|
||||
}
|
||||
|
||||
#include "moc_statekeysmodel.cpp"
|
||||
|
||||
@@ -69,7 +69,12 @@ public:
|
||||
/**
|
||||
* @brief Get the full JSON for an event.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QModelIndex &index);
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QString &type, const QString &stateKey);
|
||||
|
||||
/**
|
||||
* @brief Get the content JSON for an event.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventContentJson(const QString &type, const QString &stateKey);
|
||||
|
||||
Q_SIGNALS:
|
||||
void roomChanged();
|
||||
|
||||
@@ -10,7 +10,11 @@ StateModel::StateModel(QObject *parent)
|
||||
|
||||
QHash<int, QByteArray> StateModel::roleNames() const
|
||||
{
|
||||
return {{TypeRole, "type"}, {EventCountRole, "eventCount"}};
|
||||
return {
|
||||
{TypeRole, "type"},
|
||||
{EventCountRole, "eventCount"},
|
||||
{StateKeyRole, "stateKey"},
|
||||
};
|
||||
}
|
||||
QVariant StateModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
@@ -20,6 +24,8 @@ QVariant StateModel::data(const QModelIndex &index, int role) const
|
||||
return m_stateEvents.keys()[row];
|
||||
case EventCountRole:
|
||||
return m_stateEvents.values()[row].count();
|
||||
case StateKeyRole:
|
||||
return m_stateEvents.values()[row][0];
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -63,14 +69,14 @@ void StateModel::setRoom(NeoChatRoom *room)
|
||||
});
|
||||
}
|
||||
|
||||
QByteArray StateModel::stateEventJson(const QModelIndex &index)
|
||||
QByteArray StateModel::stateEventJson(const QString &type, const QString &stateKey)
|
||||
{
|
||||
auto row = index.row();
|
||||
const auto type = m_stateEvents.keys()[row];
|
||||
const auto stateKey = m_stateEvents.values()[row][0];
|
||||
const auto event = m_room->currentState().get(type, stateKey);
|
||||
return QJsonDocument(m_room->currentState().get(type, stateKey)->fullJson()).toJson();
|
||||
}
|
||||
|
||||
return QJsonDocument(event->fullJson()).toJson();
|
||||
QByteArray StateModel::stateEventContentJson(const QString &type, const QString &stateKey)
|
||||
{
|
||||
return QJsonDocument(m_room->currentState().get(type, stateKey)->contentJson()).toJson();
|
||||
}
|
||||
|
||||
#include "moc_statemodel.cpp"
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
enum Roles {
|
||||
TypeRole = 0, /**< The type of the state event. */
|
||||
EventCountRole, /**< Number of events of this type. */
|
||||
StateKeyRole, /**<State key. Only valid if there's exactly one event of this type. */
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
@@ -62,7 +63,12 @@ public:
|
||||
/**
|
||||
* @brief Get the full JSON for an event.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QModelIndex &index);
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QString &type, const QString &stateKey);
|
||||
|
||||
/**
|
||||
* @brief Get the content JSON for an event.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventContentJson(const QString &type, const QString &stateKey);
|
||||
|
||||
Q_SIGNALS:
|
||||
void roomChanged();
|
||||
@@ -71,7 +77,7 @@ private:
|
||||
QPointer<NeoChatRoom> m_room;
|
||||
|
||||
/**
|
||||
* @brief A map from state event type to number of events of that type
|
||||
* @brief A map from state event type to state keys
|
||||
*/
|
||||
QMap<QString, QList<QString>> m_stateEvents;
|
||||
void loadState();
|
||||
|
||||
Reference in New Issue
Block a user