Refactor error handling and move unrelated functions out of ActionsHandler

This commit is contained in:
Tobias Fella
2021-04-15 16:41:48 +02:00
parent 077844ba61
commit 00681a8abe
5 changed files with 62 additions and 55 deletions

View File

@@ -60,9 +60,6 @@ Kirigami.ApplicationWindow {
property var actionsHandler: ActionsHandler { property var actionsHandler: ActionsHandler {
room: roomManager.currentRoom room: roomManager.currentRoom
connection: Controller.activeConnection connection: Controller.activeConnection
onRoomJoined: {
roomManager.enterRoom(Controller.activeConnection.room(roomName))
}
} }
property var currentRoom: null property var currentRoom: null
@@ -294,6 +291,10 @@ Kirigami.ApplicationWindow {
consentSheet.url = url consentSheet.url = url
consentSheet.open() consentSheet.open()
} }
function onRoomJoined(roomName) {
roomManager.enterRoom(Controller.activeConnection.room(roomName))
}
} }
Connections { Connections {

View File

@@ -9,6 +9,8 @@
#include <QDebug> #include <QDebug>
#include <QStringBuilder> #include <QStringBuilder>
#include "controller.h"
ActionsHandler::ActionsHandler(QObject *parent) ActionsHandler::ActionsHandler(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
@@ -48,7 +50,7 @@ void ActionsHandler::setConnection(Connection *connection)
if (m_connection != nullptr) { if (m_connection != nullptr) {
connect(m_connection, &Connection::directChatAvailable, this, [this](Quotient::Room *room) { connect(m_connection, &Connection::directChatAvailable, this, [this](Quotient::Room *room) {
room->setDisplayed(true); room->setDisplayed(true);
Q_EMIT roomJoined(room->id()); Q_EMIT Controller::instance().roomJoined(room->id());
}); });
} }
Q_EMIT connectionChanged(); Q_EMIT connectionChanged();
@@ -140,35 +142,6 @@ QVariantList ActionsHandler::commands() const
return commands; return commands;
} }
void ActionsHandler::joinRoom(const QString &alias)
{
if (!alias.contains(":")) {
Q_EMIT showMessage(MessageType::Error, i18n("The room id you are trying to join is not valid"));
return;
}
const auto knownServer = alias.mid(alias.indexOf(":") + 1);
auto joinRoomJob = m_connection->joinRoom(alias, QStringList{knownServer});
Quotient::JoinRoomJob::connect(joinRoomJob, &JoinRoomJob::failure, [=] {
Q_EMIT showMessage(MessageType::Error, i18n("Server error when joining the room \"%1\": %2", joinRoomJob->errorString()));
});
Quotient::JoinRoomJob::connect(joinRoomJob, &JoinRoomJob::success, [this, joinRoomJob] {
Q_EMIT roomJoined(joinRoomJob->roomId());
});
}
void ActionsHandler::createRoom(const QString &name, const QString &topic)
{
auto createRoomJob = m_connection->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::failure, [=] {
Q_EMIT showMessage(MessageType::Error, i18n("Room creation failed: \"%1\"", createRoomJob->errorString()));
});
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::success, [=] {
Q_EMIT roomJoined(createRoomJob->roomId());
});
}
void ActionsHandler::postMessage(const QString &text, void ActionsHandler::postMessage(const QString &text,
const QString &attachementPath, const QString &attachementPath,
const QString &replyEventId, const QString &replyEventId,
@@ -265,13 +238,13 @@ void ActionsHandler::postMessage(const QString &text,
return; return;
} }
if (splittedText.count() > 1) { if (splittedText.count() > 1) {
joinRoom(splittedText[0] + ":" + splittedText[1]); Controller::instance().joinRoom(splittedText[0] + ":" + splittedText[1]);
return; return;
} else if (splittedText[0].indexOf(":") != -1) { } else if (splittedText[0].indexOf(":") != -1) {
joinRoom(splittedText[0]); Controller::instance().joinRoom(splittedText[0]);
return; return;
} else { } else {
joinRoom(splittedText[0] + ":matrix.org"); Controller::instance().joinRoom(splittedText[0] + ":matrix.org");
} }
return; return;
} }

View File

@@ -50,22 +50,10 @@ Q_SIGNALS:
/// These messages will be displayed in the room view header. /// These messages will be displayed in the room view header.
void showMessage(MessageType messageType, QString message); void showMessage(MessageType messageType, QString message);
/// \brief Emitted when an action made the user join a room.
///
/// Either when a new room was created, a direct chat was started
/// or a group chat was joined. The UI will react to this signal
/// and switch to the newly joined room.
void roomJoined(QString roomName);
void roomChanged(); void roomChanged();
void connectionChanged(); void connectionChanged();
public Q_SLOTS: public Q_SLOTS:
/// \brief Create new room for a group chat.
void createRoom(const QString &name, const QString &topic);
/// \brief Join a room.
void joinRoom(const QString &alias);
/// \brief Post a message. /// \brief Post a message.
/// ///

View File

