Skip to content

Commit

Permalink
feat: joining with v2 invitation link
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Mar 28, 2024
1 parent 3c6796d commit c6c9df4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { SocketModule } from '../socket/socket.module'
import { StorageModule } from '../storage/storage.module'
import { TorModule } from '../tor/tor.module'
import { ConnectionsManagerService } from './connections-manager.service'
import { ServerProxyServiceModule } from '../storageServerProxy/storageServerProxy.module'

@Module({
imports: [RegistrationModule, StorageModule, TorModule, SocketModule, LocalDbModule],
imports: [RegistrationModule, StorageModule, TorModule, SocketModule, LocalDbModule, ServerProxyServiceModule],
providers: [ConnectionsManagerService],
exports: [ConnectionsManagerService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
type PermsData,
type UserProfile,
type UserProfilesStoredEvent,
CreateNetworkPayload,
CommunityOwnership,
} from '@quiet/types'
import { CONFIG_OPTIONS, QUIET_DIR, SERVER_IO_PROVIDER, SOCKS_PROXY_AGENT } from '../const'
import { ConfigOptions, GetPorts, ServerIoProviderTypes } from '../types'
Expand All @@ -61,7 +63,9 @@ import { StorageEvents } from '../storage/storage.types'
import { LazyModuleLoader } from '@nestjs/core'
import Logger from '../common/logger'
import { emitError } from '../socket/socket.errors'
import { isPSKcodeValid } from '@quiet/common'
import { isPSKcodeValid, p2pAddressesToPairs } from '@quiet/common'
import { ServerProxyService } from '../storageServerProxy/storageServerProxy.service'
import { ServerStoredCommunityMetadata } from '../storageServerProxy/storageServerProxy.types'

@Injectable()
export class ConnectionsManagerService extends EventEmitter implements OnModuleInit {
Expand All @@ -79,6 +83,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
@Inject(SOCKS_PROXY_AGENT) public readonly socksProxyAgent: Agent,
private readonly socketService: SocketService,
private readonly registrationService: RegistrationService,
private readonly storageServerProxyService: ServerProxyService,
private readonly localDbService: LocalDbService,
private readonly storageService: StorageService,
private readonly tor: Tor,
Expand Down Expand Up @@ -514,6 +519,22 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.registrationService.setPermsData(payload)
})

this.socketService.on(
SocketActionTypes.DOWNLOAD_INVITE_DATA,
async (payload: { cid: string; serverAddress: string }, callback: (response: CreateNetworkPayload) => void) => {
this.logger(`socketService - ${SocketActionTypes.DOWNLOAD_INVITE_DATA}`)
this.storageServerProxyService.setServerAddress(payload.serverAddress)
const downloadedData = await this.storageServerProxyService.downloadData(payload.cid)
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: p2pAddressesToPairs(downloadedData.peerList),
psk: downloadedData.psk,
ownerOrbitDbIdentity: downloadedData.ownerOrbitDbIdentity,
}
callback(createNetworkPayload)
}
)

