-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/desktop/src/utils/functions/__snapshots__/exportMessages.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
packages/desktop/src/utils/functions/exportMessages.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |