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:
@@ -1,6 +1,6 @@
|
||||
import System from '../System.js';
|
||||
import { getUserEncryptionKey } from '../keyManager.js';
|
||||
import BookRepo, { BookQuery, BookToolsTable, BookToolsSettings, EritBooksTable } from "../repositories/book.repository.js";
|
||||
import BookRepo, { BookQuery, BookToolsTable, EritBooksTable } from "../repositories/book.repository.js";
|
||||
import { BookActSummariesTable } from "../repositories/act.repository.js";
|
||||
import { BookAIGuideLineTable, BookGuideLineTable } from "../repositories/guideline.repository.js";
|
||||
import ChapterRepo, {
|
||||
@@ -35,9 +35,13 @@ import Cover from "./Cover.js";
|
||||
import UserRepo from "../repositories/user.repository.js";
|
||||
|
||||
export interface SyncedBookTools {
|
||||
charactersEnabled: boolean;
|
||||
worldsEnabled: boolean;
|
||||
locationsEnabled: boolean;
|
||||
lastUpdate: number;
|
||||
}
|
||||
|
||||
export interface BookToolsSettings {
|
||||
characters: boolean;
|
||||
worlds: boolean;
|
||||
locations: boolean;
|
||||
}
|
||||
|
||||
export interface BookProps {
|
||||
@@ -308,7 +312,7 @@ export default class Book {
|
||||
|
||||
public static updateBookToolSetting(userId: string, bookId: string, toolName: 'characters' | 'worlds' | 'locations', enabled: boolean, lang: 'fr' | 'en' = 'fr'): boolean {
|
||||
const columnName: 'characters_enabled' | 'worlds_enabled' | 'locations_enabled' = `${toolName}_enabled` as 'characters_enabled' | 'worlds_enabled' | 'locations_enabled';
|
||||
return BookRepo.updateBookToolSetting(userId, bookId, columnName, enabled, lang);
|
||||
return BookRepo.updateBookToolSetting(userId, bookId, columnName, enabled, System.timeStampInSeconds(), lang);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -199,7 +199,7 @@ export default class Download {
|
||||
if (!issuesInserted) return false;
|
||||
|
||||
return data.bookTools.every((bookTool: BookToolsTable): boolean => {
|
||||
return BookRepo.insertSyncBookTools(bookTool.book_id, userId, bookTool.characters_enabled, bookTool.worlds_enabled, bookTool.locations_enabled, lang);
|
||||
return BookRepo.insertSyncBookTools(bookTool.book_id, userId, bookTool.characters_enabled, bookTool.worlds_enabled, bookTool.locations_enabled, bookTool.last_update, lang);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user