@@ -36,6 +36,7 @@
#include "csapi/account-data.h" #include "csapi/account-data.h"
#include "csapi/content-repo.h" #include "csapi/content-repo.h"
#include "csapi/joining.h"
#include "csapi/logout.h" #include "csapi/logout.h"
#include "csapi/profile.h" #include "csapi/profile.h"
#include "csapi/registration.h" #include "csapi/registration.h"
@@ -53,6 +54,8 @@
#include "trayicon.h" #include "trayicon.h"
#endif #endif
using namespace Quotient;
Controller::Controller(QObject *parent) Controller::Controller(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
@@ -159,7 +162,7 @@ void Controller::loginWithAccessToken(const QString &serverAddr, const QString &
setActiveConnection(conn); setActiveConnection(conn);
}); });
connect(conn, &Connection::networkError, this, [=](QString error, const QString &, int, int) { connect(conn, &Connection::networkError, this, [=](QString error, const QString &, int, int) {
Q_EMIT errorOccured("Network Error", std::move(error)); Q_EMIT errorOccured(i18n("Network Error: %1", error));
}); });
conn->connectWithToken(user, token, deviceName); conn->connectWithToken(user, token, deviceName);
} }
@@ -195,7 +198,7 @@ void Controller::logout(Connection *conn, bool serverSideLogout)
} }
auto logoutJob = conn->callApi<LogoutJob>(); auto logoutJob = conn->callApi<LogoutJob>();
connect(logoutJob, &LogoutJob::failure, this, [=] { connect(logoutJob, &LogoutJob::failure, this, [=] {
Q_EMIT errorOccured("Server-side Logout Failed", logoutJob->errorString()); Q_EMIT errorOccured(i18n("Server-side Logout Failed: %1", logoutJob->errorString()));
}); });
} }
@@ -267,16 +270,16 @@ void Controller::invokeLogin()
}); });
connect(connection, &Connection::loginError, this, [=](const QString &error, const QString &) { connect(connection, &Connection::loginError, this, [=](const QString &error, const QString &) {
if (error == "Unrecognised access token") { if (error == "Unrecognised access token") {
Q_EMIT errorOccured(i18n("Login Failed"), i18n("Access Token invalid or revoked")); Q_EMIT errorOccured(i18n("Login Failed: Access Token invalid or revoked"));
logout(connection, false); logout(connection, false);
} else { } else {
Q_EMIT errorOccured(i18n("Login Failed"), error); Q_EMIT errorOccured(i18n("Login Failed", error));
logout(connection, true); logout(connection, true);
} }
Q_EMIT initiated(); Q_EMIT initiated();
}); });
connect(connection, &Connection::networkError, this, [=](const QString &error, const QString &, int, int) { connect(connection, &Connection::networkError, this, [=](const QString &error, const QString &, int, int) {
Q_EMIT errorOccured("Network Error", error); Q_EMIT errorOccured(i18n("Network Error: %1", error));
}); });
connection->connectWithToken(account.userId(), accessToken, account.deviceId()); connection->connectWithToken(account.userId(), accessToken, account.deviceId());
} }
@@ -354,7 +357,7 @@ bool Controller::saveAccessTokenToFile(const AccountSettings &account, const QBy
auto fileDir = QFileInfo(accountTokenFile).dir(); auto fileDir = QFileInfo(accountTokenFile).dir();
if (!((fileDir.exists() || fileDir.mkpath(".")) && accountTokenFile.open(QFile::WriteOnly))) { if (!((fileDir.exists() || fileDir.mkpath(".")) && accountTokenFile.open(QFile::WriteOnly))) {
Q_EMIT errorOccured("I/O Denied", "Cannot save access token."); Q_EMIT errorOccured("I/O Denied: Cannot save access token.");
} else { } else {
accountTokenFile.write(accessToken); accountTokenFile.write(accessToken);
return true; return true;
@@ -558,3 +561,32 @@ NeochatDeleteDeviceJob::NeochatDeleteDeviceJob(const QString &deviceId, const Om
addParam<IfNotEmpty>(_data, QStringLiteral("auth"), auth); addParam<IfNotEmpty>(_data, QStringLiteral("auth"), auth);
setRequestData(std::move(_data)); setRequestData(std::move(_data));
} }
void Controller::createRoom(const QString &name, const QString &topic)
{
auto createRoomJob = m_connection->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::failure, [=] {
Q_EMIT errorOccured(i18n("Room creation failed: \"%1\"", createRoomJob->errorString()));
});
Quotient::CreateRoomJob::connect(createRoomJob, &CreateRoomJob::success, [=] {
Q_EMIT roomJoined(createRoomJob->roomId());
});
}
void Controller::joinRoom(const QString &alias)
{
if (!alias.contains(":")) {
Q_EMIT errorOccured(i18n("The room id you are trying to join is not valid"));
return;
}
const auto knownServer = alias.mid(alias.indexOf(":") + 1);
auto joinRoomJob = m_connection->joinRoom(alias, QStringList{knownServer});
connect(joinRoomJob, &JoinRoomJob::failure, [=] {
Q_EMIT errorOccured(i18n("Server error when joining the room \"%1\": %2", joinRoomJob->errorString()));
});
connect(joinRoomJob, &JoinRoomJob::success, [this, joinRoomJob] {
Q_EMIT errorOccured(joinRoomJob->roomId());
});
}

View File

@@ -72,6 +72,12 @@ public:
}; };
Q_ENUM(PasswordStatus); Q_ENUM(PasswordStatus);
/// \brief Create new room for a group chat.
void createRoom(const QString &name, const QString &topic);
/// \brief Join a room.
void joinRoom(const QString &alias);
private: private:
explicit Controller(QObject *parent = nullptr); explicit Controller(QObject *parent = nullptr);
~Controller() override; ~Controller() override;
@@ -94,7 +100,14 @@ private Q_SLOTS:
Q_SIGNALS: Q_SIGNALS:
void busyChanged(); void busyChanged();
/// Error occurred because of user inputs /// Error occurred because of user inputs
void errorOccured(QString error, QString detail); void errorOccured(const QString &error);
/// \brief Emitted when an action made the user join a room.
///
/// Either when a new room was created, a direct chat was started
/// or a group chat was joined. The UI will react to this signal
/// and switch to the newly joined room.
void roomJoined(const QString &roomName);
/// Error occurred because of server or bug in NeoChat /// Error occurred because of server or bug in NeoChat
void globalErrorOccured(QString error, QString detail); void globalErrorOccured(QString error, QString detail);