Skip to content

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed Nov 17, 2023
1 parent dce52e7 commit a52ccad
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 127 deletions.
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './naming'
export * from './fileData'
export * from './libp2p'
export * from './tests'
export * from './messages'
50 changes: 50 additions & 0 deletions packages/common/src/messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { PublicChannelStorage } from '@quiet/types'
import { generateChannelId } from './channelAddress'
import { userCreatedChannelMessage, userJoinedMessage, verifyUserInfoMessage } from './messages'

describe('messages helper', () => {
const username = 'johnny'

const generalChannel: PublicChannelStorage = {
name: 'general',
description: 'Welcome to #general',
timestamp: 1,
owner: username,
id: generateChannelId('general'),
messages: { ids: [], entities: {} },
}

const sportChannel: PublicChannelStorage = {
name: 'sport',
description: 'Welcome to #sport',
timestamp: 1,
owner: username,
id: generateChannelId('sport'),
messages: { ids: [], entities: {} },
}
it('userCreatedChannelMessage', () => {
const expectedMessage = '@johnny created #sport'
const message = userCreatedChannelMessage(username, sportChannel.name)
expect(message).toEqual(expectedMessage)
})

it('userJoinedMessage', () => {
const expectedMessage =
'**@johnny** has joined and will be registered soon. 🎉 [Learn more](https://github.com/TryQuiet/quiet/wiki/Quiet-FAQ#how-does-username-registration-work)'
const message = userJoinedMessage(username)
expect(message).toEqual(expectedMessage)
})

it('verifyUserInfoMessage - general channel', () => {
const expectedMessage =
'**@johnny** has joined and will be registered soon. 🎉 [Learn more](https://github.com/TryQuiet/quiet/wiki/Quiet-FAQ#how-does-username-registration-work)'
const message = verifyUserInfoMessage(username, generalChannel)
expect(message).toEqual(expectedMessage)
})

it('verifyUserInfoMessage - other channel', () => {
const expectedMessage = '@johnny created #sport'
const message = verifyUserInfoMessage(username, sportChannel)
expect(message).toEqual(expectedMessage)
})
})
15 changes: 15 additions & 0 deletions packages/common/src/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PublicChannelStorage } from '@quiet/types'

export const userCreatedChannelMessage = (username: string, channelName: string) =>
`@${username} created #${channelName}`

export const userJoinedMessage = (username: string) =>
`**@${username}** has joined and will be registered soon. 🎉 [Learn more](https://github.com/TryQuiet/quiet/wiki/Quiet-FAQ#how-does-username-registration-work)`

export const verifyUserInfoMessage = (username: string, channel: PublicChannelStorage) => {
if (channel.name === 'general') {
return userJoinedMessage(username)
} else {
return userCreatedChannelMessage(username, channel.name)
}
}
6 changes: 3 additions & 3 deletions packages/e2e-tests/src/tests/invitationLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Sidebar,
WarningModal,
} from '../selectors'
import { capitalizeFirstLetter, composeInvitationDeepUrl, parseInvitationCode } from '@quiet/common'
import { capitalizeFirstLetter, composeInvitationDeepUrl, parseInvitationCode, userJoinedMessage } from '@quiet/common'
import { execSync } from 'child_process'
import { type SupportedPlatformDesktop } from '@quiet/types'

