Compare commits

...

1 Commits

Author SHA1 Message Date
Joshua Goins
fb63ac5642 Fix a crash when sitting around with no room open
This doesn't check if m_room is null before accessing it's connection.
You can easily reproduce this by sitting around leaving NeoChat open
without entering a room.
2024-09-06 04:55:12 -04:00
2 changed files with 25 additions and 28 deletions

View File

@@ -578,22 +578,21 @@ MessageComponent MessageContentModel::linkPreviewComponent(const QUrl &link)
} }
if (linkPreviewer->loaded()) { if (linkPreviewer->loaded()) {
return MessageComponent{MessageComponentType::LinkPreview, QString(), {{"link"_ls, link}}}; return MessageComponent{MessageComponentType::LinkPreview, QString(), {{"link"_ls, link}}};
} else { }
connect(linkPreviewer, &LinkPreviewer::loadedChanged, [this, link]() { connect(linkPreviewer, &LinkPreviewer::loadedChanged, this, [this, link]() {
const auto linkPreviewer = dynamic_cast<NeoChatConnection *>(m_room->connection())->previewerForLink(link); const auto linkPreviewer = dynamic_cast<NeoChatConnection *>(m_room->connection())->previewerForLink(link);
if (linkPreviewer != nullptr && linkPreviewer->loaded()) { if (linkPreviewer != nullptr && linkPreviewer->loaded()) {
for (auto &component : m_components) { for (auto &component : m_components) {
if (component.attributes["link"_ls].toUrl() == link) { if (component.attributes["link"_ls].toUrl() == link) {
// HACK: Because DelegateChooser can't switch the delegate on dataChanged it has to think there is a new delegate. // HACK: Because DelegateChooser can't switch the delegate on dataChanged it has to think there is a new delegate.
beginResetModel(); beginResetModel();
component.type = MessageComponentType::LinkPreview; component.type = MessageComponentType::LinkPreview;
endResetModel(); endResetModel();
}
} }
} }
}); }
return MessageComponent{MessageComponentType::LinkPreviewLoad, QString(), {{"link"_ls, link}}}; });
} return MessageComponent{MessageComponentType::LinkPreviewLoad, QString(), {{"link"_ls, link}}};
} }
QList<MessageComponent> MessageContentModel::addLinkPreviews(QList<MessageComponent> inputComponents) QList<MessageComponent> MessageContentModel::addLinkPreviews(QList<MessageComponent> inputComponents)

View File

@@ -84,21 +84,19 @@ void MessageEventModel::setRoom(NeoChatRoom *room)
return; return;
} }
if (m_currentRoom) { // HACK: Reset the model to a null room first to make sure QML dismantles
// HACK: Reset the model to a null room first to make sure QML dismantles // last room's objects before the room is actually changed
// last room's objects before the room is actually changed beginResetModel();
beginResetModel(); m_currentRoom->disconnect(this);
m_currentRoom->disconnect(this); m_currentRoom = nullptr;
m_currentRoom = nullptr; endResetModel();
endResetModel();
// Don't clear the member objects until the model has been fully reset and all // Don't clear the member objects until the model has been fully reset and all
// refs cleared. // refs cleared.
m_memberObjects.clear(); m_memberObjects.clear();
m_contentModels.clear(); m_contentModels.clear();
m_reactionModels.clear(); m_reactionModels.clear();
m_readMarkerModels.clear(); m_readMarkerModels.clear();
}
beginResetModel(); beginResetModel();
m_currentRoom = room; m_currentRoom = room;