Skip to content

Commit

Permalink
fix: Wait for channels to be initialized before sending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
leblowl committed Apr 16, 2024
1 parent 2aa2e3e commit 6f6f8f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Socket, applyEmitParams } from '../../../types'
import { type PayloadAction } from '@reduxjs/toolkit'
import { sign, loadPrivateKey, pubKeyFromCsr } from '@quiet/identity'
import { call, select, apply, put } from 'typed-redux-saga'
import { call, select, apply, put, delay } from 'typed-redux-saga'
import { arrayBufferToString } from 'pvutils'
import { config } from '../../users/const/certFieldTypes'
import { identitySelectors } from '../../identity/identity.selectors'
Expand Down Expand Up @@ -75,6 +75,20 @@ export function* sendMessageSaga(
const isUploadingFileMessage = action.payload.media?.cid?.includes('uploading')
if (isUploadingFileMessage) return // Do not broadcast message until file is uploaded

// Wait until we have subscribed to the channel
//
// TODO: I think we probably want to revise how we are sending
// messages by having the backend handling queueing and retrying
// (in a durable way).
while (true) {
const subscribedChannels = yield* select(publicChannelsSelectors.subscribedChannels)
if (subscribedChannels.includes(channelId)) {
break
}
console.error('Failed to send message, channel not subscribed. Retrying...')
yield* delay(500)
}

yield* apply(
socket,
socket.emit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { put, select, call, delay } from 'typed-redux-saga'
import { put, select, call } from 'typed-redux-saga'
import { messagesActions } from '../../messages/messages.slice'
import { publicChannelsSelectors } from '../publicChannels.selectors'
import { WriteMessagePayload, MessageType, PublicChannel, PublicChannelStorage } from '@quiet/types'
Expand All @@ -24,24 +24,6 @@ export function* sendIntroductionMessageSaga(): Generator {
channelId: generalChannel.id,
}

// FIXME: This is a quick fix for an issue that can be fixed by
// unifying CHANNELS_STORED and CHANNELS_SUBSCRIBED events and
// refactoring a bit. The problem is that the frontend sends a
// message upon receiving the CHANNELS_STORED event, but the channel
// hasn't been fully initialized/subscribed yet (it doesn't exist in
// publicChannelsRepos on the backend so the backend fails to send
// it). Ideally, I think we should only tell the frontend about
// channels once they've been fully initialized. Once we fix that,
// we can remove the following code.
while (true) {
const subscribedChannels = yield* select(publicChannelsSelectors.subscribedChannels)
if (subscribedChannels.includes(generalChannel.id)) {
break
}
console.error('Failed to send introduction message, general channel not subscribed. Retrying...')
yield* delay(500)
}

yield* put(messagesActions.sendMessage(payload))
yield* put(identityActions.updateIdentity({ ...identity, introMessageSent: true }))
}

0 comments on commit 6f6f8f9

Please sign in to comment.