alpha
Jan Umbach 2023-02-05 21:30:02 +01:00
parent 536291a76f
commit b8f15fdf26
13 changed files with 189 additions and 37 deletions

View File

@ -17,6 +17,8 @@ import {appStatus} from '@caj/configs/appNonSaveVar';
import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer'; import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer';
import BigDataManager from '@caj/helper/storage/BigDataManager'; import BigDataManager from '@caj/helper/storage/BigDataManager';
import {initKey} from '@caj/helper/storage/bdm/encryption'; import {initKey} from '@caj/helper/storage/bdm/encryption';
import UserManager from '@caj/user/UserManager';
import MyUserManager from '@caj/user/MyUserManager';
const AnimationView = animated(View); const AnimationView = animated(View);
@ -27,15 +29,23 @@ function onAppStart() {
BigDataManager.initDatabase() BigDataManager.initDatabase()
.then(async () => { .then(async () => {
console.log('finish'); console.log('finish');
/*await BigDataManager.setEntry('users', { await BigDataManager.setEntry('users', {
UserId: 'test', UserId: 'test',
AccountName: 'test222222222', AccountName: 'test222222222',
Username: 'us', Username: 'us',
});*/ });
setTimeout(async () => { setTimeout(async () => {
console.log('get'); console.log('get');
console.log(await BigDataManager.getEntry('users', 'test')); console.log(await BigDataManager.getEntry('users', 'test'));
console.log(
'user',
await UserManager.getUser(MyUserManager.getSelectedUserId()),
);
console.log(
'user2',
await UserManager.getUser('ce97da21-c5f1-46f8-947b-ce9288b74ed1'),
);
}, 1100); }, 1100);
store.dispatch( store.dispatch(

View File

@ -1,11 +1,13 @@
import DBSchemas from './schemas';
import {getDatabase} from './getDB'; import {getDatabase} from './getDB';
import {databaseNames, possibleDBKeys} from './types'; import {databaseConf, databaseNames, possibleDBKeys} from './types';
export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => { export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
const realm = await getDatabase(dbName); const realm = await getDatabase(dbName);
console.log(realm.path); console.log(realm.path);
const val = realm.objectForPrimaryKey(dbName, key); const schema = DBSchemas[dbName].details.properties;
const val = realm.objectForPrimaryKey<typeof schema>('users', key);
return val; return val;
}; };

View File

@ -7,8 +7,8 @@ export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
const UserId = MyUserManager.getSelectedUserId(); const UserId = MyUserManager.getSelectedUserId();
const tx = db.transaction(UserId, 'readonly'); const tx = db.transaction(dbName, 'readonly');
const store = tx.objectStore(UserId); const store = tx.objectStore(dbName);
return await store.get(key); return await store.get(key);
//await store.put(value); //await store.put(value);

View File

@ -36,11 +36,22 @@ setInterval(() => {
) { ) {
dbObj.db?.close(); dbObj.db?.close();
dbObj.lastUsedTimestamp = undefined; dbObj.lastUsedTimestamp = undefined;
console.log('Database not used. Closing ' + dbObj.name); //console.log('Database not used. Closing ' + dbObj.name);
} }
} }
}, 5000); }, 5000);
export function closeAllDatabases() {
for (let name in databases) {
let dbObj = databases[name];
if (dbObj.lastUsedTimestamp !== undefined) {
dbObj.db?.close();
dbObj.lastUsedTimestamp = undefined;
}
}
}
export async function openMyDatabase(schema: databaseConf): Promise<DBType> { export async function openMyDatabase(schema: databaseConf): Promise<DBType> {
const folderPath = MyUserManager.getSelectedUserId(); const folderPath = MyUserManager.getSelectedUserId();
const path = folderPath + '/' + schema.filePath; const path = folderPath + '/' + schema.filePath;

View File

@ -38,6 +38,17 @@ setInterval(() => {
} }
}, 5000); }, 5000);
export function closeAllDatabases() {
for (let name in databases) {
let dbObj = databases[name];
if (dbObj.lastUsedTimestamp !== undefined) {
dbObj.db?.close();
dbObj.lastUsedTimestamp = undefined;
}
}
}
export async function openMyDatabase( export async function openMyDatabase(
schema: databaseConf, schema: databaseConf,
init?: boolean, init?: boolean,
@ -50,12 +61,12 @@ export async function openMyDatabase(
}, },
); );
if (init === true) { /*if (init === true) {
const UserId = MyUserManager.getSelectedUserId(); const UserId = MyUserManager.getSelectedUserId();
console.log(db.objectStoreNames.contains(UserId)); console.log(db.objectStoreNames.contains(UserId));
if (db.objectStoreNames.contains(UserId) === false) { if (db.objectStoreNames.contains(UserId) === false) {
} }
} }*/
return db; return db;
} }

