mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-10 03:53:26 -08:00
[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:
parent
90b3013db6
commit
52d8f84ec8
4 changed files with 77 additions and 12 deletions
|
|
@ -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 */,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<QString, ServerInfo_User> getBuddyList(const QString &name) override;
|
||||
QMap<QString, ServerInfo_User> getIgnoreList(const QString &name) override;
|
||||
bool isInBuddyList(const QString &whoseList, const QString &who) override;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue