Make various models more robust against deleted rooms

This commit is contained in:
Tobias Fella
2024-03-28 22:28:04 +01:00
parent 158942d1b5
commit e6a11b2ad8
12 changed files with 26 additions and 14 deletions

View File

@@ -80,6 +80,7 @@ QString ActionsHandler::handleMentions(QString handledText, QList<Mention> *ment
void ActionsHandler::handleMessage(const QString &text, QString handledText, ChatBarCache *chatBarCache)
{
Q_ASSERT(m_room);
if (NeoChatConfig::allowQuickEdit()) {
QRegularExpression sed(QStringLiteral("^s/([^/]*)/([^/]*)(/g)?$"));
auto match = sed.match(text);

View File

@@ -58,7 +58,7 @@ public Q_SLOTS:
void handleMessageEvent(ChatBarCache *chatBarCache);
private:
NeoChatRoom *m_room = nullptr;
QPointer<NeoChatRoom> m_room;
void checkEffects(const QString &text);
QString handleMentions(QString handledText, QList<Mention> *mentions);

View File

@@ -91,7 +91,9 @@ QVariant CompletionModel::data(const QModelIndex &index, int role) const
if (mediaId.isEmpty()) {
return QVariant();
}
return m_room->connection()->makeMediaUrl(QUrl(QStringLiteral("mxc://%1").arg(mediaId)));
if (m_room) {
return m_room->connection()->makeMediaUrl(QUrl(QStringLiteral("mxc://%1").arg(mediaId)));
}
}
}
if (m_autoCompletionType == Emoji) {

View File

@@ -118,7 +118,7 @@ private:
QString m_text;
QString m_fullText;
CompletionProxyModel *m_filterModel;
NeoChatRoom *m_room = nullptr;
QPointer<NeoChatRoom> m_room;
AutoCompletionType m_autoCompletionType = None;
void updateCompletion();

View File

@@ -87,7 +87,7 @@ public:
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
private:
NeoChatRoom *m_room = nullptr;
QPointer<NeoChatRoom> m_room;
const Quotient::RoomEvent *m_event = nullptr;
QList<MessageComponent> m_components;

View File

@@ -16,7 +16,7 @@
#include <Quotient/user.h>
ReactionModel::ReactionModel(const Quotient::RoomMessageEvent *event, const NeoChatRoom *room)
ReactionModel::ReactionModel(const Quotient::RoomMessageEvent *event, NeoChatRoom *room)
: QAbstractListModel(nullptr)
, m_room(room)
, m_event(event)

View File

@@ -44,7 +44,7 @@ public:
HasLocalUser, /**< Whether the local user is in the list of authors. */
};
explicit ReactionModel(const Quotient::RoomMessageEvent *event, const NeoChatRoom *room);
explicit ReactionModel(const Quotient::RoomMessageEvent *event, NeoChatRoom *room);
/**
* @brief Get the given role value at the given index.
@@ -68,7 +68,7 @@ public:
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
private:
const NeoChatRoom *m_room;
QPointer<NeoChatRoom> m_room;
const Quotient::RoomMessageEvent *m_event;
QList<Reaction> m_reactions;
QMap<QString, QString> m_shortcodes;

View File

@@ -121,7 +121,7 @@ private:
void setSearching(bool searching);
QString m_searchText;
NeoChatRoom *m_room = nullptr;
QPointer<NeoChatRoom> m_room;
Quotient::Omittable<Quotient::SearchJob::ResultRoomEvents> m_result = Quotient::none;
Quotient::SearchJob *m_job = nullptr;
bool m_searching = false;

View File

@@ -55,11 +55,14 @@ void StateKeysModel::setRoom(NeoChatRoom *room)
m_room = room;
Q_EMIT roomChanged();
loadState();
connect(room, &NeoChatRoom::changed, this, [this] {
if (room) {
loadState();
});
connect(room, &NeoChatRoom::changed, this, [this] {
loadState();
});
}
}
QString StateKeysModel::eventType() const

View File

@@ -76,7 +76,7 @@ Q_SIGNALS:
void eventTypeChanged();
private:
NeoChatRoom *m_room = nullptr;
QPointer<NeoChatRoom> m_room;
QString m_eventType;
QVector<const Quotient::StateEvent *> m_stateKeys;
void loadState();

View File

@@ -22,7 +22,9 @@ QVariant StickerModel::data(const QModelIndex &index, int role) const
const auto &row = index.row();
const auto &image = m_images[row];
if (role == UrlRole) {
return m_room->connection()->makeMediaUrl(image.url);
if (m_room) {
return m_room->connection()->makeMediaUrl(image.url);
}
}
if (role == BodyRole) {
if (image.body) {
@@ -108,6 +110,10 @@ void StickerModel::setRoom(NeoChatRoom *room)
void StickerModel::postSticker(int index)
{
if (!m_room) {
qWarning() << "No room";
}
const auto &image = m_images[index];
const auto &body = image.body ? *image.body : image.shortcode;
QJsonObject infoJson;

View File

@@ -101,6 +101,6 @@ private:
ImagePacksModel *m_model = nullptr;
int m_index = 0;
QList<Quotient::ImagePackEventContent::ImagePackImage> m_images;
NeoChatRoom *m_room;
QPointer<NeoChatRoom> m_room;
void reloadImages();
};