From 52d8f84ec87100126eeecb19584727519db7c8e5 Mon Sep 17 00:00:00 2001 From: BruebachL <44814898+BruebachL@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:16:20 +0100 Subject: [PATCH] [Server] Move buddy and ignore list writes into the database interface (#7827) cmdAddToList/cmdRemoveFromList issued raw SQL against the buddy and ignore tables and looked accounts up through getUserIdInDB, a method that only exists on the MySQL implementation. Add getUserId plus add/remove operations to Server_DatabaseInterface, implement them in Servatrice_DatabaseInterface over the shared single-row helpers, and let the handler ask the injected interface instead of the concrete class. --- .../server/remote/server_database_interface.h | 21 ++++++++++ .../src/servatrice_database_interface.cpp | 41 +++++++++++++++++++ .../src/servatrice_database_interface.h | 7 ++++ servatrice/src/serversocketinterface.cpp | 20 ++++----- 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/server_database_interface.h b/libcockatrice_network/libcockatrice/network/server/remote/server_database_interface.h index 936c48880..4000e453d 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/server_database_interface.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/server_database_interface.h @@ -77,6 +77,27 @@ public: { return false; } + /** @brief Numeric id of an existing account, or a negative value when absent. */ + virtual int getUserId(const QString & /* userName */) + { + return -1; + } + virtual bool addToBuddyList(int /* userId */, int /* targetId */) + { + return false; + } + virtual bool removeFromBuddyList(int /* userId */, int /* targetId */) + { + return false; + } + virtual bool addToIgnoreList(int /* userId */, int /* targetId */) + { + return false; + } + virtual bool removeFromIgnoreList(int /* userId */, int /* targetId */) + { + return false; + } virtual ServerInfo_User getUserData(const QString &name, bool withId = false) = 0; virtual void storeGameInformation(const QString & /* roomName */, const QStringList & /* roomGameTypes */, diff --git a/servatrice/src/servatrice_database_interface.cpp b/servatrice/src/servatrice_database_interface.cpp index c11f63363..7b5942920 100644 --- a/servatrice/src/servatrice_database_interface.cpp +++ b/servatrice/src/servatrice_database_interface.cpp @@ -991,6 +991,47 @@ bool Servatrice_DatabaseInterface::isInIgnoreList(const QString &whoseList, cons return query->next(); } +int Servatrice_DatabaseInterface::getUserId(const QString &name) +{ + return getUserIdInDB(name); +} + +bool Servatrice_DatabaseInterface::addToBuddyList(int userId, int targetId) +{ + return addToList("buddylist", userId, targetId); +} + +bool Servatrice_DatabaseInterface::removeFromBuddyList(int userId, int targetId) +{ + return removeFromList("buddylist", userId, targetId); +} + +bool Servatrice_DatabaseInterface::addToIgnoreList(int userId, int targetId) +{ + return addToList("ignorelist", userId, targetId); +} + +bool Servatrice_DatabaseInterface::removeFromIgnoreList(int userId, int targetId) +{ + return removeFromList("ignorelist", userId, targetId); +} + +bool Servatrice_DatabaseInterface::addToList(const QString &table, int userId, int targetId) +{ + QSqlQuery *query = prepareQuery("insert into {prefix}_" + table + " (id_user1, id_user2) values(:id1, :id2)"); + query->bindValue(":id1", userId); + query->bindValue(":id2", targetId); + return execSqlQuery(query); +} + +bool Servatrice_DatabaseInterface::removeFromList(const QString &table, int userId, int targetId) +{ + QSqlQuery *query = prepareQuery("delete from {prefix}_" + table + " where id_user1 = :id1 and id_user2 = :id2"); + query->bindValue(":id1", userId); + query->bindValue(":id2", targetId); + return execSqlQuery(query); +} + ServerInfo_User Servatrice_DatabaseInterface::evalUserQueryResult(const QSqlQuery *query, bool complete, bool withId) { ServerInfo_User result; diff --git a/servatrice/src/servatrice_database_interface.h b/servatrice/src/servatrice_database_interface.h index 13a581673..2a6e6a966 100644 --- a/servatrice/src/servatrice_database_interface.h +++ b/servatrice/src/servatrice_database_interface.h @@ -67,6 +67,8 @@ private: * backup key is unset or the sealed value does not fit the column. */ QString sealLegacyBackup(const QString &storedName, const QString &credential); bool isStrictModeEnabled(bool &ok) const; + bool addToList(const QString &table, int userId, int targetId); + bool removeFromList(const QString &table, int userId, int targetId); protected: AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, @@ -113,6 +115,11 @@ public: /** @brief Drop every legacy password backup that has not been used within the TTL. */ void purgeExpiredLegacyBackups(); int getUserIdInDB(const QString &name); + int getUserId(const QString &name) override; + bool addToBuddyList(int userId, int targetId) override; + bool removeFromBuddyList(int userId, int targetId) override; + bool addToIgnoreList(int userId, int targetId) override; + bool removeFromIgnoreList(int userId, int targetId) override; QMap getBuddyList(const QString &name) override; QMap getIgnoreList(const QString &name) override; bool isInBuddyList(const QString &whoseList, const QString &who) override; diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index 88fb8e0cc..9678efeb6 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -563,7 +563,7 @@ Response::ResponseCode AbstractServerSocketInterface::cmdAddToList(const Command } int id1 = userInfo->id(); - int id2 = sqlInterface->getUserIdInDB(user); + int id2 = databaseInterface->getUserId(user); if (id2 < 0) { return Response::RespNameNotFound; } @@ -571,11 +571,9 @@ Response::ResponseCode AbstractServerSocketInterface::cmdAddToList(const Command return Response::RespContextError; } - QSqlQuery *query = - sqlInterface->prepareQuery("insert into {prefix}_" + list + "list (id_user1, id_user2) values(:id1, :id2)"); - query->bindValue(":id1", id1); - query->bindValue(":id2", id2); - if (!sqlInterface->execSqlQuery(query)) { + const bool stored = + list == "buddy" ? databaseInterface->addToBuddyList(id1, id2) : databaseInterface->addToIgnoreList(id1, id2); + if (!stored) { return Response::RespInternalError; } @@ -617,16 +615,14 @@ Response::ResponseCode AbstractServerSocketInterface::cmdRemoveFromList(const Co } int id1 = userInfo->id(); - int id2 = sqlInterface->getUserIdInDB(user); + int id2 = databaseInterface->getUserId(user); if (id2 < 0) { return Response::RespNameNotFound; } - QSqlQuery *query = - sqlInterface->prepareQuery("delete from {prefix}_" + list + "list where id_user1 = :id1 and id_user2 = :id2"); - query->bindValue(":id1", id1); - query->bindValue(":id2", id2); - if (!sqlInterface->execSqlQuery(query)) { + const bool removed = list == "buddy" ? databaseInterface->removeFromBuddyList(id1, id2) + : databaseInterface->removeFromIgnoreList(id1, id2); + if (!removed) { return Response::RespInternalError; }