Fix ZeldaZach review for PR 7496: deck path in ctor, server-time baseline, single-query sync check

This commit is contained in:
Lukas Brübach 2026-10-13 07:21:42 +02:00
parent 477605d1e0
commit 1433af321d
6 changed files with 30 additions and 41 deletions

View file

@ -299,11 +299,9 @@ void DeckSyncManager::syncNow()
.modified = QFileInfo(absolutePath).lastModified()}); .modified = QFileInfo(absolutePath).lastModified()});
} }
if (!firstSyncRun && sidecar.lastSyncSecs > 0) { // Always ask the server first: its server_time becomes the sync baseline,
sendSyncCheck(sidecar.lastSyncSecs); // so uploads racing the DeckList response cannot fall outside the window.
return; sendSyncCheck(sidecar.lastSyncSecs);
}
fetchFullList();
} }
void DeckSyncManager::fetchFullList() void DeckSyncManager::fetchFullList()
@ -334,6 +332,9 @@ void DeckSyncManager::onCheckFinished(const Response &response, const CommandCon
return; return;
} }
const Response_DeckSyncStatus &status = response.GetExtension(Response_DeckSyncStatus::ext); const Response_DeckSyncStatus &status = response.GetExtension(Response_DeckSyncStatus::ext);
if (status.has_server_time()) {
pendingServerTime = static_cast<qint64>(status.server_time());
}
DeckSyncSidecar sidecar(sidecarPath); DeckSyncSidecar sidecar(sidecarPath);
if (!sidecar.load()) { if (!sidecar.load()) {
fetchFullList(); fetchFullList();

View file

@ -260,6 +260,13 @@ TabDeckStorage::TabDeckStorage(TabSupervisor *_tabSupervisor,
syncManager = new DeckSyncManager( syncManager = new DeckSyncManager(
client, [](const DeckList &deck) { return getDeckColorIdentity(deck, CardDatabaseManager::query()); }, this); client, [](const DeckList &deck) { return getDeckColorIdentity(deck, CardDatabaseManager::query()); }, this);
syncManager->setDeckPath(SettingsCache::instance().paths().getDeckPath());
connect(&SettingsCache::instance().paths(), &PathsSettings::deckPathChanged, this, [this] {
const QString deckPath = SettingsCache::instance().paths().getDeckPath();
localDirModel->setRootPath(deckPath);
localDirView->setRootIndex(localDirModel->index(localDirModel->rootPath(), 0));
syncManager->setDeckPath(deckPath);
});
syncManager->setConflictDefault(SettingsCache::instance().deckSync().getConflictDefault()); syncManager->setConflictDefault(SettingsCache::instance().deckSync().getConflictDefault());
syncManager->setAutoSyncEnabled(SettingsCache::instance().deckSync().getAutoSync()); syncManager->setAutoSyncEnabled(SettingsCache::instance().deckSync().getAutoSync());
syncManager->setSyncIntervalMinutes(SettingsCache::instance().deckSync().getSyncIntervalMinutes()); syncManager->setSyncIntervalMinutes(SettingsCache::instance().deckSync().getSyncIntervalMinutes());

View file

@ -71,6 +71,7 @@ QString PathsSettings::getRedirectCachePath() const
void PathsSettings::setDeckPath(const QString &_deckPath) void PathsSettings::setDeckPath(const QString &_deckPath)
{ {
setValue(_deckPath, "decks"); setValue(_deckPath, "decks");
emit deckPathChanged();
} }
void PathsSettings::setFiltersPath(const QString &_filtersPath) void PathsSettings::setFiltersPath(const QString &_filtersPath)

View file

@ -59,6 +59,7 @@ public:
void setAppDirWarningAcknowledged(bool _appDirWarningAcknowledged); void setAppDirWarningAcknowledged(bool _appDirWarningAcknowledged);
signals: signals:
void deckPathChanged();
void cardDatabasePathChanged(); void cardDatabasePathChanged();
void picsPathChanged(); void picsPathChanged();
void themeChanged(); void themeChanged();

View file

@ -898,51 +898,31 @@ Response::ResponseCode AbstractServerSocketInterface::cmdDeckSetVisibility(const
return Response::RespOk; return Response::RespOk;
} }
bool AbstractServerSocketInterface::syncCheckHelper(int folderId, bool AbstractServerSocketInterface::syncCheckHelper(int userId, qint64 sinceSecs, Response_DeckSyncStatus *status)
const QString &prefix,
int userId,
qint64 sinceSecs,
Response_DeckSyncStatus *status)
{ {
QSqlQuery *query = sqlInterface->prepareQuery("select id, name from {prefix}_decklist_folders where id_parent = " // Single query over the user's files: folder rows are hard-deleted while
":id_parent and id_user = :id_user"); // their files linger as tombstones, so walking the live folder tree would
query->bindValue(":id_parent", folderId); // miss deletes pointing at nonexistent folders. The client only checks
query->bindValue(":id_user", userId); // changed_files_size(), so folder paths are not reconstructed here.
if (!sqlInterface->execSqlQuery(query)) { QSqlQuery *query = sqlInterface->prepareQuery("select id, guid, hash, last_modified, deleted from "
return false; "{prefix}_decklist_files where id_user = :id_user "
} "and (last_modified is null or last_modified >= "
QList<std::pair<int, QString>> subfolders; "FROM_UNIXTIME(:since))");
while (query->next()) {
subfolders.append({query->value(0).toInt(), query->value(1).toString()});
}
for (const auto &[subfolderId, subfolderName] : subfolders) {
const QString path = prefix.isEmpty() ? subfolderName : prefix + "/" + subfolderName;
if (!syncCheckHelper(subfolderId, path, userId, sinceSecs, status)) {
return false;
}
}
query = sqlInterface->prepareQuery("select id, name, guid, hash, last_modified, deleted from "
"{prefix}_decklist_files where id_folder = :id_folder and id_user = :id_user "
"and (last_modified is null or last_modified > FROM_UNIXTIME(:since))");
query->bindValue(":id_folder", folderId);
query->bindValue(":id_user", userId); query->bindValue(":id_user", userId);
query->bindValue(":since", sinceSecs); query->bindValue(":since", sinceSecs);
if (!sqlInterface->execSqlQuery(query)) { if (!sqlInterface->execSqlQuery(query)) {
return false; return false;
} }
while (query->next()) { while (query->next()) {
const QString name = query->value(1).toString();
DeckSyncChangedFile *changed = status->add_changed_files(); DeckSyncChangedFile *changed = status->add_changed_files();
changed->set_path((prefix.isEmpty() ? name : prefix + "/" + name).toStdString());
changed->set_deck_id(query->value(0).toUInt()); changed->set_deck_id(query->value(0).toUInt());
changed->set_guid(query->value(2).toString().toStdString()); changed->set_guid(query->value(1).toString().toStdString());
changed->set_hash(query->value(3).toString().toStdString()); changed->set_hash(query->value(2).toString().toStdString());
const QDateTime lastModified = query->value(4).toDateTime(); const QDateTime lastModified = query->value(3).toDateTime();
if (lastModified.isValid()) { if (lastModified.isValid()) {
changed->set_last_modified(lastModified.toSecsSinceEpoch()); changed->set_last_modified(lastModified.toSecsSinceEpoch());
} }
changed->set_deleted(query->value(5).toBool()); changed->set_deleted(query->value(4).toBool());
} }
return true; return true;
} }
@ -957,7 +937,7 @@ Response::ResponseCode AbstractServerSocketInterface::cmdDeckSyncCheck(const Com
sqlInterface->checkSql(); sqlInterface->checkSql();
Response_DeckSyncStatus *re = new Response_DeckSyncStatus; Response_DeckSyncStatus *re = new Response_DeckSyncStatus;
if (!syncCheckHelper(0, QString(), userInfo->id(), static_cast<qint64>(cmd.last_sync_time()), re)) { if (!syncCheckHelper(userInfo->id(), static_cast<qint64>(cmd.last_sync_time()), re)) {
delete re; delete re;
return Response::RespContextError; return Response::RespContextError;
} }

View file

@ -165,8 +165,7 @@ private:
bool storeDeckVersion(int deckId, const QString &content, const QString &hash); bool storeDeckVersion(int deckId, const QString &content, const QString &hash);
DeckList *getDeckFromDatabase(int deckId); DeckList *getDeckFromDatabase(int deckId);
Response::ResponseCode cmdDeckDownload(const Command_DeckDownload &cmd, ResponseContainer &rc); Response::ResponseCode cmdDeckDownload(const Command_DeckDownload &cmd, ResponseContainer &rc);
bool bool syncCheckHelper(int userId, qint64 sinceSecs, Response_DeckSyncStatus *status);
syncCheckHelper(int folderId, const QString &prefix, int userId, qint64 sinceSecs, Response_DeckSyncStatus *status);
Response::ResponseCode cmdDeckSyncCheck(const Command_DeckSyncCheck &cmd, ResponseContainer &rc); Response::ResponseCode cmdDeckSyncCheck(const Command_DeckSyncCheck &cmd, ResponseContainer &rc);
Response::ResponseCode cmdDeckDownloadPublic(const Command_DeckDownloadPublic &cmd, ResponseContainer &rc); Response::ResponseCode cmdDeckDownloadPublic(const Command_DeckDownloadPublic &cmd, ResponseContainer &rc);
Response::ResponseCode cmdDeckShareCreate(const Command_DeckShareCreate &cmd, ResponseContainer &rc); Response::ResponseCode cmdDeckShareCreate(const Command_DeckShareCreate &cmd, ResponseContainer &rc);