Skip to content

Commit

Permalink
Convert UserProfileStore into NestJS provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Leblow committed Nov 6, 2023
1 parent c7e03c9 commit 128405d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/backend/src/nest/storage/UserProfileStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Injectable } from '@nestjs/common'
import { EventEmitter } from 'events'
import KeyValueStore from 'orbit-db-kvstore'
import OrbitDB from 'orbit-db'
Expand Down Expand Up @@ -54,6 +55,7 @@ export const base64DataURLToByteArray = (contents: string): Uint8Array => {
return new Uint8Array(bytes)
}

@Injectable()
export class UserProfileStore {
public orbitDb: OrbitDB
public store: KeyValueStore<UserProfile>
Expand All @@ -65,13 +67,11 @@ export class UserProfileStore {
public static readonly codec = dagCbor
public static readonly hasher = sha256

constructor(orbitDb: OrbitDB) {
this.orbitDb = orbitDb
}

public async init(emitter: EventEmitter) {
public async init(orbitDb: OrbitDB, emitter: EventEmitter) {
logger('Initializing user profiles key/value store')

this.orbitDb = orbitDb

this.store = await this.orbitDb.keyvalue<UserProfile>('user-profiles', {
replicate: false,
Index: UserProfileKeyValueIndex,
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/nest/storage/storage.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common'
import { StorageService } from './storage.service'
import { UserProfileStore } from './UserProfileStore'
import { LocalDbModule } from '../local-db/local-db.module'
import { IpfsFileManagerModule } from '../ipfs-file-manager/ipfs-file-manager.module'

@Module({
imports: [LocalDbModule, IpfsFileManagerModule],
providers: [StorageService],
exports: [StorageService],
providers: [StorageService, UserProfileStore],
exports: [StorageService, UserProfileStore],
})
export class StorageModule {}
8 changes: 3 additions & 5 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export class StorageService extends EventEmitter {
private certificatesRequests: EventStore<string>
private communityMetadata: KeyValueStore<CommunityMetadata>

public userProfileStore: UserProfileStore

private ipfs: IPFS
private orbitDb: OrbitDB
private filesManager: IpfsFileManagerService
Expand All @@ -83,7 +81,8 @@ export class StorageService extends EventEmitter {
@Inject(QUIET_DIR) public readonly quietDir: string,
@Inject(ORBIT_DB_DIR) public readonly orbitDbDir: string,
@Inject(IPFS_REPO_PATCH) public readonly ipfsRepoPath: string,
private readonly lazyModuleLoader: LazyModuleLoader
private readonly lazyModuleLoader: LazyModuleLoader,
public readonly userProfileStore: UserProfileStore
) {
super()
}
Expand Down Expand Up @@ -233,8 +232,7 @@ export class StorageService extends EventEmitter {
this.logger('4/6')
await this.createDbForCommunityMetadata()
this.logger('5/6')
this.userProfileStore = new UserProfileStore(this.orbitDb)
await this.userProfileStore.init(this)
await this.userProfileStore.init(this.orbitDb, this)
this.logger('6/6')
await this.initAllChannels()
this.logger('Initialized DBs')
Expand Down

0 comments on commit 128405d

Please sign in to comment.