[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.
This commit is contained in:
BruebachL 2026-11-10 12:16:20 +01:00 • committed by GitHub
parent 90b3013db6
commit 52d8f84ec8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 77 additions and 12 deletions

View file

@ -77,6 +77,27 @@ public:
{ {
return false; 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 ServerInfo_User getUserData(const QString &name, bool withId = false) = 0;
virtual void storeGameInformation(const QString & /* roomName */, virtual void storeGameInformation(const QString & /* roomName */,
const QStringList & /* roomGameTypes */, const QStringList & /* roomGameTypes */,

View file

@ -991,6 +991,47 @@ bool Servatrice_DatabaseInterface::isInIgnoreList(const QString &whoseList, cons
return query->next(); 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 Servatrice_DatabaseInterface::evalUserQueryResult(const QSqlQuery *query, bool complete, bool withId)
{ {
ServerInfo_User result; ServerInfo_User result;

View file

@ -67,6 +67,8 @@ private:
* backup key is unset or the sealed value does not fit the column. */ * backup key is unset or the sealed value does not fit the column. */
QString sealLegacyBackup(const QString &storedName, const QString &credential); QString sealLegacyBackup(const QString &storedName, const QString &credential);
bool isStrictModeEnabled(bool &ok) const; bool isStrictModeEnabled(bool &ok) const;
bool addToList(const QString &table, int userId, int targetId);
bool removeFromList(const QString &table, int userId, int targetId);
protected: protected:
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler,
@ -113,6 +115,11 @@ public:
/** @brief Drop every legacy password backup that has not been used within the TTL. */ /** @brief Drop every legacy password backup that has not been used within the TTL. */
void purgeExpiredLegacyBackups(); void purgeExpiredLegacyBackups();
int getUserIdInDB(const QString &name); 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<QString, ServerInfo_User> getBuddyList(const QString &name) override; QMap<QString, ServerInfo_User> getBuddyList(const QString &name) override;
QMap<QString, ServerInfo_User> getIgnoreList(const QString &name) override; QMap<QString, ServerInfo_User> getIgnoreList(const QString &name) override;
bool isInBuddyList(const QString &whoseList, const QString &who) override; bool isInBuddyList(const QString &whoseList, const QString &who) override;

View file

@ -563,7 +563,7 @@ Response::ResponseCode AbstractServerSocketInterface::cmdAddToList(const Command
} }
int id1 = userInfo->id(); int id1 = userInfo->id();
int id2 = sqlInterface->getUserIdInDB(user); int id2 = databaseInterface->getUserId(user);
if (id2 < 0) { if (id2 < 0) {
return Response::RespNameNotFound; return Response::RespNameNotFound;
} }
@ -571,11 +571,9 @@ Response::ResponseCode AbstractServerSocketInterface::cmdAddToList(const Command
return Response::RespContextError; return Response::RespContextError;
} }
QSqlQuery *query = const bool stored =
sqlInterface->prepareQuery("insert into {prefix}_" + list + "list (id_user1, id_user2) values(:id1, :id2)"); list == "buddy" ? databaseInterface->addToBuddyList(id1, id2) : databaseInterface->addToIgnoreList(id1, id2);
query->bindValue(":id1", id1); if (!stored) {
query->bindValue(":id2", id2);
if (!sqlInterface->execSqlQuery(query)) {
return Response::RespInternalError; return Response::RespInternalError;
} }
@ -617,16 +615,14 @@ Response::ResponseCode AbstractServerSocketInterface::cmdRemoveFromList(const Co
} }
int id1 = userInfo->id(); int id1 = userInfo->id();
int id2 = sqlInterface->getUserIdInDB(user); int id2 = databaseInterface->getUserId(user);
if (id2 < 0) { if (id2 < 0) {
return Response::RespNameNotFound; return Response::RespNameNotFound;
} }
QSqlQuery *query = const bool removed = list == "buddy" ? databaseInterface->removeFromBuddyList(id1, id2)
sqlInterface->prepareQuery("delete from {prefix}_" + list + "list where id_user1 = :id1 and id_user2 = :id2"); : databaseInterface->removeFromIgnoreList(id1, id2);
query->bindValue(":id1", id1); if (!removed) {
query->bindValue(":id2", id2);
if (!sqlInterface->execSqlQuery(query)) {
return Response::RespInternalError; return Response::RespInternalError;
} }