View File

@ -1,24 +1,24 @@
import {openDB, deleteDB, wrap, unwrap} from 'idb'; import {openDB, deleteDB, wrap, unwrap} from 'idb';
import {databases, DBObject, getDatabase} from './getDB'; import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB';
import DBSchemas from './schemas'; import DBSchemas from './schemas';
export const initDatabase = (): Promise<void> => { export const initDatabase = (): Promise<void> => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
setTimeout(async () => { closeAllDatabases();
for (const key in DBSchemas) {
const schema = DBSchemas[key as keyof typeof DBSchemas];
let dbObj: DBObject = { for (const key in DBSchemas) {
schema: schema, const schema = DBSchemas[key as keyof typeof DBSchemas];
name: schema.details.name,
db: undefined,
};
databases[dbObj.name] = dbObj; let dbObj: DBObject = {
schema: schema,
name: schema.details.name,
db: undefined,
};
await getDatabase(dbObj.name, true); // init Database databases[dbObj.name] = dbObj;
}
resolve(); await getDatabase(dbObj.name, true); // init Database
}, 1000); }
resolve();
}); });
}; };

View File

@ -18,3 +18,21 @@ export const usersDBMigration = (Schema: databaseConf) => {
return callback; return callback;
}; };
export const chatDBMigration = (Schema: databaseConf) => {
const callback: MigrationCallback = (oldRealm, newRealm) => {
/*// only apply this change if upgrading to schemaVersion 2
if (oldRealm.schemaVersion < 2) {
const oldObjects = oldRealm.objects('Person');
const newObjects = newRealm.objects('Person');
// loop through all objects and set the fullName property in the new schema
for (const objectIndex in oldObjects) {
const oldObject = oldObjects[objectIndex];
const newObject = newObjects[objectIndex];
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
}
}*/
};
return callback;
};

View File

