Skip to content

Commit

Permalink
feat: export chats per channel
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b authored Nov 27, 2023
1 parent 35ac4d5 commit c3faf4f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Send an info message immediately after a user joins the community

* Feature: add functionality to export chat to text document in desktop version

[2.0.3-alpha.6]

* Fix: filter out invalid peer addresses in peer list. Update peer list in localdb.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { ContextMenuItemProps } from '../ContextMenu.types'

import { useModal } from '../../../containers/hooks'
import { ModalName } from '../../../sagas/modals/modals.types'
import { exportChats } from '../../../../utils/functions/exportMessages'

export const ChannelContextMenu: FC = () => {
const community = useSelector(communities.selectors.currentCommunity)
const channel = useSelector(publicChannels.selectors.currentChannel)
const channelMessages = useSelector(publicChannels.selectors.currentChannelMessagesMergedBySender)

let title = ''
if (channel) {
Expand All @@ -36,6 +38,10 @@ export const ChannelContextMenu: FC = () => {
deleteChannelModal.handleOpen()
},
},
{
title: 'Export messages',
action: () => channel && exportChats(channel?.name, channelMessages),
},
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export const BasicMessageComponent: React.FC<BasicMessageProps & FileActionsProp
const userLabel = messageDisplayData?.isDuplicated
? UserLabelType.DUPLICATE
: !messageDisplayData?.isRegistered
? UserLabelType.UNREGISTERED
: null
? UserLabelType.UNREGISTERED
: null

console.log('Unregistered Debug - Basic Message', { userLabel })

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`channelMessagesToText should convert channel messages to text format:
"[JohnDoe Nov 22, 12:18 PM]
Hello, World!
[JaneDoe Nov 22, 12:18 PM]
How are you?
[Alice Nov 22, 1:14 PM]
Hi there!
1`] = `
"[JohnDoe Nov 22, 12:18 PM]
Hello, World!
[JaneDoe Nov 22, 12:18 PM]
How are you?
[Alice Nov 22, 1:14 PM]
Hi there!
"
`;
58 changes: 58 additions & 0 deletions packages/desktop/src/utils/functions/exportMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { channelMessagesToText } from './exportMessages'
import { MessagesDailyGroups } from '@quiet/types'

const messages: MessagesDailyGroups = {
'2023-11-22': [
[
{
id: '1',
type: 1,
message: 'Hello, World!',
createdAt: 1637547600,
date: 'Nov 22, 12:18 PM',
nickname: 'JohnDoe',
isRegistered: true,
isDuplicated: false,
pubKey: 'public_key',
},
{
id: '2',
type: 1,
message: 'How are you?',
createdAt: 1637551200,
date: 'Nov 22, 12:18 PM',
nickname: 'JaneDoe',
isRegistered: true,
isDuplicated: false,
pubKey: 'public_key_2',
},
{
id: '3',
type: 1,
message: 'Hi there!',
createdAt: 1637554800,
date: 'Nov 22, 1:14 PM',
nickname: 'Alice',
isRegistered: true,
isDuplicated: false,
pubKey: 'public_key_3',
},
],
],
}

describe('channelMessagesToText', () => {
it('should convert channel messages to text format', () => {
const result = channelMessagesToText(messages)
expect(result).toMatchSnapshot(`
"[JohnDoe Nov 22, 12:18 PM]
Hello, World!
[JaneDoe Nov 22, 12:18 PM]
How are you?
[Alice Nov 22, 1:14 PM]
Hi there!
`)
})
})
39 changes: 39 additions & 0 deletions packages/desktop/src/utils/functions/exportMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MessagesDailyGroups } from '@quiet/types'
import { dialog } from '@electron/remote'
import fs from 'fs'

export const exportChats = async (channelName: string, channelMessages: MessagesDailyGroups) => {
dialog
.showSaveDialog({
title: 'Save file',
defaultPath: `${channelName}.txt`,
buttonLabel: 'Save',

filters: [
{ name: 'txt', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] },
],
})
.then(({ filePath }) => {
if (filePath) {
fs.writeFile(filePath, channelMessagesToText(channelMessages), err => {
if (err) {
console.log(err)
}
})
}
})
}

// This function is exported just to test it
export const channelMessagesToText = (channelMessages: MessagesDailyGroups) => {
return Object.keys(channelMessages)
.map(day =>
channelMessages[day]
.map(messages =>
messages.map(message => `[${message.nickname} ${message.date}]\n${message.message}\n\n`).join('')
)
.join('')
)
.join('\n')
}

0 comments on commit c3faf4f

Please sign in to comment.