Skip to content

Commit

Permalink
Read messages from a file and add to OrbitDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Leblow committed Jan 2, 2024
1 parent b9d1ca1 commit 36e616f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
38 changes: 35 additions & 3 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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')
}

Expand Down Expand Up @@ -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}`
Expand Down Expand Up @@ -791,6 +803,26 @@ export class StorageService extends EventEmitter {
})
}

public async loadMessagesFromFile(filepath: string): Promise<void> {
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
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ protected void onDestroy() {
super.onDestroy();
Log.d("QUIET", "Application destroyed.");
}
}
}

0 comments on commit 36e616f

Please sign in to comment.