// Public Channels
this.socketService.on(
SocketActionTypes.CREATE_CHANNEL,
Expand Down
35 changes: 2 additions & 33 deletions packages/backend/src/nest/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ import {
type MessagesLoadedPayload,
type NetworkInfo,
CreateNetworkPayload,
CommunityOwnership,
} from '@quiet/types'
import EventEmitter from 'events'
import { CONFIG_OPTIONS, SERVER_IO_PROVIDER } from '../const'
import { ConfigOptions, ServerIoProviderTypes } from '../types'
import { suspendableSocketEvents } from './suspendable.events'
import Logger from '../common/logger'
import fetch from 'node-fetch'
import { ServerStoredCommunityMetadata } from '../storageServerProxy/storageServerProxy.types'
import { p2pAddressesToPairs } from '@quiet/common'

@Injectable()
export class SocketService extends EventEmitter implements OnModuleInit {
Expand Down Expand Up @@ -178,35 +174,8 @@ export class SocketService extends EventEmitter implements OnModuleInit {
socket.on(
SocketActionTypes.DOWNLOAD_INVITE_DATA,
async (payload: { serverAddress: string; cid: string }, callback: (response: CreateNetworkPayload) => void) => {
// FIXME
this.emit(
SocketActionTypes.DOWNLOAD_STORAGE_SERVER_DATA,
payload,
async (downloadedData: ServerStoredCommunityMetadata) => {
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: p2pAddressesToPairs(downloadedData.peerList), // TODO: prepare in storageServerProxy
psk: downloadedData.psk,
ownerOrbitDbIdentity: downloadedData.ownerOrbitDbIdentity,
}
const communityMetadataPayload: CommunityMetadata = {
id: downloadedData.id,
ownerCertificate: downloadedData.ownerCertificate,
rootCa: downloadedData.rootCa,
}
// TODO: Save the data to the store
this.emit(SocketActionTypes.SET_COMMUNITY_METADATA, communityMetadataPayload)
// TODO: emit metadata

callback(createNetworkPayload)
}
)
// callback({
// ownership: CommunityOwnership.User,
// peers: data.peerList,
// psk: data.psk,
// ownerOrbitDbIdentity: data.ownerOrbitDbIdentity,
// })
console.log('SOCKET Downloading invite data', payload)
this.emit(SocketActionTypes.DOWNLOAD_INVITE_DATA, payload, callback)
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
import { Injectable, OnModuleInit } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import EventEmitter from 'events'
import { ServerStoredCommunityMetadata } from './storageServerProxy.types'
import fetchRetry from 'fetch-retry'
import { SocketActionTypes } from '@quiet/types'
import Logger from '../common/logger'
const fetch = fetchRetry(global.fetch)
// TODO: handle errors
// TODO: handle retries
@Injectable()
export class ServerProxyService extends EventEmitter implements OnModuleInit {
export class ServerProxyService extends EventEmitter {
private readonly logger = Logger(ServerProxyService.name)
serverAddress: string

constructor() {
super()
}

onModuleInit() {
this.on(SocketActionTypes.SET_STORAGE_SERVER_ADDRESS, (payload: { serverAddress: string }) =>
this.setServerAddress(payload.serverAddress)
)
this.on(
SocketActionTypes.DOWNLOAD_STORAGE_SERVER_DATA,
async (payload: { cid: string }, callback: (downloadedData: ServerStoredCommunityMetadata) => void) => {
callback(await this.downloadData(payload.cid))
}
)
this.on(
SocketActionTypes.UPLOAD_STORAGE_SERVER_DATA,
async (payload: { cid: string; data: ServerStoredCommunityMetadata }, callback: (cid: string) => void) => {
callback(await this.uploadData(payload.cid, payload.data))
}
)
}

setServerAddress = (serverAddress: string) => {
this.serverAddress = serverAddress
}
Expand All @@ -52,46 +35,39 @@ export class ServerProxyService extends EventEmitter implements OnModuleInit {
}

auth = async () => {
console.log('Authenticating')
this.logger('Authenticating')
const authResponse = await fetch(this.authUrl, {
method: 'POST',
})
console.log('Auth response status', authResponse.status)
this.logger('Auth response status', authResponse.status)
const authResponseData = await authResponse.json()
console.log('Auth response data', authResponseData)
return authResponseData['access_token']
}

public downloadData = async (cid: string): Promise<ServerStoredCommunityMetadata> => {
console.log('Downloading data', cid)
this.logger('Downloading data', cid)
const accessToken = await this.auth()
const dataResponse = await fetch(this.getInviteUrl(cid), {
method: 'GET',
headers: { Authorization: this.getAuthorizationHeader(accessToken) },
})
const data = await dataResponse.json()
console.log('Downloaded data', data)
// this.emit('storageServerDataDownloaded', data)
const data: ServerStoredCommunityMetadata = await dataResponse.json()
this.logger('Downloaded data', data)
return data
}

public uploadData = async (cid: string, data: ServerStoredCommunityMetadata) => {
console.log('Uploading data', cid, data)
this.logger('Uploading data', cid, data)
const accessToken = await this.auth()
const putBody = JSON.stringify(data)
console.log('putBody', putBody)
const dataResponsePost = await fetch(this.getInviteUrl(cid), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: this.getAuthorizationHeader(accessToken),
},
body: putBody,
body: JSON.stringify(data),
})
console.log('Upload data response status', dataResponsePost)
// if (dataResponsePost.status === 200) {
// this.emit('storageServerDataUploaded', { cid })
// }
this.logger('Upload data response status', dataResponsePost)
return cid
}
}

0 comments on commit c6c9df4

Please sign in to comment.