From a8ca0e331f339eddb187fc47c3b7cf4ab600bfc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Wed, 24 May 2023 13:05:38 +0200 Subject: [PATCH] fix: sync checks (#223) --- src/app/settings/pages/AccountPage.vue | 10 ++- src/app/utils/sync.ts | 2 +- src/app/welcome/pages/WelcomePage.vue | 9 ++- src/init/infrastructure/initDatabases.ts | 12 ++-- src/services/auth/strategies/email.ts | 2 +- src/services/persistence/PouchRepository.ts | 69 +++++++++---------- .../persistence/ReviewCardSerializer.ts | 2 +- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/app/settings/pages/AccountPage.vue b/src/app/settings/pages/AccountPage.vue index 1ad91b72..bb82fe9d 100644 --- a/src/app/settings/pages/AccountPage.vue +++ b/src/app/settings/pages/AccountPage.vue @@ -84,14 +84,16 @@ import { IonHeader, IonPage, IonTitle, IonToolbar, IonLoading, IonIcon, useIonRouter, alertController } from '@ionic/vue' import { mail, logoApple, logoGoogle } from 'ionicons/icons' -import { computed } from 'vue' +import { computed, inject } from 'vue' import { go, useAuthentication, useEmitter, useSync } from '@/app/shared' import { useSettingsStore } from '@/app/settings' +import { CouchDB } from '@/services/persistence' /* -------------------------------------------------------------------------- */ /* Dependencies */ /* -------------------------------------------------------------------------- */ +const userDataDb = inject('userData') as CouchDB const emitter = useEmitter() const settings = useSettingsStore() const auth = useAuthentication() @@ -137,5 +139,11 @@ async function onEmailSignIn() { async function onLogOut() { settings.auth.sessionId = '' settings.auth.token = '' + settings.auth.collectionId = '' + settings.auth.strategy = '' + settings.auth.expiresAt = undefined + settings.auth.refreshedAt = undefined + await userDataDb.destroy() + emitter.emit('syncCompleted') } diff --git a/src/app/utils/sync.ts b/src/app/utils/sync.ts index 512df40c..a41dbc2c 100644 --- a/src/app/utils/sync.ts +++ b/src/app/utils/sync.ts @@ -9,7 +9,7 @@ import { export function createRepositories(remote: string, token: string) { - const couchDB = new CouchDB(remote, undefined, token) + const couchDB = new CouchDB(remote, { token: token }) return new Repositories( new InMemoryRepository(), diff --git a/src/app/welcome/pages/WelcomePage.vue b/src/app/welcome/pages/WelcomePage.vue index 4db8b887..55314512 100644 --- a/src/app/welcome/pages/WelcomePage.vue +++ b/src/app/welcome/pages/WelcomePage.vue @@ -52,10 +52,10 @@ {{ $t("welcome.login.withEmail") }} diff --git a/src/init/infrastructure/initDatabases.ts b/src/init/infrastructure/initDatabases.ts index 1b400394..47bdf0c9 100644 --- a/src/init/infrastructure/initDatabases.ts +++ b/src/init/infrastructure/initDatabases.ts @@ -6,10 +6,14 @@ import { InitResult } from '../initialization' * Initialize databases */ export async function initDatabases(): Promise { - const adapter = Capacitor.getPlatform() == 'ios' ? 'cordova-sqlite' : undefined + const config = { + adapter: Capacitor.getPlatform() == 'ios' ? 'cordova-sqlite' : undefined, + location: 'default' + } + return { - verses: new CouchDB('verses', adapter), - userData: new CouchDB('userData', adapter), - userDataTutorial: new CouchDB('tutorial:userData', adapter) + verses: new CouchDB('verses', config), + userData: new CouchDB('userData', config), + userDataTutorial: new CouchDB('tutorial:userData', config) } } \ No newline at end of file diff --git a/src/services/auth/strategies/email.ts b/src/services/auth/strategies/email.ts index ff88caaf..ac8d6708 100644 --- a/src/services/auth/strategies/email.ts +++ b/src/services/auth/strategies/email.ts @@ -4,7 +4,7 @@ import { AuthenticationStrategy, AuthenticationResult } from './strategy' export class EmailAuthenticationStrategy implements AuthenticationStrategy { //@ts-ignore async authenticate(data: any): Promise { - console.log(`lgoinig in with email "${data.email}", "${data.code}"`, data.email && !data.code) + console.log(`Signing in with email "${data.email}", "${data.code}"`, data.email && !data.code) if (data.email && !data.code) { // TODO: //@ts-ignore diff --git a/src/services/persistence/PouchRepository.ts b/src/services/persistence/PouchRepository.ts index 4e78f915..aeabb48d 100644 --- a/src/services/persistence/PouchRepository.ts +++ b/src/services/persistence/PouchRepository.ts @@ -13,35 +13,21 @@ PouchDB.plugin(PouchdbFind) PouchDB.plugin(PouchDBAdapterSqlLite) +export interface CouchDBConfig { + adapter?: string, + token?: string + location?: string +} + export class CouchDB { private _db: PouchDB.Database + private _name: string + private _config: CouchDBConfig - constructor(dbName: string, adapter?: string, token?: string) { - if (adapter) { - this._db = new PouchDB(dbName, { - adapter: 'cordova-sqlite', - // revs_limit: 1, - // auto_compaction: true, - // @ts-ignore - location: 'default', - // iosDatabaseLocation: 'default', - // @ts-ignore - fetch: function (url, opts) { - opts.headers.set('Authorization', 'Bearer ' + token) - return PouchDB.fetch(url, opts) - } - }) - } else { - this._db = new PouchDB(dbName, { - fetch: function (url, opts) { - // @ts-ignore - opts.headers.set('Authorization', 'Bearer ' + token) - return PouchDB.fetch(url, opts) - } - // revs_limit: 1, - // auto_compaction: true, - }) - } + constructor(name: string, config: CouchDBConfig) { + this._name = name + this._config = config + this._db = this.creaateDatabase() } async sync(to: string) { @@ -52,14 +38,23 @@ export class CouchDB { return await this._db.replicate.from(from) } - async deleteAll() { - const docs = await this._db.allDocs() - docs.rows.forEach(async y => { - await this._db.remove(y.id, y.value.rev) - }) + async destroy() { + await this._db.destroy() + this._db = this.creaateDatabase() } - get db(): PouchDB.Database { return this ._db } + get db(): PouchDB.Database { return this._db } + + private creaateDatabase() { + return new PouchDB(this._name, { + ...this._config, + fetch: (url, opts) => { + // @ts-ignore + opts.headers.set('Authorization', 'Bearer ' + this._config.token) + return PouchDB.fetch(url, opts) + } + }) + } } export class PouchRepository< @@ -146,7 +141,7 @@ export class PouchRepository< try { const doc = await this._db.db.get(id.value, { latest: true }) await this._db.db.remove(doc) - } catch(e) { console.log('CANT DELETE', e, this._collectionName) } + } catch (e) { console.log('CANT DELETE', e, this._collectionName) } } } @@ -184,17 +179,17 @@ class QueryConverter { // return query return { - [query.field]: { [this.operatorsMap[query.operator]] : value } + [query.field]: { [this.operatorsMap[query.operator]]: value } } } else if (query instanceof Expression) { if (query.operator === LogicalOperators.Or) { - return { '$or': [ ...query.query.map(x => this._visit(x)) ] } + return { '$or': [...query.query.map(x => this._visit(x))] } } if (query.operator === LogicalOperators.Not) { - return { '$not': deepMerge({}, ...query.query.map(x => this._visit(x)) ) } + return { '$not': deepMerge({}, ...query.query.map(x => this._visit(x))) } } if (query.operator === LogicalOperators.And) { - return { '$and': [ ...query.query.map(x => this._visit(x)) ] } + return { '$and': [...query.query.map(x => this._visit(x))] } } return deepMerge( diff --git a/src/services/persistence/ReviewCardSerializer.ts b/src/services/persistence/ReviewCardSerializer.ts index 421c8336..32c21a5b 100644 --- a/src/services/persistence/ReviewCardSerializer.ts +++ b/src/services/persistence/ReviewCardSerializer.ts @@ -17,7 +17,7 @@ export class ReviewCardSerializer implements ObjectMapper { 'interval': from.interval, 'ease': from.ease, 'lapses': from.lapses, - 'difficultyChangedAt': from.difficultyChangedAt, + 'difficultyChangedAt': date(from.difficultyChangedAt), } } }