-
Notifications
You must be signed in to change notification settings - Fork 88
/
startConnection.saga.ts
295 lines (288 loc) · 12.3 KB
/
startConnection.saga.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { eventChannel } from 'redux-saga'
import { type Socket } from '../../../types'
import { all, call, fork, put, takeEvery } from 'typed-redux-saga'
import logger from '../../../utils/logger'
import { appMasterSaga } from '../../app/app.master.saga'
import { connectionActions } from '../../appConnection/connection.slice'
import { communitiesMasterSaga } from '../../communities/communities.master.saga'
import { connectionMasterSaga } from '../../appConnection/connection.master.saga'
import { communitiesActions } from '../../communities/communities.slice'
import { errorsMasterSaga } from '../../errors/errors.master.saga'
import { errorsActions } from '../../errors/errors.slice'
import { identityMasterSaga } from '../../identity/identity.master.saga'
import { identityActions } from '../../identity/identity.slice'
import { messagesMasterSaga } from '../../messages/messages.master.saga'
import { filesMasterSaga } from '../../files/files.master.saga'
import { messagesActions } from '../../messages/messages.slice'
import { publicChannelsMasterSaga } from '../../publicChannels/publicChannels.master.saga'
import { publicChannelsActions } from '../../publicChannels/publicChannels.slice'
import { usersActions } from '../../users/users.slice'
import { filesActions } from '../../files/files.slice'
import { networkActions } from '../../network/network.slice'
import {
type ResponseCreateCommunityPayload,
type ResponseRegistrarPayload,
type StorePeerListPayload,
type ResponseCreateNetworkPayload,
type ResponseLaunchCommunityPayload,
type ChannelDeletionResponsePayload,
type ChannelMessagesIdsResponse,
type ChannelsReplicatedPayload,
type CommunityId,
type CreatedChannelResponse,
type DownloadStatus,
type ErrorPayload,
type FileMetadata,
type IncomingMessages,
type NetworkDataPayload,
type RemoveDownloadStatus,
type SendCertificatesResponse,
type SetChannelSubscribedPayload,
SocketActionTypes,
type SavedOwnerCertificatePayload,
type SendOwnerCertificatePayload,
CommunityMetadata,
SendCsrsResponse,
} from '@quiet/types'
const log = logger('socket')
export function subscribe(socket: Socket) {
return eventChannel<
| ReturnType<typeof messagesActions.incomingMessages>
| ReturnType<typeof messagesActions.responseSendMessagesIds>
| ReturnType<typeof messagesActions.removePendingMessageStatus>
| ReturnType<typeof messagesActions.addPublicChannelsMessagesBase>
| ReturnType<typeof publicChannelsActions.addChannel>
| ReturnType<typeof publicChannelsActions.setChannelSubscribed>
| ReturnType<typeof publicChannelsActions.sendInitialChannelMessage>
| ReturnType<typeof publicChannelsActions.sendNewUserInfoMessage>
| ReturnType<typeof publicChannelsActions.channelsReplicated>
| ReturnType<typeof publicChannelsActions.createGeneralChannel>
| ReturnType<typeof publicChannelsActions.channelDeletionResponse>
| ReturnType<typeof usersActions.responseSendCertificates>
| ReturnType<typeof usersActions.storeCsrs>
| ReturnType<typeof communitiesActions.responseCreateNetwork>
| ReturnType<typeof errorsActions.addError>
| ReturnType<typeof errorsActions.handleError>
| ReturnType<typeof identityActions.storeUserCertificate>
| ReturnType<typeof identityActions.throwIdentityError>
| ReturnType<typeof identityActions.saveOwnerCertToDb>
| ReturnType<typeof identityActions.savedOwnerCertificate>
| ReturnType<typeof communitiesActions.storePeerList>
| ReturnType<typeof communitiesActions.updateCommunity>
| ReturnType<typeof communitiesActions.responseRegistrar>
| ReturnType<typeof communitiesActions.launchRegistrar>
| ReturnType<typeof communitiesActions.launchCommunity>
| ReturnType<typeof communitiesActions.addOwnerCertificate>
| ReturnType<typeof networkActions.addInitializedCommunity>
| ReturnType<typeof networkActions.addInitializedRegistrar>
| ReturnType<typeof networkActions.removeConnectedPeer>
| ReturnType<typeof connectionActions.updateNetworkData>
| ReturnType<typeof networkActions.addConnectedPeers>
| ReturnType<typeof filesActions.broadcastHostedFile>
| ReturnType<typeof filesActions.updateMessageMedia>
| ReturnType<typeof filesActions.updateDownloadStatus>
| ReturnType<typeof filesActions.removeDownloadStatus>
| ReturnType<typeof filesActions.checkForMissingFiles>
| ReturnType<typeof connectionActions.setTorBootstrapProcess>
| ReturnType<typeof connectionActions.setTorConnectionProcess>
| ReturnType<typeof connectionActions.torBootstrapped>
| ReturnType<typeof communitiesActions.clearInvitationCodes>
| ReturnType<typeof identityActions.saveUserCsr>
| ReturnType<typeof connectionActions.setTorInitialized>
| ReturnType<typeof communitiesActions.saveCommunityMetadata>
| ReturnType<typeof communitiesActions.sendCommunityMetadata>
>(emit => {
// UPDATE FOR APP
socket.on(SocketActionTypes.TOR_INITIALIZED, () => {
emit(connectionActions.setTorInitialized())
})
socket.on(SocketActionTypes.CONNECTION_PROCESS_INFO, (payload: string) => {
emit(connectionActions.setTorConnectionProcess(payload))
})
// Misc
socket.on(SocketActionTypes.PEER_CONNECTED, (payload: { peers: string[] }) => {
log({ payload })
emit(networkActions.addConnectedPeers(payload.peers))
})
socket.on(SocketActionTypes.PEER_DISCONNECTED, (payload: NetworkDataPayload) => {
emit(networkActions.removeConnectedPeer(payload.peer))
emit(connectionActions.updateNetworkData(payload))
})
// Files
socket.on(SocketActionTypes.UPDATE_MESSAGE_MEDIA, (payload: FileMetadata) => {
emit(filesActions.updateMessageMedia(payload))
})
socket.on(SocketActionTypes.UPLOADED_FILE, (payload: FileMetadata) => {
emit(filesActions.broadcastHostedFile(payload))
})
socket.on(SocketActionTypes.DOWNLOAD_PROGRESS, (payload: DownloadStatus) => {
emit(filesActions.updateDownloadStatus(payload))
})
socket.on(SocketActionTypes.REMOVE_DOWNLOAD_STATUS, (payload: RemoveDownloadStatus) => {
emit(filesActions.removeDownloadStatus(payload))
})
// Channels
socket.on(SocketActionTypes.CHANNELS_REPLICATED, (payload: ChannelsReplicatedPayload) => {
emit(publicChannelsActions.channelsReplicated(payload))
})
socket.on(SocketActionTypes.CHANNEL_SUBSCRIBED, (payload: SetChannelSubscribedPayload) => {
emit(publicChannelsActions.setChannelSubscribed(payload))
})
socket.on(SocketActionTypes.CHANNEL_DELETION_RESPONSE, (payload: ChannelDeletionResponsePayload) => {
emit(publicChannelsActions.channelDeletionResponse(payload))
})
socket.on(SocketActionTypes.CREATED_CHANNEL, (payload: CreatedChannelResponse) => {
emit(
messagesActions.addPublicChannelsMessagesBase({
channelId: payload.channel.id,
})
)
emit(publicChannelsActions.addChannel(payload))
emit(
publicChannelsActions.sendInitialChannelMessage({
channelName: payload.channel.name,
channelId: payload.channel.id,
})
)
})
// Messages
socket.on(SocketActionTypes.SEND_MESSAGES_IDS, (payload: ChannelMessagesIdsResponse) => {
emit(messagesActions.responseSendMessagesIds(payload))
})
socket.on(SocketActionTypes.INCOMING_MESSAGES, (payload: IncomingMessages) => {
const { messages } = payload
for (const message of messages) {
emit(messagesActions.removePendingMessageStatus(message.id))
}
emit(messagesActions.incomingMessages(payload))
})
socket.on(SocketActionTypes.CHECK_FOR_MISSING_FILES, (payload: CommunityId) => {
emit(filesActions.checkForMissingFiles(payload))
})
// Community
socket.on(SocketActionTypes.NEW_COMMUNITY, (_payload: ResponseCreateCommunityPayload) => {
console.log('on SocketActionTypes.NEW_COMMUNITY')
emit(identityActions.saveOwnerCertToDb())
emit(publicChannelsActions.createGeneralChannel())
})
socket.on(SocketActionTypes.REGISTRAR, (payload: ResponseRegistrarPayload) => {
console.log('SocketActionTypes.REGISTRAR')
log(SocketActionTypes.REGISTRAR, payload)
emit(communitiesActions.responseRegistrar(payload))
emit(networkActions.addInitializedRegistrar(payload.id))
})
socket.on(SocketActionTypes.PEER_LIST, (payload: StorePeerListPayload) => {
emit(communitiesActions.storePeerList(payload))
})
socket.on(SocketActionTypes.NETWORK, (payload: ResponseCreateNetworkPayload) => {
log(SocketActionTypes.NETWORK, payload)
emit(communitiesActions.responseCreateNetwork(payload))
})
socket.on(SocketActionTypes.COMMUNITY, (payload: ResponseLaunchCommunityPayload) => {
console.log('Hunting for heisenbug: Community event received in state-manager')
emit(communitiesActions.launchRegistrar(payload.id))
emit(identityActions.saveUserCsr())
emit(filesActions.checkForMissingFiles(payload.id))
emit(networkActions.addInitializedCommunity(payload.id))
emit(communitiesActions.clearInvitationCodes())
// For backward compatibility (old community):
emit(communitiesActions.sendCommunityMetadata())
})
// Errors
socket.on(SocketActionTypes.ERROR, (payload: ErrorPayload) => {
log(payload)
emit(errorsActions.handleError(payload))
})
// Certificates
socket.on(SocketActionTypes.RESPONSE_GET_CSRS, (payload: SendCsrsResponse) => {
emit(usersActions.storeCsrs(payload))
})
socket.on(SocketActionTypes.RESPONSE_GET_CERTIFICATES, (payload: SendCertificatesResponse) => {
emit(
publicChannelsActions.sendNewUserInfoMessage({
certificates: payload.certificates,
})
)
emit(usersActions.responseSendCertificates(payload))
})
socket.on(SocketActionTypes.SEND_USER_CERTIFICATE, (payload: SendOwnerCertificatePayload) => {
console.log('Received SEND_USER_CERTIFICATE', payload.communityId)
emit(
communitiesActions.addOwnerCertificate({
communityId: payload.communityId,
ownerCertificate: payload.payload.ownerCert,
})
)
emit(
communitiesActions.storePeerList({
communityId: payload.communityId,
peerList: payload.payload.peers,
})
)
emit(
identityActions.storeUserCertificate({
userCertificate: payload.payload.certificate,
communityId: payload.communityId,
})
)
emit(
communitiesActions.updateCommunity({
id: payload.communityId,
rootCa: payload.payload.rootCa,
})
)
emit(communitiesActions.launchCommunity(payload.communityId))
})
socket.on(SocketActionTypes.SAVED_OWNER_CERTIFICATE, (payload: SavedOwnerCertificatePayload) => {
console.log('Received SAVED_OWNER_CERTIFICATE', payload.communityId)
emit(
communitiesActions.addOwnerCertificate({
communityId: payload.communityId,
ownerCertificate: payload.network.certificate,
})
)
emit(
communitiesActions.storePeerList({
communityId: payload.communityId,
peerList: payload.network.peers,
})
)
emit(
identityActions.storeUserCertificate({
userCertificate: payload.network.certificate,
communityId: payload.communityId,
})
)
emit(identityActions.savedOwnerCertificate(payload.communityId))
})
socket.on(SocketActionTypes.SAVE_COMMUNITY_METADATA, (payload: CommunityMetadata) => {
console.log('SAVE COMMUNITY METADATA', payload)
emit(
communitiesActions.saveCommunityMetadata({
rootCa: payload.rootCa,
ownerCertificate: payload.ownerCertificate,
})
)
})
return () => undefined
})
}
export function* handleActions(socket: Socket): Generator {
const socketChannel = yield* call(subscribe, socket)
yield takeEvery(socketChannel, function* (action) {
yield put(action)
})
}
export function* useIO(socket: Socket): Generator {
yield all([
fork(handleActions, socket),
fork(publicChannelsMasterSaga, socket),
fork(messagesMasterSaga, socket),
fork(filesMasterSaga, socket),
fork(identityMasterSaga, socket),
fork(communitiesMasterSaga, socket),
fork(appMasterSaga, socket),
fork(connectionMasterSaga),
fork(errorsMasterSaga),
])
}