Reduce nesting

This commit is contained in:
Tobias Fella
2025-08-29 22:20:08 +02:00
parent 5ee5991c39
commit 19e74b60a9

View File

@@ -886,11 +886,12 @@ void NeoChatRoom::addParent(const QString &parentId, bool canonical, bool setPar
setState("m.space.parent"_L1, parentId, QJsonObject{{"canonical"_L1, canonical}, {"via"_L1, QJsonArray{connection()->domain()}}}); setState("m.space.parent"_L1, parentId, QJsonObject{{"canonical"_L1, canonical}, {"via"_L1, QJsonArray{connection()->domain()}}});
if (setParentChild) { if (!setParentChild) {
return;
}
if (auto parent = static_cast<NeoChatRoom *>(connection()->room(parentId))) { if (auto parent = static_cast<NeoChatRoom *>(connection()->room(parentId))) {
parent->setState("m.space.child"_L1, id(), QJsonObject{{"via"_L1, QJsonArray{connection()->domain()}}}); parent->setState("m.space.child"_L1, id(), QJsonObject{{"via"_L1, QJsonArray{connection()->domain()}}});
} }
}
} }
void NeoChatRoom::removeParent(const QString &parentId) void NeoChatRoom::removeParent(const QString &parentId)
@@ -940,25 +941,29 @@ void NeoChatRoom::addChild(const QString &childId, bool setChildParent, bool can
} }
setState("m.space.child"_L1, childId, QJsonObject{{"via"_L1, QJsonArray{connection()->domain()}}, {"suggested"_L1, suggested}, {"order"_L1, order}}); setState("m.space.child"_L1, childId, QJsonObject{{"via"_L1, QJsonArray{connection()->domain()}}, {"suggested"_L1, suggested}, {"order"_L1, order}});
if (setChildParent) { if (!setChildParent) {
return;
}
if (auto child = static_cast<NeoChatRoom *>(connection()->room(childId))) { if (auto child = static_cast<NeoChatRoom *>(connection()->room(childId))) {
if (child->canSendState("m.space.parent"_L1)) { if (!child->canSendState("m.space.parent"_L1)) {
return;
}
child->setState("m.space.parent"_L1, id(), QJsonObject{{"canonical"_L1, canonical}, {"via"_L1, QJsonArray{connection()->domain()}}}); child->setState("m.space.parent"_L1, id(), QJsonObject{{"canonical"_L1, canonical}, {"via"_L1, QJsonArray{connection()->domain()}}});
if (canonical) { if (!canonical) {
return;
}
// Only one canonical parent can exist so make sure others are set to false. // Only one canonical parent can exist so make sure others are set to false.
auto parentEvents = child->currentState().eventsOfType("m.space.parent"_L1); auto parentEvents = child->currentState().eventsOfType("m.space.parent"_L1);
for (const auto &parentEvent : parentEvents) { for (const auto &parentEvent : parentEvents) {
if (parentEvent->contentPart<bool>("canonical"_L1)) { if (!parentEvent->contentPart<bool>("canonical"_L1)) {
continue;
}
auto content = parentEvent->contentJson(); auto content = parentEvent->contentJson();
content.insert("canonical"_L1, false); content.insert("canonical"_L1, false);
setState("m.space.parent"_L1, parentEvent->stateKey(), content); setState("m.space.parent"_L1, parentEvent->stateKey(), content);
} }
} }
}
}
}
}
} }
void NeoChatRoom::removeChild(const QString &childId, bool unsetChildParent) void NeoChatRoom::removeChild(const QString &childId, bool unsetChildParent)
@@ -971,13 +976,15 @@ void NeoChatRoom::removeChild(const QString &childId, bool unsetChildParent)
} }
setState("m.space.child"_L1, childId, {}); setState("m.space.child"_L1, childId, {});
if (unsetChildParent) { if (!unsetChildParent) {
return;
}
if (auto child = static_cast<NeoChatRoom *>(connection()->room(childId))) { if (auto child = static_cast<NeoChatRoom *>(connection()->room(childId))) {
if (child->canSendState("m.space.parent"_L1) && child->currentState().contains("m.space.parent"_L1, id())) { if (!child->canSendState("m.space.parent"_L1) || !child->currentState().contains("m.space.parent"_L1, id())) {
return;
}
child->setState("m.space.parent"_L1, id(), {}); child->setState("m.space.parent"_L1, id(), {});
} }
}
}
} }
bool NeoChatRoom::isSuggested(const QString &childId) bool NeoChatRoom::isSuggested(const QString &childId)