From 36e616fbbd5f819df64de0c2ffba56844cac2925 Mon Sep 17 00:00:00 2001 From: Lucas Leblow Date: Tue, 12 Dec 2023 13:21:41 -0700 Subject: [PATCH] Read messages from a file and add to OrbitDB --- .../src/nest/storage/storage.service.ts | 38 +++++++++++++++++-- packages/desktop/package.json | 2 +- .../java/com/quietmobile/MainActivity.java | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/nest/storage/storage.service.ts b/packages/backend/src/nest/storage/storage.service.ts index b2149dc6b4..e424e62a23 100644 --- a/packages/backend/src/nest/storage/storage.service.ts +++ b/packages/backend/src/nest/storage/storage.service.ts @@ -12,6 +12,10 @@ import type { IPFS } from 'ipfs-core' import OrbitDB from 'orbit-db' import EventStore from 'orbit-db-eventstore' import KeyValueStore from 'orbit-db-kvstore' +// @ts-ignore - needs declaration file? +import Log from 'ipfs-log' +// @ts-ignore - needs declaration file? +import { read, write } from 'orbit-db-io' import path from 'path' import { EventEmitter } from 'events' import PeerId from 'peer-id' @@ -128,7 +132,10 @@ export class StorageService extends EventEmitter { this.attachFileManagerEvents() await this.initDatabases() + // Should we await this? void this.startIpfs() + + this.loadMessagesFromFile('test.json') } private async startIpfs() { @@ -398,9 +405,9 @@ export class StorageService extends EventEmitter { await this.channels.load({ fetchEntryTimeout: 1000 }) this.logger('Channels count:', Object.keys(this.channels.all).length) this.logger('Channels names:', Object.keys(this.channels.all)) - Object.values(this.channels.all).forEach(async (channel: PublicChannel) => { + await Promise.all(Object.values(this.channels.all).map(async (channel: PublicChannel) => { await this.subscribeToChannel(channel) - }) + })) this.logger('STORAGE: Finished createDbForChannels') } @@ -673,7 +680,12 @@ export class StorageService extends EventEmitter { return } try { - await repo.db.add(message) + const hash = await repo.db.add(message) + // @ts-ignore + const entry = repo.db._oplog.get(hash) + console.log("ENTRY\n\n\n\n\n\n\n\n\n") + console.log(entry) + console.log(encodeURIComponent(JSON.stringify(entry))) } catch (e) { this.logger.error( `STORAGE: Could not append message (entry not allowed to write to the log). Details: ${e.message}` @@ -791,6 +803,26 @@ export class StorageService extends EventEmitter { }) } + public async loadMessagesFromFile(filepath: string): Promise { + this.logger('Loading messages from file', filepath) + const entryStrings = fs.readFileSync(filepath, 'utf8').split("\n") + const entries = entryStrings.map(e => e.length > 0 ? JSON.parse(decodeURIComponent(e)) : undefined).filter(e => e !== undefined) + + // TODO: Group by repo and sync all messages for a specific repo at once + for (const entry of entries) { + const channelId = entry.payload.value.channelId + const repo = this.publicChannelsRepos.get(channelId) + + if (repo) { + this.logger('Found channel, syncing entries') + // @ts-ignore + await repo.db.sync([entry]) + } else { + this.logger.error(`Could not add entries. No '${channelId}' channel in saved public channels`) + } + } + } + private clean() { // @ts-ignore this.channels = undefined diff --git a/packages/desktop/package.json b/packages/desktop/package.json index b4e6da4b71..3f98c3198e 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -111,7 +111,7 @@ "build:renderer:prod": "webpack --config webpack/webpack.config.renderer.prod.js", "postBuild": "node scripts/postBuild.js", "prestart": "npm run build:main", - "start": "cross-env DEBUG='backend*,quiet*,state-manager*,desktop*,utils*,libp2p:websockets:listener:backend,libp2p:connection-manager:auto-dialler' npm run start:renderer", + "start": "cross-env DEBUG='*' npm run start:renderer", "start:main": "cross-env NODE_ENV=development electron .", "start:renderer": "cross-env NODE_ENV=development webpack-dev-server --config webpack/webpack.config.renderer.dev.js", "storybook": "export NODE_OPTIONS=--openssl-legacy-provider && start-storybook -p 6006", diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/MainActivity.java b/packages/mobile/android/app/src/main/java/com/quietmobile/MainActivity.java index 4670677df5..4ee36ada17 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/MainActivity.java +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/MainActivity.java @@ -123,4 +123,4 @@ protected void onDestroy() { super.onDestroy(); Log.d("QUIET", "Application destroyed."); } -} \ No newline at end of file +}