Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/remove "translation" code related to data migration and do a single release #1753

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 4 additions & 42 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,7 @@ export class StorageService extends EventEmitter {
// @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options'
await this.channels.load({ fetchEntryTimeout: 2000 })

const channels = Object.values(this.channels.all).map(channel => {
return this.transformChannel(channel)
})
const channels = Object.values(this.channels.all)

const keyValueChannels: {
[key: string]: PublicChannel
Expand All @@ -552,7 +550,6 @@ export class StorageService extends EventEmitter {
this.logger('ALL CHANNELS COUNT:', Object.keys(this.channels.all).length)
this.logger('ALL CHANNELS COUNT:', Object.keys(this.channels.all))
Object.values(this.channels.all).forEach(async (channel: PublicChannel) => {
channel = this.transformChannel(channel)
await this.subscribeToChannel(channel)
})
this.logger('STORAGE: Finished createDbForChannels')
Expand Down Expand Up @@ -629,7 +626,7 @@ export class StorageService extends EventEmitter {

db.events.on('replicate.progress', async (address, _hash, entry, progress, total) => {
this.logger(`progress ${progress as string}/${total as string}. Address: ${address as string}`)
const messages = this.transformMessages([entry.payload.value])
const messages = [entry.payload.value]

const verified = await this.verifyMessage(messages[0])

Expand Down Expand Up @@ -691,48 +688,14 @@ export class StorageService extends EventEmitter {
})
}

public transformMessages(msgs: ChannelMessage[]) {
console.log('---------------- TRANSFORMING MESSAGES ----------------------')
const messages = msgs.map(msg => {
console.log('processing message ', msg.id)
// @ts-ignore
if (msg.channelAddress) {
console.log('message before transformation ', msg)
// @ts-ignore
msg.channelId = msg.channelAddress
// @ts-ignore
delete msg.channelAddress
console.log('transformed message to new format ', msg)
return msg
}
return msg
})
return messages
}

public transformChannel(channel: PublicChannel) {
// @ts-ignore
if (channel.address) {
console.log('channel before transformation ', channel)
// @ts-ignore
channel.id = channel.address
// @ts-ignore
delete channel.address
console.log('transformed channel to new format ', channel)
return channel
}
return channel
}

public async askForMessages(channelId: string, ids: string[]) {
const repo = this.publicChannelsRepos.get(channelId)
if (!repo) return
const messages = this.getAllEventLogEntries<ChannelMessage>(repo.db)
let filteredMessages: ChannelMessage[] = []
const filteredMessages: ChannelMessage[] = []
for (const id of ids) {
filteredMessages.push(...messages.filter(i => i.id === id))
}
filteredMessages = this.transformMessages(filteredMessages)
this.emit(StorageEvents.LOAD_MESSAGES, {
messages: filteredMessages,
isVerified: true,
Expand All @@ -749,8 +712,7 @@ export class StorageService extends EventEmitter {
}
this.logger(`Creating channel ${data.id}`)

// @ts-ignore
const channelId = data.id || data.address
const channelId = data.id

const db: EventStore<ChannelMessage> = await this.orbitDb.log<ChannelMessage>(`channels.${channelId}`, {
replicate: options.replicate,
Expand Down
43 changes: 0 additions & 43 deletions packages/state-manager/src/sagas/messages/messages.transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { type PublicChannelsMessagesBase, type ChannelMessage } from '@quiet/types'
import { type Dictionary, type EntityState } from '@reduxjs/toolkit'
import { createTransform } from 'redux-persist'
import { StoreKeys } from '../store.keys'
import { messageSendingStatusAdapter } from './messages.adapter.ts'
Expand All @@ -10,52 +8,11 @@ export const MessagesTransform = createTransform(
return { ...inboundState }
},
(outboundState: MessagesState, _key: any) => {
outboundState.publicChannelsMessagesBase.entities = transformPublicChannelsMessagesBaseEntities(
outboundState.publicChannelsMessagesBase.entities
)

return {
...outboundState,
publicKeyMapping: {},
publicChannelsMessagesBase: outboundState.publicChannelsMessagesBase,
messageSendingStatus: messageSendingStatusAdapter.getInitialState(),
}
},
{ whitelist: [StoreKeys.Messages] }
)

const transformPublicChannelsMessagesBaseEntities = (messagesBaseEntities: Dictionary<PublicChannelsMessagesBase>) => {
const messagesRefactor = (messages: EntityState<ChannelMessage>) => {
const transformedMessagesEntities = messages.entities
for (const [key, _message] of Object.entries(transformedMessagesEntities)) {
const message = { ..._message } as any
if (message.channelAddress) {
const transformedMessage = {
...message,
channelId: message.channelAddress,
}
delete transformedMessage.channelAddress

transformedMessagesEntities[key] = transformedMessage
}
}

return transformedMessagesEntities
}

for (const [key, _message] of Object.entries(messagesBaseEntities)) {
const message = { ..._message } as any
if (message.channelAddress) {
const messages = { ...message.messages, entities: messagesRefactor(message.messages) }
const transformedMessage = {
...message,
messages,
channelId: message.channelAddress,
}
delete transformedMessage.channelAddress

messagesBaseEntities[key] = transformedMessage
}
}
return messagesBaseEntities
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
INITIAL_CURRENT_CHANNEL_ID,
type ChannelMessage,
type PublicChannelStatus,
type PublicChannelStorage,
} from '@quiet/types'
import { type Dictionary, type EntityState } from '@reduxjs/toolkit'
import { INITIAL_CURRENT_CHANNEL_ID } from '@quiet/types'
import { createTransform } from 'redux-persist'
import { StoreKeys } from '../store.keys'
import { publicChannelsAdapter, publicChannelsSubscriptionsAdapter } from './publicChannels.adapter'
Expand All @@ -15,23 +9,13 @@ export const PublicChannelsTransform = createTransform(
return { ...inboundState }
},
(outboundState: PublicChannelsState, _key: any) => {
console.log('OUTBOUND STATE PublicChannelsTransform')
const generalChannelId = getGeneralChannelId(outboundState)

const transformedOutboundState = { ...outboundState } as any
if (transformedOutboundState.currentChannelAddress) {
delete transformedOutboundState.currentChannelAddress
}

outboundState.channels.entities = transformChannelsEntities(outboundState.channels.entities)

outboundState.channelsStatus.entities = transformChannelStatusEntities(outboundState.channelsStatus.entities)

return {
...transformedOutboundState,
currentChannelId: generalChannelId,
channels: outboundState.channels,
channelsStatus: outboundState.channelsStatus,
channelsSubscriptions: publicChannelsSubscriptionsAdapter.getInitialState(),
}
},
Expand All @@ -47,67 +31,3 @@ const getGeneralChannelId = (state: PublicChannelsState) => {
console.log('PublicChannelsTransform: new general channel id', generalChannelId)
return generalChannelId
}

const transformChannelsEntities = (channelsEntities: Dictionary<PublicChannelStorage>) => {
const messagesRefactor = (messages: EntityState<ChannelMessage>) => {
const transformedMessagesEntities = messages.entities
console.log('PublicChannelsTransform: transformedMessagesEntities', transformedMessagesEntities)
for (const [key, _message] of Object.entries(transformedMessagesEntities)) {
const message = { ..._message } as any
if (message.channelAddress) {
const transformedMessage = {
...message,
channelId: message.channelAddress,
}
delete transformedMessage.channelAddress

transformedMessagesEntities[key] = transformedMessage
}
}

return transformedMessagesEntities
}

for (const [key, _channel] of Object.entries(channelsEntities)) {
const channel = { ..._channel } as any
if (channel.address) {
const messages = { ...channel.messages, entities: messagesRefactor(channel.messages) }
const transformedChannel = {
...channel,
messages,
id: channel.address,
}
delete transformedChannel.address

channelsEntities[key] = transformedChannel
}
}
return channelsEntities
}

const transformChannelStatusEntities = (channelsStatusEntities: Dictionary<PublicChannelStatus>) => {
const transformedChannelsStatusEntities = { ...channelsStatusEntities }
for (const [key, _channel] of Object.entries(transformedChannelsStatusEntities)) {
const channel = { ..._channel } as any
if (channel.address) {
let transformedNewestMessage = { ...channel.newestMessage }
if (transformedNewestMessage.channelAddress) {
transformedNewestMessage = {
channelId: channel.address,
...transformedNewestMessage,
}
delete transformedNewestMessage.channelAddress
}
const transformedChannel = {
...channel,
id: channel.address,
newestMessage: transformedNewestMessage,
}
delete transformedChannel.address

transformedChannelsStatusEntities[key] = transformedChannel
}
}

return transformedChannelsStatusEntities
}
Loading