Skip to content

Commit 9f04bdf

Browse files
author
Lucas Leblow
authored
fix: Wait for channels to be initialized before sending messages (#2448)
1 parent 2aa2e3e commit 9f04bdf

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

packages/state-manager/src/sagas/messages/sendMessage/sendMessage.saga.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Socket, applyEmitParams } from '../../../types'
22
import { type PayloadAction } from '@reduxjs/toolkit'
33
import { sign, loadPrivateKey, pubKeyFromCsr } from '@quiet/identity'
4-
import { call, select, apply, put } from 'typed-redux-saga'
4+
import { call, select, apply, put, delay } from 'typed-redux-saga'
55
import { arrayBufferToString } from 'pvutils'
66
import { config } from '../../users/const/certFieldTypes'
77
import { identitySelectors } from '../../identity/identity.selectors'
@@ -75,6 +75,20 @@ export function* sendMessageSaga(
7575
const isUploadingFileMessage = action.payload.media?.cid?.includes('uploading')
7676
if (isUploadingFileMessage) return // Do not broadcast message until file is uploaded
7777

78+
// Wait until we have subscribed to the channel
79+
//
80+
// TODO: I think we probably want to revise how we are sending
81+
// messages by having the backend handling queueing and retrying
82+
// (in a durable way).
83+
while (true) {
84+
const subscribedChannels = yield* select(publicChannelsSelectors.subscribedChannels)
85+
if (subscribedChannels.includes(channelId)) {
86+
break
87+
}
88+
console.error('Failed to send message, channel not subscribed. Retrying...')
89+
yield* delay(500)
90+
}
91+
7892
yield* apply(
7993
socket,
8094
socket.emit,
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { put, select, call, delay } from 'typed-redux-saga'
1+
import { put, select, call } from 'typed-redux-saga'
22
import { messagesActions } from '../../messages/messages.slice'
33
import { publicChannelsSelectors } from '../publicChannels.selectors'
44
import { WriteMessagePayload, MessageType, PublicChannel, PublicChannelStorage } from '@quiet/types'
@@ -24,24 +24,6 @@ export function* sendIntroductionMessageSaga(): Generator {
2424
channelId: generalChannel.id,
2525
}
2626

27-
// FIXME: This is a quick fix for an issue that can be fixed by
28-
// unifying CHANNELS_STORED and CHANNELS_SUBSCRIBED events and
29-
// refactoring a bit. The problem is that the frontend sends a
30-
// message upon receiving the CHANNELS_STORED event, but the channel
31-
// hasn't been fully initialized/subscribed yet (it doesn't exist in
32-
// publicChannelsRepos on the backend so the backend fails to send
33-
// it). Ideally, I think we should only tell the frontend about
34-
// channels once they've been fully initialized. Once we fix that,
35-
// we can remove the following code.
36-
while (true) {
37-
const subscribedChannels = yield* select(publicChannelsSelectors.subscribedChannels)
38-
if (subscribedChannels.includes(generalChannel.id)) {
39-
break
40-
}
41-
console.error('Failed to send introduction message, general channel not subscribed. Retrying...')
42-
yield* delay(500)
43-
}
44-
4527
yield* put(messagesActions.sendMessage(payload))
4628
yield* put(identityActions.updateIdentity({ ...identity, introMessageSent: true }))
4729
}

0 commit comments

Comments
 (0)