[Server] Serve the login message through the database interface (#7854)

This commit is contained in:
BruebachL 2026-11-10 12:16:32 +01:00 • committed by GitHub
parent ec3ad812f0
commit f8f8fffa6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 18 deletions

View file

@ -712,6 +712,11 @@ public:
{
return {};
}
/** @brief The newest server message for the login banner; false when the lookup fails or finds no row. */
virtual bool getLatestServerMessage(int /* serverId */, QString & /* message */)
{
return false;
}
virtual void clearSessionTables()
{

View file

@ -820,26 +820,20 @@ void Servatrice::updateLoginMessage()
return;
}
QSqlQuery *query = servatriceDatabaseInterface->prepareQuery(
"select message from {prefix}_servermessages where id_server = :id_server order by timest desc limit 1");
query->bindValue(":id_server", serverId);
if (servatriceDatabaseInterface->execSqlQuery(query)) {
if (query->next()) {
const QString newLoginMessage = query->value(0).toString();
QString newLoginMessage;
if (servatriceDatabaseInterface->getLatestServerMessage(serverId, newLoginMessage)) {
loginMessageMutex.lock();
loginMessage = newLoginMessage;
loginMessageMutex.unlock();
loginMessageMutex.lock();
loginMessage = newLoginMessage;
loginMessageMutex.unlock();
Event_ServerMessage event;
event.set_message(newLoginMessage.toStdString());
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
QMapIterator<QString, Server_ProtocolHandler *> usersIterator(users);
while (usersIterator.hasNext()) {
usersIterator.next().value()->sendProtocolItem(*se);
}
delete se;
Event_ServerMessage event;
event.set_message(newLoginMessage.toStdString());
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
QMapIterator<QString, Server_ProtocolHandler *> usersIterator(users);
while (usersIterator.hasNext()) {
usersIterator.next().value()->sendProtocolItem(*se);
}
delete se;
}
}

View file

@ -3293,6 +3293,23 @@ Server_DatabaseInterface::UptimeSnapshot Servatrice_DatabaseInterface::getLatest
return snapshot;
}
bool Servatrice_DatabaseInterface::getLatestServerMessage(int serverId, QString &message)
{
if (!checkSql()) {
return false;
}
QSqlQuery *query = prepareQuery(
"select message from {prefix}_servermessages where id_server = :id_server order by timest desc limit 1");
query->bindValue(":id_server", serverId);
if (!execSqlQuery(query) || !query->next()) {
return false;
}
message = query->value(0).toString();
return true;
}
bool Servatrice_DatabaseInterface::removeUserAvatar(const QString &userName)
{
if (!checkSql()) {

View file

@ -278,6 +278,7 @@ public:
// Uptime snapshot as recorded by Servatrice::statusUpdate() into the
// {prefix}_uptime table. valid is false when no snapshot exists yet.
UptimeSnapshot getLatestUptimeSnapshot(int serverId) override;
bool getLatestServerMessage(int serverId, QString &message) override;
bool removeUserAvatar(const QString &userName) override;
// Outcome of removeUserBanner(), so the caller can tell a missing user apart
// from a user who simply has no banner card set.