@ -29,7 +29,7 @@ export const usersDBMigration = (Schema: databaseConf) => {
) => { ) => {
if (oldVersion == 0) { if (oldVersion == 0) {
// perform the initialization // perform the initialization
db.createObjectStore(MyUserManager.getSelectedUserId(), { db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey, keyPath: Schema.details.primaryKey,
}); });
} else if (newVersion !== null) { } else if (newVersion !== null) {
@ -56,7 +56,7 @@ export const chatDBMigration = (Schema: databaseConf) => {
) => { ) => {
if (oldVersion == 0) { if (oldVersion == 0) {
// perform the initialization // perform the initialization
db.createObjectStore(MyUserManager.getSelectedUserId(), { db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey, keyPath: Schema.details.primaryKey,
}); });
} else if (newVersion !== null) { } else if (newVersion !== null) {

View File

@ -1,5 +1,5 @@
import {usersDBMigration} from './migration'; import {usersDBMigration} from './migration';
import {chatDBMigration} from './migration.web'; import {chatDBMigration} from './migration';
import {databaseConf} from './types'; import {databaseConf} from './types';
const users: databaseConf = { const users: databaseConf = {

View File

@ -7,8 +7,8 @@ export const setEntry = async (dbName: databaseNames, value: any) => {
const UserId = MyUserManager.getSelectedUserId(); const UserId = MyUserManager.getSelectedUserId();
const tx = db.transaction(UserId, 'readwrite'); const tx = db.transaction(dbName, 'readwrite');
const store = tx.objectStore(UserId); const store = tx.objectStore(dbName);
//const val = (await store.get('counter')) || 0; //const val = (await store.get('counter')) || 0;
await store.put(value); await store.put(value);
await tx.done; await tx.done;

View File

@ -9,6 +9,7 @@ import {
} from '@caj/configs/types'; } from '@caj/configs/types';
import {saveVarChanges} from '@caj/helper/appData'; import {saveVarChanges} from '@caj/helper/appData';
import {apiBackendRequest, makeRequest} from '@caj/helper/request'; import {apiBackendRequest, makeRequest} from '@caj/helper/request';
import BigDataManager from '@caj/helper/storage/BigDataManager';
import {store} from '@caj/redux/store'; import {store} from '@caj/redux/store';
import {MyUserAccount, createUserProp, SourceProp} from './types'; import {MyUserAccount, createUserProp, SourceProp} from './types';
@ -33,13 +34,10 @@ function createNewMyUser(
EMail, EMail,
SessionId, SessionId,
WebSocketSessionId, WebSocketSessionId,
lastUpdateTimestamp: createUserProp( lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
SourceProp.offline,
Math.floor(new Date().getTime() / 1000),
),
ProfilePicture: { ProfilePicture: {
hq: createUserProp(SourceProp.offline), hq: createUserProp(SourceProp.online),
lq: createUserProp(SourceProp.offline), lq: createUserProp(SourceProp.online),
}, },
userSettings: { userSettings: {
lang: store.getState().appVariables.lang.details.langCode, lang: store.getState().appVariables.lang.details.langCode,
@ -48,6 +46,7 @@ function createNewMyUser(
}; };
createMyUser(user); createMyUser(user);
BigDataManager.initDatabase();
makeRequest({ makeRequest({
path: apiBackendRequest.APP_START, path: apiBackendRequest.APP_START,

View File

@ -1,5 +1,106 @@
import {UserId, XAuthorization} from '@caj/configs/types'; import {UserId, XAuthorization} from '@caj/configs/types';
import {makeRequest, apiBackendRequest} from '@caj/helper/request';
import BigDataManager from '@caj/helper/storage/BigDataManager';
import {store} from '@caj/redux/store'; import {store} from '@caj/redux/store';
import {createUserProp, SourceProp, User} from './types';
const UserManager = {}; async function getUser(
UserId: UserId,
save?: boolean,
): Promise<User | undefined> {
let user: User | undefined;
let state = store.getState();
if (UserId === state.appVariables.preferences.selectedAccount) {
const usr = state.appVariables.preferences.accounts[UserId];
if (usr !== undefined) {
user = {
AccountName: usr.AccountName,
Description: usr.Description,
FollowersCount: usr.FollowersCount,
FollowingCount: usr.FollowingCount,
lastUpdateTimestamp: usr.lastUpdateTimestamp,
ProfilePicture: usr.ProfilePicture,
UserId,
Username: usr.Username,
XpLevel: usr.XpLevel,
XpPoints: usr.XpPoints,
};
}
}
if (user === undefined) {
const usr = await BigDataManager.getEntry('users', UserId);
user = {
AccountName: usr.AccountName,
Description: usr.Description,
FollowersCount: usr.FollowersCount,
FollowingCount: usr.FollowingCount,
lastUpdateTimestamp: usr.lastUpdateTimestamp,
ProfilePicture: usr.ProfilePicture,
UserId,
Username: usr.Username,
XpLevel: usr.XpLevel,
XpPoints: usr.XpPoints,
};
}
if (user === undefined) {
try {
const resp = await makeRequest({
path: apiBackendRequest.GET_USER_PROFILE,
requestGET: {':userId': UserId},
response: {
AccountName: '',
Description: '',
FollowersCount: 0,
FollowingCount: 0,
Username: '',
XpLevel: 0,
XpPoints: 0,
AvatarUrl: '',
},
});
user = {
AccountName: createUserProp(
SourceProp.cached,
resp.response.AccountName,
),
Description: createUserProp(
SourceProp.cached,
resp.response.Description,
),
FollowersCount: createUserProp(
SourceProp.cached,
resp.response.FollowersCount,
),
FollowingCount: createUserProp(
SourceProp.cached,
resp.response.FollowingCount,
),
lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
ProfilePicture: {
hq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
lq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
},
UserId,
Username: createUserProp(SourceProp.offline, resp.response.Username),
XpLevel: createUserProp(SourceProp.cached, resp.response.XpLevel),
XpPoints: createUserProp(SourceProp.cached, resp.response.XpPoints),
};
BigDataManager.setEntry('users', user);
} catch (error: any) {
console.error(error.status);
}
}
return user;
}
const UserManager = {getUser};
export default UserManager; export default UserManager;

View File

@ -31,7 +31,7 @@ export interface User {
UserId: UserId; UserId: UserId;
ProfilePicture: ProfilePicture; ProfilePicture: ProfilePicture;
lastUpdateTimestamp: BasicUserProp<timestamp>; lastUpdateTimestamp: timestamp;
AccountName: BasicUserProp<AccountName>; AccountName: BasicUserProp<AccountName>;
Username: BasicUserProp<Username>; Username: BasicUserProp<Username>;
Description: BasicUserProp<string>; Description: BasicUserProp<string>;