diff --git a/src/appStart/StartHelper.tsx b/src/appStart/StartHelper.tsx index 39d77cd..4ef29e4 100644 --- a/src/appStart/StartHelper.tsx +++ b/src/appStart/StartHelper.tsx @@ -17,6 +17,8 @@ import {appStatus} from '@caj/configs/appNonSaveVar'; import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer'; import BigDataManager from '@caj/helper/storage/BigDataManager'; import {initKey} from '@caj/helper/storage/bdm/encryption'; +import UserManager from '@caj/user/UserManager'; +import MyUserManager from '@caj/user/MyUserManager'; const AnimationView = animated(View); @@ -27,15 +29,23 @@ function onAppStart() { BigDataManager.initDatabase() .then(async () => { console.log('finish'); - /*await BigDataManager.setEntry('users', { + await BigDataManager.setEntry('users', { UserId: 'test', AccountName: 'test222222222', Username: 'us', - });*/ + }); setTimeout(async () => { console.log('get'); 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); store.dispatch( diff --git a/src/caj/helper/storage/bdm/get.ts b/src/caj/helper/storage/bdm/get.ts index 4ee46c4..4fbb0f5 100644 --- a/src/caj/helper/storage/bdm/get.ts +++ b/src/caj/helper/storage/bdm/get.ts @@ -1,11 +1,13 @@ +import DBSchemas from './schemas'; import {getDatabase} from './getDB'; -import {databaseNames, possibleDBKeys} from './types'; +import {databaseConf, databaseNames, possibleDBKeys} from './types'; export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => { const realm = await getDatabase(dbName); console.log(realm.path); - const val = realm.objectForPrimaryKey(dbName, key); + const schema = DBSchemas[dbName].details.properties; + const val = realm.objectForPrimaryKey('users', key); return val; }; diff --git a/src/caj/helper/storage/bdm/get.web.ts b/src/caj/helper/storage/bdm/get.web.ts index c9c10d0..26cf4c7 100644 --- a/src/caj/helper/storage/bdm/get.web.ts +++ b/src/caj/helper/storage/bdm/get.web.ts @@ -7,8 +7,8 @@ export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => { const UserId = MyUserManager.getSelectedUserId(); - const tx = db.transaction(UserId, 'readonly'); - const store = tx.objectStore(UserId); + const tx = db.transaction(dbName, 'readonly'); + const store = tx.objectStore(dbName); return await store.get(key); //await store.put(value); diff --git a/src/caj/helper/storage/bdm/getDB.ts b/src/caj/helper/storage/bdm/getDB.ts index 4a20d21..9b91895 100644 --- a/src/caj/helper/storage/bdm/getDB.ts +++ b/src/caj/helper/storage/bdm/getDB.ts @@ -36,11 +36,22 @@ setInterval(() => { ) { dbObj.db?.close(); dbObj.lastUsedTimestamp = undefined; - console.log('Database not used. Closing ' + dbObj.name); + //console.log('Database not used. Closing ' + dbObj.name); } } }, 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 { const folderPath = MyUserManager.getSelectedUserId(); const path = folderPath + '/' + schema.filePath; diff --git a/src/caj/helper/storage/bdm/getDB.web.ts b/src/caj/helper/storage/bdm/getDB.web.ts index 7d5dd97..2621f1f 100644 --- a/src/caj/helper/storage/bdm/getDB.web.ts +++ b/src/caj/helper/storage/bdm/getDB.web.ts @@ -38,6 +38,17 @@ setInterval(() => { } }, 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, init?: boolean, @@ -50,12 +61,12 @@ export async function openMyDatabase( }, ); - if (init === true) { + /*if (init === true) { const UserId = MyUserManager.getSelectedUserId(); console.log(db.objectStoreNames.contains(UserId)); if (db.objectStoreNames.contains(UserId) === false) { } - } + }*/ return db; } diff --git a/src/caj/helper/storage/bdm/init.ts b/src/caj/helper/storage/bdm/init.ts index c577026..0e4cc44 100644 --- a/src/caj/helper/storage/bdm/init.ts +++ b/src/caj/helper/storage/bdm/init.ts @@ -1,24 +1,24 @@ import {openDB, deleteDB, wrap, unwrap} from 'idb'; -import {databases, DBObject, getDatabase} from './getDB'; +import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB'; import DBSchemas from './schemas'; export const initDatabase = (): Promise => { return new Promise(async (resolve, reject) => { - setTimeout(async () => { - for (const key in DBSchemas) { - const schema = DBSchemas[key as keyof typeof DBSchemas]; + closeAllDatabases(); - let dbObj: DBObject = { - schema: schema, - name: schema.details.name, - db: undefined, - }; + for (const key in DBSchemas) { + const schema = DBSchemas[key as keyof typeof DBSchemas]; - databases[dbObj.name] = dbObj; + let dbObj: DBObject = { + schema: schema, + name: schema.details.name, + db: undefined, + }; - await getDatabase(dbObj.name, true); // init Database - } - resolve(); - }, 1000); + databases[dbObj.name] = dbObj; + + await getDatabase(dbObj.name, true); // init Database + } + resolve(); }); }; diff --git a/src/caj/helper/storage/bdm/migration.ts b/src/caj/helper/storage/bdm/migration.ts index beb9f48..0f10f2a 100644 --- a/src/caj/helper/storage/bdm/migration.ts +++ b/src/caj/helper/storage/bdm/migration.ts @@ -18,3 +18,21 @@ export const usersDBMigration = (Schema: databaseConf) => { 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; +}; diff --git a/src/caj/helper/storage/bdm/migration.web.ts b/src/caj/helper/storage/bdm/migration.web.ts index ad8f9d7..80e6784 100644 --- a/src/caj/helper/storage/bdm/migration.web.ts +++ b/src/caj/helper/storage/bdm/migration.web.ts @@ -29,7 +29,7 @@ export const usersDBMigration = (Schema: databaseConf) => { ) => { if (oldVersion == 0) { // perform the initialization - db.createObjectStore(MyUserManager.getSelectedUserId(), { + db.createObjectStore(Schema.details.name, { keyPath: Schema.details.primaryKey, }); } else if (newVersion !== null) { @@ -56,7 +56,7 @@ export const chatDBMigration = (Schema: databaseConf) => { ) => { if (oldVersion == 0) { // perform the initialization - db.createObjectStore(MyUserManager.getSelectedUserId(), { + db.createObjectStore(Schema.details.name, { keyPath: Schema.details.primaryKey, }); } else if (newVersion !== null) { diff --git a/src/caj/helper/storage/bdm/schemas.ts b/src/caj/helper/storage/bdm/schemas.ts index d577786..f51a774 100644 --- a/src/caj/helper/storage/bdm/schemas.ts +++ b/src/caj/helper/storage/bdm/schemas.ts @@ -1,5 +1,5 @@ import {usersDBMigration} from './migration'; -import {chatDBMigration} from './migration.web'; +import {chatDBMigration} from './migration'; import {databaseConf} from './types'; const users: databaseConf = { diff --git a/src/caj/helper/storage/bdm/set.web.ts b/src/caj/helper/storage/bdm/set.web.ts index d6c9dbe..0f3f4da 100644 --- a/src/caj/helper/storage/bdm/set.web.ts +++ b/src/caj/helper/storage/bdm/set.web.ts @@ -5,10 +5,10 @@ import {databaseNames} from './types'; export const setEntry = async (dbName: databaseNames, value: any) => { const db = await getDatabase(dbName); - const UserId = MyUserManager.getSelectedUserId(); + const UserId = MyUserManager.getSelectedUserId(); - const tx = db.transaction(UserId, 'readwrite'); - const store = tx.objectStore(UserId); + const tx = db.transaction(dbName, 'readwrite'); + const store = tx.objectStore(dbName); //const val = (await store.get('counter')) || 0; await store.put(value); await tx.done; diff --git a/src/caj/user/MyUserManager.ts b/src/caj/user/MyUserManager.ts index 18ae722..ccc85b6 100644 --- a/src/caj/user/MyUserManager.ts +++ b/src/caj/user/MyUserManager.ts @@ -9,6 +9,7 @@ import { } from '@caj/configs/types'; import {saveVarChanges} from '@caj/helper/appData'; import {apiBackendRequest, makeRequest} from '@caj/helper/request'; +import BigDataManager from '@caj/helper/storage/BigDataManager'; import {store} from '@caj/redux/store'; import {MyUserAccount, createUserProp, SourceProp} from './types'; @@ -33,13 +34,10 @@ function createNewMyUser( EMail, SessionId, WebSocketSessionId, - lastUpdateTimestamp: createUserProp( - SourceProp.offline, - Math.floor(new Date().getTime() / 1000), - ), + lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000), ProfilePicture: { - hq: createUserProp(SourceProp.offline), - lq: createUserProp(SourceProp.offline), + hq: createUserProp(SourceProp.online), + lq: createUserProp(SourceProp.online), }, userSettings: { lang: store.getState().appVariables.lang.details.langCode, @@ -48,6 +46,7 @@ function createNewMyUser( }; createMyUser(user); + BigDataManager.initDatabase(); makeRequest({ path: apiBackendRequest.APP_START, diff --git a/src/caj/user/UserManager.ts b/src/caj/user/UserManager.ts index 115ea8e..decd1e2 100644 --- a/src/caj/user/UserManager.ts +++ b/src/caj/user/UserManager.ts @@ -1,5 +1,106 @@ 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 {createUserProp, SourceProp, User} from './types'; -const UserManager = {}; +async function getUser( + UserId: UserId, + save?: boolean, +): Promise { + 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; diff --git a/src/caj/user/types.ts b/src/caj/user/types.ts index 7412014..0524f33 100644 --- a/src/caj/user/types.ts +++ b/src/caj/user/types.ts @@ -31,7 +31,7 @@ export interface User { UserId: UserId; ProfilePicture: ProfilePicture; - lastUpdateTimestamp: BasicUserProp; + lastUpdateTimestamp: timestamp; AccountName: BasicUserProp; Username: BasicUserProp; Description: BasicUserProp;