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

chore(2631): Refactor storage service to separate channel and message logic #2713

Open
wants to merge 8 commits into
base: 4.0.0
Choose a base branch
from
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
"string-replace-loader": "3.1.0",
"ts-jest-resolver": "^2.0.0",
"uint8arrays": "^5.1.0",
"uint8arraylist": "^2.4.8",
"utf-8-validate": "^5.0.2",
"validator": "^13.11.0",
"ws": "^8.18.0"
Expand Down
13 changes: 7 additions & 6 deletions packages/backend/src/nest/auth/sigchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ export class SigChainService implements OnModuleInit {
* @param setActive Whether to set the chain as active
* @returns Whether the chain was set as active
*/
addChain(chain: SigChain, setActive: boolean): boolean {
if (this.chains.has(chain.team.teamName)) {
throw new Error(`Chain for team ${chain.team.teamName} already exists`)
addChain(chain: SigChain, setActive: boolean, teamName?: string): boolean {
const name = teamName || chain.team!.teamName
if (this.chains.has(name)) {
throw new Error(`Chain for team ${teamName} already exists`)
}
this.chains.set(chain.team.teamName, chain)
this.chains.set(name, chain)
if (setActive) {
this.setActiveChain(chain.team.teamName)
this.setActiveChain(name)
return true
}
return false
Expand Down Expand Up @@ -119,7 +120,7 @@ export class SigChainService implements OnModuleInit {
): Promise<SigChain> {
this.logger.info('Deserializing chain')
const sigChain = SigChain.load(serializedTeam, context, teamKeyRing)
this.addChain(sigChain, setActive)
this.addChain(sigChain, setActive, sigChain.team?.teamName)
return sigChain
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI

if (community.name) {
try {
this.logger.info(`Loading chain for team name ${community.name}`)
await this.sigChainService.loadChain(community.name, true)
} catch (e) {
this.logger.warn('Failed to load sigchain', e)
Expand Down Expand Up @@ -706,6 +707,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI

const community = {
id: payload.id,
name: payload.name,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrastaea This is all you need to do to fix the chain loading

peerList: [...new Set([localAddress, ...metadata.peers])],
psk: metadata.psk,
ownerOrbitDbIdentity: metadata.ownerOrbitDbIdentity,
Expand Down Expand Up @@ -899,7 +901,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.serverIoProvider.io.emit(SocketActionTypes.CERTIFICATES_STORED, {
certificates: await this.storageService?.loadAllCertificates(),
})
await this.storageService?.loadAllChannels()
await this.storageService?.channels.loadAllChannels()
}
})
this.socketService.on(
Expand Down Expand Up @@ -954,7 +956,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.socketService.on(
SocketActionTypes.CREATE_CHANNEL,
async (args: CreateChannelPayload, callback: (response?: CreateChannelResponse) => void) => {
callback(await this.storageService?.subscribeToChannel(args.channel))
callback(await this.storageService?.channels.subscribeToChannel(args.channel))
}
)
this.socketService.on(
Expand All @@ -963,39 +965,39 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
payload: { channelId: string; ownerPeerId: string },
callback: (response: DeleteChannelResponse) => void
) => {
callback(await this.storageService?.deleteChannel(payload))
callback(await this.storageService?.channels.deleteChannel(payload))
}
)
this.socketService.on(
SocketActionTypes.DELETE_FILES_FROM_CHANNEL,
async (payload: DeleteFilesFromChannelSocketPayload) => {
this.logger.info(`socketService - ${SocketActionTypes.DELETE_FILES_FROM_CHANNEL}`)
await this.storageService?.deleteFilesFromChannel(payload)
await this.storageService?.channels.deleteFilesFromChannel(payload)
// await this.deleteFilesFromTemporaryDir() //crashes on mobile, will be fixes in next versions
}
)
this.socketService.on(SocketActionTypes.SEND_MESSAGE, async (args: SendMessagePayload) => {
await this.storageService?.sendMessage(args.message)
await this.storageService?.channels.sendMessage(args.message)
})
this.socketService.on(
SocketActionTypes.GET_MESSAGES,
async (payload: GetMessagesPayload, callback: (response?: MessagesLoadedPayload) => void) => {
callback(await this.storageService?.getMessages(payload.channelId, payload.ids))
callback(await this.storageService?.channels.getMessages(payload.channelId, payload.ids))
}
)

// Files
this.socketService.on(SocketActionTypes.DOWNLOAD_FILE, async (metadata: FileMetadata) => {
await this.storageService?.downloadFile(metadata)
await this.storageService?.channels.downloadFile(metadata)
})
this.socketService.on(SocketActionTypes.UPLOAD_FILE, async (metadata: FileMetadata) => {
await this.storageService?.uploadFile(metadata)
await this.storageService?.channels.uploadFile(metadata)
})
this.socketService.on(SocketActionTypes.FILE_UPLOADED, async (args: FileMetadata) => {
await this.storageService?.uploadFile(args)
await this.storageService?.channels.uploadFile(args)
})
this.socketService.on(SocketActionTypes.CANCEL_DOWNLOAD, mid => {
this.storageService?.cancelDownload(mid)
this.storageService?.channels.cancelDownload(mid)
})

// System
Expand All @@ -1018,37 +1020,37 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.logger.info(`Storage - ${StorageEvents.CERTIFICATES_STORED}`)
this.serverIoProvider.io.emit(SocketActionTypes.CERTIFICATES_STORED, payload)
})
this.storageService.on(StorageEvents.CHANNELS_STORED, (payload: ChannelsReplicatedPayload) => {
this.storageService.channels.on(StorageEvents.CHANNELS_STORED, (payload: ChannelsReplicatedPayload) => {
this.serverIoProvider.io.emit(SocketActionTypes.CHANNELS_STORED, payload)
})
this.storageService.on(StorageEvents.MESSAGES_STORED, (payload: MessagesLoadedPayload) => {
this.storageService.channels.on(StorageEvents.MESSAGES_STORED, (payload: MessagesLoadedPayload) => {
this.serverIoProvider.io.emit(SocketActionTypes.MESSAGES_STORED, payload)
})
this.storageService.on(StorageEvents.MESSAGE_IDS_STORED, (payload: ChannelMessageIdsResponse) => {
this.storageService.channels.on(StorageEvents.MESSAGE_IDS_STORED, (payload: ChannelMessageIdsResponse) => {
if (payload.ids.length === 0) {
return
}
this.serverIoProvider.io.emit(SocketActionTypes.MESSAGE_IDS_STORED, payload)
})
this.storageService.on(StorageEvents.CHANNEL_SUBSCRIBED, (payload: ChannelSubscribedPayload) => {
this.storageService.channels.on(StorageEvents.CHANNEL_SUBSCRIBED, (payload: ChannelSubscribedPayload) => {
this.serverIoProvider.io.emit(SocketActionTypes.CHANNEL_SUBSCRIBED, payload)
})
this.storageService.on(StorageEvents.REMOVE_DOWNLOAD_STATUS, (payload: RemoveDownloadStatus) => {
this.storageService.channels.on(StorageEvents.REMOVE_DOWNLOAD_STATUS, (payload: RemoveDownloadStatus) => {
this.serverIoProvider.io.emit(SocketActionTypes.REMOVE_DOWNLOAD_STATUS, payload)
})
this.storageService.on(StorageEvents.FILE_UPLOADED, (payload: UploadFilePayload) => {
this.storageService.channels.on(StorageEvents.FILE_UPLOADED, (payload: UploadFilePayload) => {
this.serverIoProvider.io.emit(SocketActionTypes.FILE_UPLOADED, payload)
})
this.storageService.on(StorageEvents.DOWNLOAD_PROGRESS, (payload: DownloadStatus) => {
this.storageService.channels.on(StorageEvents.DOWNLOAD_PROGRESS, (payload: DownloadStatus) => {
this.serverIoProvider.io.emit(SocketActionTypes.DOWNLOAD_PROGRESS, payload)
})
this.storageService.on(StorageEvents.MESSAGE_MEDIA_UPDATED, (payload: FileMetadata) => {
this.storageService.channels.on(StorageEvents.MESSAGE_MEDIA_UPDATED, (payload: FileMetadata) => {
this.serverIoProvider.io.emit(SocketActionTypes.MESSAGE_MEDIA_UPDATED, payload)
})
this.storageService.on(StorageEvents.COMMUNITY_UPDATED, (payload: Community) => {
this.storageService.channels.on(StorageEvents.COMMUNITY_UPDATED, (payload: Community) => {
this.serverIoProvider.io.emit(SocketActionTypes.COMMUNITY_UPDATED, payload)
})
this.storageService.on(StorageEvents.SEND_PUSH_NOTIFICATION, (payload: PushNotificationPayload) => {
this.storageService.channels.on(StorageEvents.SEND_PUSH_NOTIFICATION, (payload: PushNotificationPayload) => {
this.serverIoProvider.io.emit(SocketActionTypes.PUSH_NOTIFICATION, payload)
})
this.storageService.on(StorageEvents.CSRS_STORED, async (payload: { csrs: string[] }) => {
Expand Down
Empty file.
Loading