Remove Story model handling verbal styles and linguistic properties
- Delete `Story` model implementation including `getVerbesStyle` method and related properties. - Cleanup unused interfaces and redundant logic from the codebase.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import User from "./User";
|
||||
import System, {UserKey} from "./System";
|
||||
import CharacterRepo, {AttributeResult, CharacterResult, CompleteCharacterResult} from "../repositories/character.repo";
|
||||
import CharacterRepo, {
|
||||
AttributeResult,
|
||||
CharacterResult,
|
||||
CompleteCharacterResult
|
||||
} from "@/electron/database/repositories/character.repository";
|
||||
import System from "@/electron/database/System";
|
||||
import {getUserEncryptionKey} from "../keyManager";
|
||||
|
||||
export type CharacterCategory = 'Main' | 'Secondary' | 'Recurring';
|
||||
|
||||
@@ -61,19 +65,17 @@ export interface CharacterAttribute {
|
||||
values: Attribute[];
|
||||
}
|
||||
|
||||
export default class Character{
|
||||
public static async getCharacterList(userId: string, bookId: string): Promise<CharacterProps[]> {
|
||||
const user = new User(userId);
|
||||
const keys:UserKey[] = await System.getAllUserKeysAndVersions(userId);
|
||||
const characters: CharacterResult[] = await CharacterRepo.fetchCharacters(userId, bookId);
|
||||
export default class Character {
|
||||
public static getCharacterList(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): CharacterProps[] {
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const characters: CharacterResult[] = CharacterRepo.fetchCharacters(userId, bookId, lang);
|
||||
if (!characters) return [];
|
||||
if (characters.length === 0) return [];
|
||||
const characterList:CharacterProps[] = [];
|
||||
const characterList: CharacterProps[] = [];
|
||||
for (const character of characters) {
|
||||
const userKey:string = await user.getUserKey(character.char_meta,true,keys);
|
||||
characterList.push({
|
||||
id:character.character_id,
|
||||
name:character.first_name ? System.decryptDataWithUserKey(character.first_name,userKey) : '',
|
||||
id: character.character_id,
|
||||
name: character.first_name ? System.decryptDataWithUserKey(character.first_name, userKey) : '',
|
||||
lastName: character.last_name ? System.decryptDataWithUserKey(character.last_name, userKey) : '',
|
||||
title: character.title ? System.decryptDataWithUserKey(character.title, userKey) : '',
|
||||
category: character.category ? System.decryptDataWithUserKey(character.category, userKey) : '',
|
||||
@@ -85,11 +87,9 @@ export default class Character{
|
||||
}
|
||||
return characterList;
|
||||
}
|
||||
|
||||
public static async addNewCharacter(userId: string, character: CharacterPropsPost, bookId: string): Promise<string> {
|
||||
const user = new User(userId);
|
||||
const meta:string = System.encryptDateKey(userId);
|
||||
const userKey:string = await user.getUserKey(meta,true);
|
||||
|
||||
public static addNewCharacter(userId: string, character: CharacterPropsPost, bookId: string, lang: 'fr' | 'en' = 'fr'): string {
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const characterId: string = System.createUniqueId();
|
||||
const encryptedName: string = System.encryptDataWithUserKey(character.name, userKey);
|
||||
const encryptedLastName: string = System.encryptDataWithUserKey(character.lastName, userKey);
|
||||
@@ -99,7 +99,7 @@ export default class Character{
|
||||
const encryptedRole: string = System.encryptDataWithUserKey(character.role, userKey);
|
||||
const encryptedBiography: string = System.encryptDataWithUserKey(character.biography ? character.biography : '', userKey);
|
||||
const encryptedHistory: string = System.encryptDataWithUserKey(character.history ? character.history : '', userKey);
|
||||
await CharacterRepo.addNewCharacter(userId, characterId, encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, bookId, meta);
|
||||
CharacterRepo.addNewCharacter(userId, characterId, encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, bookId, lang);
|
||||
const attributes: string[] = Object.keys(character);
|
||||
for (const key of attributes) {
|
||||
if (Array.isArray(character[key as keyof CharacterPropsPost])) {
|
||||
@@ -108,18 +108,16 @@ export default class Character{
|
||||
for (const item of array) {
|
||||
const type: string = key;
|
||||
const name: string = item.name;
|
||||
await this.addNewAttribute(characterId, userId, type, name);
|
||||
this.addNewAttribute(characterId, userId, type, name, lang);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return characterId;
|
||||
}
|
||||
|
||||
static async updateCharacter(userId: string, character: CharacterPropsPost): Promise<boolean> {
|
||||
const user = new User(userId);
|
||||
const meta: string = System.encryptDateKey(userId);
|
||||
const userKey: string = await user.getUserKey(meta, true);
|
||||
|
||||
static updateCharacter(userId: string, character: CharacterPropsPost, lang: 'fr' | 'en' = 'fr'): boolean {
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const encryptedName: string = System.encryptDataWithUserKey(character.name, userKey);
|
||||
const encryptedLastName: string = System.encryptDataWithUserKey(character.lastName, userKey);
|
||||
const encryptedTitle: string = System.encryptDataWithUserKey(character.title, userKey);
|
||||
@@ -128,72 +126,63 @@ export default class Character{
|
||||
const encryptedRole: string = System.encryptDataWithUserKey(character.role, userKey);
|
||||
const encryptedBiography: string = System.encryptDataWithUserKey(character.biography ? character.biography : '', userKey);
|
||||
const encryptedHistory: string = System.encryptDataWithUserKey(character.history ? character.history : '', userKey);
|
||||
return await CharacterRepo.updateCharacter(userId, character.id, encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, meta);
|
||||
return CharacterRepo.updateCharacter(userId, character.id, encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, lang);
|
||||
}
|
||||
|
||||
static async addNewAttribute(characterId: string, userId: string, type: string, name: string): Promise<string> {
|
||||
const user = new User(userId);
|
||||
const meta: string = System.encryptDateKey(userId);
|
||||
const userKey: string = await user.getUserKey(meta, true);
|
||||
|
||||
static addNewAttribute(characterId: string, userId: string, type: string, name: string, lang: 'fr' | 'en' = 'fr'): string {
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const attributeId: string = System.createUniqueId();
|
||||
const encryptedType: string = System.encryptDataWithUserKey(type, userKey);
|
||||
const encryptedName: string = System.encryptDataWithUserKey(name, userKey);
|
||||
return await CharacterRepo.insertAttribute(attributeId, characterId, userId, encryptedType, encryptedName, meta);
|
||||
return CharacterRepo.insertAttribute(attributeId, characterId, userId, encryptedType, encryptedName, lang);
|
||||
}
|
||||
|
||||
static async deleteAttribute(userId: string, attributeId: string) {
|
||||
return await CharacterRepo.deleteAttribute(userId, attributeId);
|
||||
static deleteAttribute(userId: string, attributeId: string, lang: 'fr' | 'en' = 'fr') {
|
||||
return CharacterRepo.deleteAttribute(userId, attributeId, lang);
|
||||
}
|
||||
|
||||
static async getAttributes(characterId: string, userId: string): Promise<CharacterAttribute[]> {
|
||||
const user: User = new User(userId);
|
||||
const keys: UserKey[] = await System.getAllUserKeysAndVersions(userId);
|
||||
const attributes: AttributeResult[] = await CharacterRepo.fetchAttributes(characterId, userId);
|
||||
|
||||
static getAttributes(characterId: string, userId: string, lang: 'fr' | 'en' = 'fr'): CharacterAttribute[] {
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const attributes: AttributeResult[] = CharacterRepo.fetchAttributes(characterId, userId, lang);
|
||||
if (!attributes?.length) return [];
|
||||
|
||||
|
||||
const groupedMap: Map<string, Attribute[]> = new Map<string, Attribute[]>();
|
||||
|
||||
for (const attribute of attributes) {
|
||||
const userKey: string = await user.getUserKey(attribute.attr_meta, true, keys);
|
||||
const type: string = System.decryptDataWithUserKey(attribute.attribute_name, userKey);
|
||||
const value: string = attribute.attribute_value ? System.decryptDataWithUserKey(attribute.attribute_value, userKey) : '';
|
||||
|
||||
|
||||
if (!groupedMap.has(type)) {
|
||||
groupedMap.set(type, []);
|
||||
}
|
||||
|
||||
|
||||
groupedMap.get(type)!.push({
|
||||
id: attribute.attr_id,
|
||||
name: value
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return Array.from<[string, Attribute[]], CharacterAttribute>(
|
||||
groupedMap,
|
||||
([type, values]: [string, Attribute[]]): CharacterAttribute => ({type, values})
|
||||
);
|
||||
}
|
||||
|
||||
static async getCompleteCharacterList(userId: string, bookId: string, characters: string[]): Promise<CompleteCharacterProps[]> {
|
||||
const characterList: CompleteCharacterResult[] = await CharacterRepo.fetchCompleteCharacters(userId, bookId, characters);
|
||||
|
||||
|
||||
static getCompleteCharacterList(userId: string, bookId: string, characters: string[], lang: 'fr' | 'en' = 'fr'): CompleteCharacterProps[] {
|
||||
const characterList: CompleteCharacterResult[] = CharacterRepo.fetchCompleteCharacters(userId, bookId, characters, lang);
|
||||
|
||||
if (!characterList || characterList.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const user = new User(userId);
|
||||
const keys: UserKey[] = await System.getAllUserKeysAndVersions(userId);
|
||||
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
const completeCharactersMap = new Map<string, CompleteCharacterProps>();
|
||||
for (const character of characterList) {
|
||||
if (!character.character_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (!completeCharactersMap.has(character.character_id)) {
|
||||
const userKey: string = await user.getUserKey(character.char_meta, true, keys);
|
||||
if (!userKey) {
|
||||
continue;
|
||||
}
|
||||
const personnageObj: CompleteCharacterProps = {
|
||||
id: '',
|
||||
name: character.first_name ? System.decryptDataWithUserKey(character.first_name, userKey) : '',
|
||||
@@ -214,21 +203,19 @@ export default class Character{
|
||||
};
|
||||
completeCharactersMap.set(character.character_id, personnageObj);
|
||||
}
|
||||
|
||||
|
||||
const personnage: CompleteCharacterProps | undefined = completeCharactersMap.get(character.character_id);
|
||||
|
||||
|
||||
if (!character.attr_meta) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const rowKey: string = await user.getUserKey(character.attr_meta, true, keys);
|
||||
|
||||
if (!personnage || !rowKey) {
|
||||
|
||||
if (!personnage) {
|
||||
continue;
|
||||
}
|
||||
const decryptedName: string = System.decryptDataWithUserKey(character.attribute_name, rowKey);
|
||||
const decryptedValue: string = character.attribute_value ? System.decryptDataWithUserKey(character.attribute_value, rowKey) : '';
|
||||
|
||||
const decryptedName: string = System.decryptDataWithUserKey(character.attribute_name, userKey);
|
||||
const decryptedValue: string = character.attribute_value ? System.decryptDataWithUserKey(character.attribute_value, userKey) : '';
|
||||
|
||||
if (Array.isArray(personnage[decryptedName])) {
|
||||
personnage[decryptedName].push({
|
||||
id: '',
|
||||
@@ -238,14 +225,14 @@ export default class Character{
|
||||
}
|
||||
return Array.from(completeCharactersMap.values());
|
||||
}
|
||||
|
||||
|
||||
static characterVCard(characters: CompleteCharacterProps[]): string {
|
||||
const charactersMap = new Map<string, CompleteCharacterProps>();
|
||||
let charactersDescription: string = '';
|
||||
|
||||
|
||||
characters.forEach((character: CompleteCharacterProps): void => {
|
||||
const characterKey: string = character.name || character.id || 'unknown';
|
||||
|
||||
|
||||
if (!charactersMap.has(characterKey)) {
|
||||
charactersMap.set(characterKey, {
|
||||
name: character.name,
|
||||
@@ -257,9 +244,9 @@ export default class Character{
|
||||
history: character.history
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const characterData: CompleteCharacterProps = charactersMap.get(characterKey)!;
|
||||
|
||||
|
||||
Object.keys(character).forEach((fieldName: string): void => {
|
||||
if (Array.isArray(character[fieldName])) {
|
||||
if (!characterData[fieldName]) characterData[fieldName] = [];
|
||||
@@ -267,18 +254,18 @@ export default class Character{
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
charactersDescription = Array.from(charactersMap.values()).map((character: CompleteCharacterProps): string => {
|
||||
const descriptionFields: string[] = [];
|
||||
const fullName: string = [character.name, character.lastName].filter(Boolean).join(' ');
|
||||
if (fullName) descriptionFields.push(`Nom : ${fullName}`);
|
||||
|
||||
|
||||
(['category', 'title', 'role', 'biography', 'history'] as const).forEach((propertyKey) => {
|
||||
if (character[propertyKey]) {
|
||||
descriptionFields.push(`${propertyKey.charAt(0).toUpperCase() + propertyKey.slice(1)} : ${character[propertyKey]}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Object.keys(character).forEach((propertyKey: string): void => {
|
||||
const propertyValue: string | Attribute[] | undefined = character[propertyKey];
|
||||
if (Array.isArray(propertyValue) && propertyValue.length > 0) {
|
||||
@@ -287,7 +274,7 @@ export default class Character{
|
||||
descriptionFields.push(`${capitalizedPropertyKey} : ${formattedValues}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return descriptionFields.join('\n');
|
||||
}).join('\n\n');
|
||||
return charactersDescription;
|
||||
|
||||
Reference in New Issue
Block a user