Skip to content

Commit

Permalink
Merge pull request #1564 from TryQuiet/fix/1523
Browse files Browse the repository at this point in the history
Deleting general channel does't work consistently
  • Loading branch information
Kacper-RF authored Jun 6, 2023
2 parents 462c3fb + a38f99e commit dfea2b4
Show file tree
Hide file tree
Showing 10 changed files with 79,281 additions and 79,175 deletions.
158,248 changes: 79,124 additions & 79,124 deletions packages/desktop/package-lock.json

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions packages/desktop/src/rtl-tests/channel.add.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ describe('Add new channel', () => {
const payload = input[1] as CreateChannelPayload
expect(payload.channel.owner).toEqual(alice.nickname)
expect(payload.channel.name).toEqual(channelName.output)
const channels = store.getState().PublicChannels.channels.entities
return socket.socketClient.emit<ChannelsReplicatedPayload>(SocketActionTypes.CHANNELS_REPLICATED, {
channels: {
...channels,
[payload.channel.name]: payload.channel
}
})
Expand Down Expand Up @@ -142,10 +144,6 @@ describe('Add new channel', () => {
})
)

await act(async () => {
await runSaga(testCreateChannelSaga).toPromise()
})

function* testCreateChannelSaga(): Generator {
const createChannelAction = yield* take(publicChannels.actions.createChannel)
expect(createChannelAction.payload.channel.name).toEqual(channelName.output)
Expand All @@ -154,6 +152,10 @@ describe('Add new channel', () => {
expect(addChannelAction.payload.channel).toEqual(createChannelAction.payload.channel)
}

await act(async () => {
await runSaga(testCreateChannelSaga).toPromise()
})

const createChannelModal = screen.queryByTestId('createChannelModal')
expect(createChannelModal).toBeNull()

Expand Down Expand Up @@ -291,7 +293,7 @@ describe('Add new channel', () => {
expect(isErrorStillExist).toBeNull()
})

it('Bug reproduction - create channel and open modal again without requierd filed error', async () => {
it('Bug reproduction - create channel and open modal again without requierd field error', async () => {
const channelName = 'las-venturas'

const { store, runSaga } = await prepareStore(
Expand All @@ -314,8 +316,10 @@ describe('Add new channel', () => {
// const payload = data[0]
expect(payload.channel.owner).toEqual(alice.nickname)
expect(payload.channel.name).toEqual(channelName)
const channels = store.getState().PublicChannels.channels.entities
return socket.socketClient.emit<ChannelsReplicatedPayload>(SocketActionTypes.CHANNELS_REPLICATED, {
channels: {
...channels,
[payload.channel.name]: payload.channel
}
})
Expand Down Expand Up @@ -408,10 +412,12 @@ describe('Add new channel', () => {
const data = input[1]
const payload = data
expect(payload.channel.owner).toEqual(alice.nickname)
const channels = store.getState().PublicChannels.channels.entities
// expect(payload.channel.name).toEqual(channelName.output)
return socket.socketClient.emit<ChannelsReplicatedPayload>(SocketActionTypes.CHANNELS_REPLICATED, {
channels: {
[payload.channel.name]: payload.channel
...channels,
[payload.channel.name]: payload.channel,
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { ChannelTile } from '../ChannelTile/ChannelTile.component'
import { Spinner } from '../Spinner/Spinner.component'
import { capitalizeFirstLetter } from '@quiet/common'

export const ChannelList: FC<ChannelListProps> = ({
community,
tiles,
communityContextMenu
}) => {
export const ChannelList: FC<ChannelListProps> = ({ community, tiles, communityContextMenu }) => {
let communityName = ''
if (community?.name) {
communityName = capitalizeFirstLetter(community.name)
Expand All @@ -28,7 +24,7 @@ export const ChannelList: FC<ChannelListProps> = ({
) : (
<FlatList
data={tiles}
keyExtractor={item => item.name}
keyExtractor={item => item.id}
renderItem={({ item }) => <ChannelTile {...item} />}
ItemSeparatorComponent={() => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('channelDeletionResponseSaga', () => {
.put(publicChannelsActions.clearMessagesCache({ channelId }))
.put(messagesActions.deleteChannelEntry({ channelId }))
.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.put(publicChannelsActions.completeChannelDeletion({}))
.put(messagesActions.sendDeletionMessage({ channelId }))
.run()
})
Expand All @@ -100,11 +101,31 @@ describe('channelDeletionResponseSaga', () => {
.put(publicChannelsActions.clearMessagesCache({ channelId }))
.put(messagesActions.deleteChannelEntry({ channelId }))
.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.provide([{ call: provideDelay }])
.put(publicChannelsActions.completeChannelDeletion({}))
.put(publicChannelsActions.createGeneralChannel())

.run()
})

test('delete channel which not exist in store', async () => {
const channelId = 'random channel'

const reducer = combineReducers(reducers)
await expectSaga(
channelDeletionResponseSaga,
publicChannelsActions.channelDeletionResponse({
channelId
})
)
.withReducer(reducer)
.withState(store.getState())
.not.put(publicChannelsActions.clearMessagesCache({ channelId }))
.not.put(messagesActions.deleteChannelEntry({ channelId }))
.not.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.not.put(publicChannelsActions.completeChannelDeletion({}))
.not.put(messagesActions.sendDeletionMessage({ channelId }))
.run()
})
})

describe('handle saga logic as standard user', () => {
Expand All @@ -125,11 +146,26 @@ describe('channelDeletionResponseSaga', () => {
.put(publicChannelsActions.clearMessagesCache({ channelId }))
.put(messagesActions.deleteChannelEntry({ channelId }))
.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.put(publicChannelsActions.completeChannelDeletion({}))
.run()
})

test('delete general channel', async () => {
test('delete general channel while user is on general channel', async () => {
store.dispatch(
publicChannelsActions.setCurrentChannel({
channelId: generalChannel.id
})
)
const channelId = generalChannel.id
const newGeneralId = 'newGeneralId'

const newGeneralChannel: PublicChannel = {
name: 'general',
description: 'general_description',
owner: 'general_owner',
timestamp: 0,
id: newGeneralId
}

const reducer = combineReducers(reducers)
await expectSaga(
Expand All @@ -145,20 +181,64 @@ describe('channelDeletionResponseSaga', () => {
.put(publicChannelsActions.clearMessagesCache({ channelId }))
.put(messagesActions.deleteChannelEntry({ channelId }))
.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.put(publicChannelsActions.completeChannelDeletion({}))
// .dispatch(publicChannelsActions.createGeneralChannel())
// .dispatch(
// publicChannelsActions.addChannel({
// channel: newGeneralChannel
// })
// )
.provide([
{ call: provideDelay },
[
select(publicChannelsSelectors.generalChannel),
{
name: 'general',
description: 'general_description',
owner: 'general_owner',
timestamp: 'general_timestamp',
id: channelId
}
]
[select(publicChannelsSelectors.generalChannel), generalChannel]
])
.put(publicChannelsActions.setCurrentChannel({ channelId: channelId }))
.put(publicChannelsActions.setCurrentChannel({ channelId }))
.run()
})

test('delete general channel while user in on other channel', async () => {
store.dispatch(
publicChannelsActions.setCurrentChannel({
channelId: photoChannel.id
})
)
const channelId = generalChannel.id

const reducer = combineReducers(reducers)
await expectSaga(
channelDeletionResponseSaga,
publicChannelsActions.channelDeletionResponse({
channelId
})
)
.withReducer(reducer)
.withState(store.getState())

.put(publicChannelsActions.startGeneralRecreation())
.put(publicChannelsActions.clearMessagesCache({ channelId }))
.put(messagesActions.deleteChannelEntry({ channelId }))
.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.put(publicChannelsActions.completeChannelDeletion({}))
.run()
})

test('delete channel which not exist in store', async () => {
const channelId = 'random channel'

const reducer = combineReducers(reducers)
await expectSaga(
channelDeletionResponseSaga,
publicChannelsActions.channelDeletionResponse({
channelId
})
)
.withReducer(reducer)
.withState(store.getState())
.not.put(publicChannelsActions.clearMessagesCache({ channelId }))
.not.put(messagesActions.deleteChannelEntry({ channelId }))
.not.put(publicChannelsActions.deleteChannelFromStore({ channelId }))
.not.put(publicChannelsActions.completeChannelDeletion({}))
.not.put(messagesActions.sendDeletionMessage({ channelId }))
.run()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { put, delay, select } from 'typed-redux-saga'
import { messagesActions } from '../../messages/messages.slice'
import { communitiesSelectors } from '../../communities/communities.selectors'
import { publicChannelsSelectors } from '../publicChannels.selectors'
import { PublicChannelStorage } from '@quiet/types'

const log = logger('publicChannels')

Expand All @@ -14,9 +15,19 @@ export function* channelDeletionResponseSaga(
log(`Deleted channel ${action.payload.channelId} saga`)

const { channelId } = action.payload

const generalChannel = yield* select(publicChannelsSelectors.generalChannel)
if (!generalChannel) return

const isChannelExist = yield* select(publicChannelsSelectors.getChannelById(channelId))
const currentChannelId = yield* select(publicChannelsSelectors.currentChannelId)
if (!isChannelExist) {
log(`Channel with id ${channelId} doesnt exist in store`)
return
}

if (!generalChannel) {
log('General Channel doesnt exist in store')
return
}

const isGeneral = channelId === generalChannel.id

Expand All @@ -30,23 +41,31 @@ export function* channelDeletionResponseSaga(

yield* put(publicChannelsActions.deleteChannelFromStore({ channelId }))

yield* put(publicChannelsActions.completeChannelDeletion({}))

const community = yield* select(communitiesSelectors.currentCommunity)

const isOwner = Boolean(community?.CA)

if (isOwner) {
if (isGeneral) {
yield* delay(1000)
yield* put(publicChannelsActions.createGeneralChannel())
} else {
yield* put(messagesActions.sendDeletionMessage({ channelId }))
}
} else {
if (isGeneral) {
yield* delay(3000)
const generalChannel = yield* select(publicChannelsSelectors.generalChannel)
if (!generalChannel) return
yield* put(publicChannelsActions.setCurrentChannel({ channelId: generalChannel.id }))
const isUserOnGeneral = currentChannelId === generalChannel.id

if (isGeneral && isUserOnGeneral) {
let newGeneralChannel: PublicChannelStorage | undefined = yield* select(
publicChannelsSelectors.generalChannel
)
while (!newGeneralChannel) {
log('General channel has not been replicated yet')
yield* delay(500)
newGeneralChannel = yield* select(publicChannelsSelectors.generalChannel)
}
yield* put(publicChannelsActions.setCurrentChannel({ channelId: newGeneralChannel.id }))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe('channelsReplicatedSaga', () => {
channelsReplicatedSaga,
publicChannelsActions.channelsReplicated({
channels: {
[sailingChannel.id]: sailingChannel
[sailingChannel.id]: sailingChannel,
[generalChannel.id]: generalChannel
}
})
)
Expand Down Expand Up @@ -128,7 +129,8 @@ describe('channelsReplicatedSaga', () => {
channelsReplicatedSaga,
publicChannelsActions.channelsReplicated({
channels: {
[sailingChannel.id]: sailingChannel
[sailingChannel.id]: sailingChannel,
[generalChannel.id]: generalChannel
}
})
)
Expand Down Expand Up @@ -253,12 +255,13 @@ describe('channelsReplicatedSaga', () => {
)
.withReducer(reducer)
.withState(store.getState())
.put(publicChannelsActions.deleteChannel({ channelId: photoChannel.id }))
.dispatch(publicChannelsActions.completeChannelDeletion({}))
.put(
publicChannelsActions.addChannel({
channel: sailingChannel
})
)
.put(publicChannelsActions.deleteChannel({ channelId: photoChannel.id }))
.run()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PayloadAction } from '@reduxjs/toolkit'
import { select, put } from 'typed-redux-saga'
import { select, put, take } from 'typed-redux-saga'
import { publicChannelsSelectors } from '../publicChannels.selectors'
import { publicChannelsActions } from '../publicChannels.slice'
import { messagesSelectors } from '../../messages/messages.selectors'
Expand All @@ -22,6 +22,18 @@ export function* channelsReplicatedSaga(

const databaseStoredChannelsIds = databaseStoredChannels.map(channel => channel.id)
console.log({ locallyStoredChannels, databaseStoredChannelsIds })

// Removing channels from store
if (databaseStoredChannelsIds.length > 0) {
for (const channelId of locallyStoredChannels) {
if (!databaseStoredChannelsIds.includes(channelId)) {
log(`REMOVING #${channelId} FROM STORE`)
yield* put(publicChannelsActions.deleteChannel({ channelId }))
yield* take(publicChannelsActions.completeChannelDeletion)
}
}
}

// Upserting channels to local storage
for (const channel of databaseStoredChannels) {
if (!locallyStoredChannels.includes(channel.id)) {
Expand All @@ -39,17 +51,6 @@ export function* channelsReplicatedSaga(
}
}

// Removing channels from store

if (databaseStoredChannelsIds.length > 0) {
for (const channelId of locallyStoredChannels) {
if (!databaseStoredChannelsIds.includes(channelId)) {
log(`REMOVING #${channelId} FROM STORE`)
yield* put(publicChannelsActions.deleteChannel({ channelId }))
}
}
}

const currentChannelCache = yield* select(publicChannelsSelectors.currentChannelMessages)
const currentChannelRepository = yield* select(
messagesSelectors.currentPublicChannelMessagesEntries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('deleteChannelSaga', () => {

const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket

beforeAll(async () => {
beforeEach(async () => {
setupCrypto()

store = prepareStore().store
Expand Down
Loading

0 comments on commit dfea2b4

Please sign in to comment.