Skip to content

Commit

Permalink
refactor: Remove DOWNLOAD_INVITE_DATA event; remove joinNetwork saga;…
Browse files Browse the repository at this point in the history
… pass whole invitation data to createNetwork saga
  • Loading branch information
EmiM committed Apr 15, 2024
1 parent 3f1051e commit 858065f
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
SaveCSRPayload,
SendCertificatesResponse,
SendMessagePayload,
ServerInvitationData,
SocketActionTypes,
UploadFilePayload,
type DeleteChannelResponse,
Expand Down Expand Up @@ -392,7 +393,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
return community
}

public async downloadCommunityData(inviteData: any) {
public async downloadCommunityData(inviteData: ServerInvitationData) {
this.logger('Downloading invite data', inviteData)
this.storageServerProxyService.setServerAddress(inviteData.serverAddress)
let downloadedData: ServerStoredCommunityMetadata
Expand Down Expand Up @@ -422,7 +423,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
const downloadedData = await this.downloadCommunityData(inviteData)
if (!downloadedData) {
emitError(this.serverIoProvider.io, {
type: SocketActionTypes.DOWNLOAD_INVITE_DATA,
type: SocketActionTypes.LAUNCH_COMMUNITY,
message: ErrorMessages.STORAGE_SERVER_CONNECTION_FAILED,
})
return
Expand Down Expand Up @@ -664,33 +665,6 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
await this.storageService?.saveCSR(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)
let downloadedData: ServerStoredCommunityMetadata
try {
downloadedData = await this.storageServerProxyService.downloadData(payload.cid)
} catch (e) {
this.logger.error(`Downloading community data failed`, e)
emitError(this.serverIoProvider.io, {
type: SocketActionTypes.DOWNLOAD_INVITE_DATA,
message: ErrorMessages.STORAGE_SERVER_CONNECTION_FAILED,
})
return
}

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
8 changes: 0 additions & 8 deletions packages/backend/src/nest/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@ export class SocketService extends EventEmitter implements OnModuleInit {
}
)

socket.on(
SocketActionTypes.DOWNLOAD_INVITE_DATA,
async (payload: { serverAddress: string; cid: string }, callback: (response: CreateNetworkPayload) => void) => {
console.log('SOCKET Downloading invite data', payload)
this.emit(SocketActionTypes.DOWNLOAD_INVITE_DATA, payload, callback)
}
)

socket.on(SocketActionTypes.LEAVE_COMMUNITY, async () => {
this.logger('Leaving community')
this.emit(SocketActionTypes.LEAVE_COMMUNITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,34 @@ describe('Handle invitation code', () => {
})

it('joins network if code is valid', async () => {
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
inviteData: validInvitationData,
}
await expectSaga(customProtocolSaga, communities.actions.customProtocol([validInvitationDeepUrl]))
.withState(store.getState())
.put(communities.actions.joinNetwork(validInvitationData))
.put(communities.actions.createNetwork(createNetworkPayload))
.run()
})

it('joins network if v2 code is valid', async () => {
const validInvitationData = getValidInvitationUrlTestData(validInvitationDatav2[0]).data
const validInvitationDeepUrl = getValidInvitationUrlTestData(validInvitationDatav2[0]).deepUrl()
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
inviteData: validInvitationData,
}
await expectSaga(customProtocolSaga, communities.actions.customProtocol([validInvitationDeepUrl]))
.withState(store.getState())
.put(communities.actions.joinNetwork(validInvitationData))
.put(communities.actions.createNetwork(createNetworkPayload))
.run()
})

it('does not try to create network if user is already in community', async () => {
community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const payload: CreateNetworkPayload = {
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: validInvitationData.pairs,
psk: validInvitationData.psk,
inviteData: validInvitationData,
}

await expectSaga(customProtocolSaga, communities.actions.customProtocol([validInvitationDeepUrl]))
Expand All @@ -68,14 +75,14 @@ describe('Handle invitation code', () => {
},
})
)
.not.put(communities.actions.createNetwork(payload))
.not.put(communities.actions.createNetwork(createNetworkPayload))
.run()
})

it('does not try to create network if code is missing psk', async () => {
const payload: CreateNetworkPayload = {
it('does not try to create network if code is missing data', async () => {
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: [],
inviteData: validInvitationData,
}

await expectSaga(
Expand All @@ -93,34 +100,7 @@ describe('Handle invitation code', () => {
},
})
)
.not.put(communities.actions.createNetwork(payload))
.run()
})

it('does not try to create network if code is missing psk', async () => {
const payload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: [],
}

await expectSaga(
customProtocolSaga,
communities.actions.customProtocol([
'quiet://?QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
])
)
.withState(store.getState())
.put(communities.actions.clearInvitationCodes())
.put(
modalsActions.openModal({
name: ModalName.warningModal,
args: {
title: 'Invalid link',
subtitle: 'The invite link you received is not valid. Please check it and try again.',
},
})
)
.not.put(communities.actions.createNetwork(payload))
.not.put(communities.actions.createNetwork(createNetworkPayload))
.run()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,4 @@ export function* customProtocolSaga(
}

yield* put(communities.actions.createNetwork(payload))

// yield* put(communities.actions.joinNetwork(data))
}
3 changes: 1 addition & 2 deletions packages/desktop/src/rtl-tests/deep.linking.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ describe('Deep linking', () => {
expect(actions).toMatchInlineSnapshot(`
Array [
"Communities/customProtocol",
"Communities/joinNetwork",
"Communities/createNetwork",
"Communities/setInvitationCodes",
"Communities/addNewCommunity",
"Communities/setCurrentCommunity",
"Communities/setInvitationCodes",
"Communities/customProtocol",
]
`)
Expand Down
47 changes: 26 additions & 21 deletions packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { initActions } from '../init.slice'
import { navigationActions } from '../../navigation/navigation.slice'
import { ScreenNames } from '../../../const/ScreenNames.enum'
import { deepLinkSaga } from './deepLink.saga'
import { type Community, CommunityOwnership, type Identity, InvitationData, InvitationDataVersion } from '@quiet/types'
import {
type Community,
CommunityOwnership,
type Identity,
InvitationData,
InvitationDataVersion,
CreateNetworkPayload,
} from '@quiet/types'
import {
composeInvitationShareUrl,
validInvitationCodeTestData,
Expand Down Expand Up @@ -58,17 +65,16 @@ describe('deepLinkSaga', () => {
socketIOSecret: 'secret',
})
)
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
inviteData: validData,
}
const reducer = combineReducers(reducers)
await expectSaga(deepLinkSaga, initActions.deepLink(validCode))
.withReducer(reducer)
.withState(store.getState())
.put(initActions.resetDeepLink())
.put(
communities.actions.joinNetwork({
version: InvitationDataVersion.v1,
...validData,
})
)
.put(communities.actions.createNetwork(createNetworkPayload))
.put(
navigationActions.replaceScreen({
screen: ScreenNames.UsernameRegistrationScreen,
Expand Down Expand Up @@ -109,9 +115,7 @@ describe('deepLinkSaga', () => {
.not.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validData.pairs,
psk: validData.psk,
ownerOrbitDbIdentity: validData.ownerOrbitDbIdentity,
inviteData: validData,
})
)
.run()
Expand All @@ -127,6 +131,11 @@ describe('deepLinkSaga', () => {

community.psk = validData.psk

const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
inviteData: validData,
}

store.dispatch(communities.actions.addNewCommunity(community))

store.dispatch(communities.actions.setCurrentCommunity(community.id))
Expand All @@ -149,11 +158,8 @@ describe('deepLinkSaga', () => {
})
.put.like({
action: {
type: communities.actions.joinNetwork.type,
payload: {
version: InvitationDataVersion.v1,
...validData,
},
type: communities.actions.createNetwork.type,
payload: createNetworkPayload,
},
})
.run()
Expand All @@ -170,6 +176,10 @@ describe('deepLinkSaga', () => {
psk: 'BNlxfE=',
ownerOrbitDbIdentity: 'testId',
}
const createNetworkPayload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
inviteData: invalidData,
}
const invalidCode = composeInvitationShareUrl(invalidData)
store.dispatch(
initActions.setWebsocketConnected({
Expand All @@ -193,12 +203,7 @@ describe('deepLinkSaga', () => {
},
},
})
.not.put(
communities.actions.joinNetwork({
version: InvitationDataVersion.v1,
...validData,
})
)
.not.put(communities.actions.createNetwork(createNetworkPayload))
.run()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { connectionActions } from '../appConnection/connection.slice'
import { createCommunitySaga } from './createCommunity/createCommunity.saga'
import { initCommunities, launchCommunitySaga } from './launchCommunity/launchCommunity.saga'
import { createNetworkSaga } from './createNetwork/createNetwork.saga'
import { joinNetworkSaga } from './joinNetwork/joinNetwork.saga'

export function* communitiesMasterSaga(socket: Socket): Generator {
yield all([
takeEvery(communitiesActions.createNetwork.type, createNetworkSaga, socket),
takeEvery(communitiesActions.joinNetwork.type, joinNetworkSaga, socket),
takeEvery(connectionActions.torBootstrapped.type, initCommunities),
takeEvery(communitiesActions.createCommunity.type, createCommunitySaga, socket),
takeEvery(communitiesActions.launchCommunity.type, launchCommunitySaga, socket),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const communitiesSlice = createSlice({
})
},
createNetwork: (state, _action: PayloadAction<CreateNetworkPayload>) => state,
joinNetwork: (state, _action: PayloadAction<InvitationData>) => state,
resetApp: (state, _action) => state,
createCommunity: (state, _action: PayloadAction<string>) => state,
launchCommunity: (state, _action: PayloadAction<string>) => state,
Expand Down
Loading

0 comments on commit 858065f

Please sign in to comment.