Add support for syncing tool settings with lastUpdate and improve consistency

- Introduced `lastUpdate` field in `book_tools` for tracking changes.
- Refactored tool enablement logic in `CharacterComponent`, `WorldSetting`, and `LocationComponent` for consistency.
- Updated database schema and migration scripts for `book_tools` table.
- Enhanced synchronization workflows to support new `lastUpdate` logic.
- Adjusted related models, repositories, and IPC handlers for streamlined management.
- Improved type safety and robustness in tool-related methods with additional checks.
This commit is contained in:
natreex
2026-01-15 18:35:48 -05:00
parent ac968b7764
commit 2e6b30c632
12 changed files with 252 additions and 226 deletions

View File

@@ -1,7 +1,7 @@
import { getUserEncryptionKey } from "../keyManager.js";
import System from "../System.js";
import { BookSyncCompare, CompleteBook, SyncedBook, SyncedBookTools } from "./Book.js";
import BookRepo, { EritBooksTable, SyncedBookResult, BookToolsTable } from "../repositories/book.repository.js";
import BookRepo, { EritBooksTable, SyncedBookResult, BookToolsTable, SyncedBookToolsResult } from "../repositories/book.repository.js";
import ChapterRepo, {
BookChapterInfosTable,
BookChaptersTable,
@@ -728,19 +728,11 @@ export default class Sync {
}
}
const serverBookTools: BookToolsTable[] = completeBook.bookTools;
if (serverBookTools && serverBookTools.length > 0) {
for (const serverBookTool of serverBookTools) {
const bookToolsExists: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
if (bookToolsExists) {
BookRepo.updateBookToolSetting(userId, bookId, 'characters_enabled', serverBookTool.characters_enabled === 1, lang);
BookRepo.updateBookToolSetting(userId, bookId, 'worlds_enabled', serverBookTool.worlds_enabled === 1, lang);
BookRepo.updateBookToolSetting(userId, bookId, 'locations_enabled', serverBookTool.locations_enabled === 1, lang);
} else {
const insertSuccessful: boolean = BookRepo.insertSyncBookTools(bookId, userId, serverBookTool.characters_enabled, serverBookTool.worlds_enabled, serverBookTool.locations_enabled, lang);
if (!insertSuccessful) {
return false;
}
if (completeBook.bookTools && completeBook.bookTools.length > 0) {
for (const serverBookTool of completeBook.bookTools) {
const success: boolean = BookRepo.insertSyncBookTools(serverBookTool.book_id, userId, serverBookTool.characters_enabled, serverBookTool.worlds_enabled, serverBookTool.locations_enabled, serverBookTool.last_update, lang);
if (!success) {
return false;
}
}
}
@@ -961,11 +953,9 @@ export default class Sync {
lastUpdate: aiGuidelineRecord.last_update
} : null;
const bookToolsRecord: BookToolsTable | null = BookRepo.fetchBookTools(userId, currentBookId, lang);
const bookTools: SyncedBookTools | null = bookToolsRecord ? {
charactersEnabled: bookToolsRecord.characters_enabled === 1,
worldsEnabled: bookToolsRecord.worlds_enabled === 1,
locationsEnabled: bookToolsRecord.locations_enabled === 1
const bookToolsQuery: SyncedBookToolsResult | null = BookRepo.fetchSyncedBookTools(userId, currentBookId, lang);
const bookTools: SyncedBookTools | null = bookToolsQuery ? {
lastUpdate: bookToolsQuery.last_update
} : null;
return {