Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read messages from a file and add to OrbitDB #2190

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)
leblowl marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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.");
}
}
}
Loading