Expand Down Expand Up @@ -182,12 +182,12 @@ describe('New user joins using invitation link while having app opened', () => {

it('Owner sees that guest joined community', async () => {
console.log('Invitation Link', 21)
const generalChannel = new Channel(guestApp.driver, 'general')
const generalChannel = new Channel(ownerApp.driver, 'general')
await generalChannel.element.isDisplayed()

const hasMessage = await generalChannel.waitForUserMessage(
joiningUserUsername,
`@${joiningUserUsername} has joined and will be registered soon. 🎉 Learn more`
userJoinedMessage(joiningUserUsername)
)
const isMessageDisplayed = await hasMessage?.isDisplayed()
expect(isMessageDisplayed).toBeTruthy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export function* saveCommunityMetadataSaga(
)

const community = yield* select(communitiesSelectors.currentCommunity)
if (!community) return
const isOwner = community.CA

if (!isOwner) {
yield* put(publicChannelsActions.sendUnregisteredInfoMessage())
}
if (community?.CA) return
yield* put(publicChannelsActions.sendUnregisteredInfoMessage())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { keyFromCertificate, parseCertificate, pubKeyFromCsr, setupCrypto } from
import { type Store } from '../store.types'
import { getFactory, publicChannels } from '../..'
import { prepareStore } from '../../utils/tests/prepareStore'
import { getMessagesFromChannelIdByPubKey, validCurrentPublicChannelMessagesEntries } from './messages.selectors'
import { validCurrentPublicChannelMessagesEntries } from './messages.selectors'
import { type communitiesActions } from '../communities/communities.slice'
import { type identityActions } from '../identity/identity.slice'
import { type FactoryGirl } from 'factory-girl'
Expand Down Expand Up @@ -117,56 +117,4 @@ describe('messagesSelectors', () => {

expect(messages[0].id).toBe(authenticMessage.id)
})

describe('getMessagesFromChannelIdByPubKey - return messages related to pubkey from the specific channel in chronological order', () => {
const generateMessage = async (identity: Identity, channelId: string) => {
await factory.create<ReturnType<typeof publicChannels.actions.test_message>['payload']>('Message', {
identity,
message: {
...(
await factory.build<typeof publicChannels.actions.test_message>('Message', {
identity,
})
).payload.message,
id: Math.random().toString(36).substr(2.9),
channelId,
createdAt: getCurrentTime(),
},
verifyAutomatically: true,
})
}
it('return only Alice messages from general channel - included John messages', async () => {
const aliceCsr = alice.userCsr?.userCsr
if (!aliceCsr) return

const arrLength = 3

for await (const _i of new Array(arrLength)) {
await new Promise<void>(resolve => setTimeout(() => resolve(), 500))
await generateMessage(alice, generalChannel.id)
await generateMessage(john, generalChannel.id)
}

const messages = getMessagesFromChannelIdByPubKey(generalChannel.id, pubKeyFromCsr(aliceCsr))(store.getState())
expect(messages.length).toEqual(arrLength)
expect(messages[0].createdAt).toBeLessThan(messages[2].createdAt)
})

it('return only Alice messages from general channel - included Alice messages on other channel', async () => {
const aliceCsr = alice.userCsr?.userCsr
if (!aliceCsr) return

const arrLength = 3

for await (const _i of new Array(arrLength)) {
await new Promise<void>(resolve => setTimeout(() => resolve(), 500))
await generateMessage(alice, generalChannel.id)
await generateMessage(alice, devChannel.id)
}

const messages = getMessagesFromChannelIdByPubKey(generalChannel.id, pubKeyFromCsr(aliceCsr))(store.getState())
expect(messages.length).toEqual(arrLength)
expect(messages[0].createdAt).toBeLessThan(messages[2].createdAt)
})
})
})
17 changes: 0 additions & 17 deletions packages/state-manager/src/sagas/messages/messages.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ export const publicChannelMessagesEntities = (address: string) =>
return channelMessagesAdapter.getSelectors().selectEntities(channelMessagesBase.messages)
})

export const getMessagesFromChannelIdByPubKey = (channelId: string, pubkey: string) =>
createSelector(publicChannelsMessagesBase, base => {
if (!base) return []

const generalMessagesBase = base[channelId]
if (!generalMessagesBase) return []

const channelMessages = channelMessagesAdapter.getSelectors().selectAll(generalMessagesBase.messages)

const messagesFromPubKey = channelMessages.filter(mess => mess.pubKey === pubkey)

const sortedMessages = messagesFromPubKey.sort((a, b) => a.createdAt - b.createdAt)

return sortedMessages
})

export const messageSendingStatusById = (messageId: string) =>
createSelector(messagesSlice, reducerState => {
return messageSendingStatusAdapter.getSelectors().selectById(reducerState.messageSendingStatus, messageId)
Expand Down Expand Up @@ -140,5 +124,4 @@ export const messagesSelectors = {
messagesVerificationStatus,
messagesSendingStatus,
messageSendingStatusById,
getMessagesFromChannelIdByPubKey,
}
Loading

0 comments on commit a52ccad

Please sign in to comment.