Chat export parameter customisation (#7647)

* use export settings and hide fields

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix exporter tests

Signed-off-by: Kerry Archibald <kerrya@element.io>

* test ExportDialog with settings

Signed-off-by: Kerry Archibald <kerrya@element.io>

* tidy debugs, rename setting to Parameters

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use reasonable 100gb limit

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use normal setting instead of UIFeature

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use a customisation

Signed-off-by: Kerry Archibald <kerrya@element.io>

* move validateNumberInRange to utils

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use nullish coalesce

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use 8gb size limit for customisation

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update comments

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry 2022-01-31 12:54:14 +01:00 committed by GitHub
parent ad87ee0a0f
commit 085ecc7f5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 501 additions and 189 deletions

View file

@ -16,6 +16,7 @@ limitations under the License.
import React from 'react';
import { mount } from 'enzyme';
import { mocked } from 'ts-jest/utils';
import '../../../skinned-sdk';
import { act } from "react-dom/test-utils";
import { Room } from 'matrix-js-sdk';
@ -25,13 +26,27 @@ import { ExportType, ExportFormat } from '../../../../src/utils/exportUtils/expo
import { createTestClient, mkStubRoom } from '../../../test-utils';
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
import HTMLExporter from "../../../../src/utils/exportUtils/HtmlExport";
import ChatExport from '../../../../src/customisations/ChatExport';
import PlainTextExporter from '../../../../src/utils/exportUtils/PlainTextExport';
jest.useFakeTimers();
const mockHtmlExporter = ({
const htmlExporterInstance = ({
export: jest.fn().mockResolvedValue({}),
});
const plainTextExporterInstance = ({
export: jest.fn().mockResolvedValue({}),
});
jest.mock("../../../../src/utils/exportUtils/HtmlExport", () => jest.fn());
jest.mock("../../../../src/utils/exportUtils/PlainTextExport", () => jest.fn());
jest.mock('../../../../src/customisations/ChatExport', () => ({
getForceChatExportParameters: jest.fn().mockReturnValue({}),
}));
const ChatExportMock = mocked(ChatExport);
const HTMLExporterMock = mocked(HTMLExporter);
const PlainTextExporterMock = mocked(PlainTextExporter);
describe('<ExportDialog />', () => {
const mockClient = createTestClient();
@ -81,8 +96,13 @@ describe('<ExportDialog />', () => {
});
beforeEach(() => {
(HTMLExporter as jest.Mock).mockImplementation(jest.fn().mockReturnValue(mockHtmlExporter));
mockHtmlExporter.export.mockClear();
HTMLExporterMock.mockClear().mockImplementation(jest.fn().mockReturnValue(htmlExporterInstance));
PlainTextExporterMock.mockClear().mockImplementation(jest.fn().mockReturnValue(plainTextExporterInstance));
htmlExporterInstance.export.mockClear();
plainTextExporterInstance.export.mockClear();
// default setting value
ChatExportMock.getForceChatExportParameters.mockClear().mockReturnValue({});
});
it('renders export dialog', () => {
@ -104,7 +124,7 @@ describe('<ExportDialog />', () => {
await submitForm(component);
// 4th arg is an component function
const exportConstructorProps = (HTMLExporter as jest.Mock).mock.calls[0].slice(0, 3);
const exportConstructorProps = HTMLExporterMock.mock.calls[0].slice(0, 3);
expect(exportConstructorProps).toEqual([
defaultProps.room,
ExportType.Timeline,
@ -114,7 +134,32 @@ describe('<ExportDialog />', () => {
numberOfMessages: 100,
},
]);
expect(mockHtmlExporter.export).toHaveBeenCalled();
expect(htmlExporterInstance.export).toHaveBeenCalled();
});
it('exports room using values set from ForceRoomExportParameters', async () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
format: ExportFormat.PlainText,
range: ExportType.Beginning,
sizeMb: 7000,
numberOfMessages: 30,
includeAttachments: true,
});
const component = getComponent();
await submitForm(component);
// 4th arg is an component function
const exportConstructorProps = PlainTextExporterMock.mock.calls[0].slice(0, 3);
expect(exportConstructorProps).toEqual([
defaultProps.room,
ExportType.Beginning,
{
attachmentsIncluded: true,
maxSize: 7000 * 1024 * 1024,
numberOfMessages: 30,
},
]);
expect(plainTextExporterInstance.export).toHaveBeenCalled();
});
it('renders success screen when export is finished', async () => {
@ -139,6 +184,19 @@ describe('<ExportDialog />', () => {
expect(getExportFormatInput(component, ExportFormat.PlainText).props().checked).toBeTruthy();
expect(getExportFormatInput(component, ExportFormat.Html).props().checked).toBeFalsy();
});
it('hides export format input when format is valid in ForceRoomExportParameters', () => {
const component = getComponent();
expect(getExportFormatInput(component, ExportFormat.Html).props().checked).toBeTruthy();
});
it('does not render export format when set in ForceRoomExportParameters', () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
format: ExportFormat.PlainText,
});
const component = getComponent();
expect(getExportFormatInput(component, ExportFormat.Html).length).toBeFalsy();
});
});
describe('export type', () => {
@ -153,6 +211,14 @@ describe('<ExportDialog />', () => {
expect(getExportTypeInput(component).props().value).toEqual(ExportType.Beginning);
});
it('does not render export type when set in ForceRoomExportParameters', () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
range: ExportType.Beginning,
});
const component = getComponent();
expect(getExportTypeInput(component).length).toBeFalsy();
});
it('does not render message count input', async () => {
const component = getComponent();
expect(getMessageCountInput(component).length).toBeFalsy();
@ -177,7 +243,7 @@ describe('<ExportDialog />', () => {
await setMessageCount(component, 0);
await submitForm(component);
expect(mockHtmlExporter.export).not.toHaveBeenCalled();
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
});
it('does not export when export type is lastNMessages and message count is more than max', async () => {
@ -186,7 +252,7 @@ describe('<ExportDialog />', () => {
await setMessageCount(component, 99999999999);
await submitForm(component);
expect(mockHtmlExporter.export).not.toHaveBeenCalled();
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
});
it('exports when export type is NOT lastNMessages and message count is falsy', async () => {
@ -196,7 +262,7 @@ describe('<ExportDialog />', () => {
await selectExportType(component, ExportType.Timeline);
await submitForm(component);
expect(mockHtmlExporter.export).toHaveBeenCalled();
expect(htmlExporterInstance.export).toHaveBeenCalled();
});
});
@ -217,7 +283,7 @@ describe('<ExportDialog />', () => {
await setSizeLimit(component, 0);
await submitForm(component);
expect(mockHtmlExporter.export).not.toHaveBeenCalled();
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
});
it('does not export when size limit is larger than max', async () => {
@ -225,7 +291,7 @@ describe('<ExportDialog />', () => {
await setSizeLimit(component, 2001);
await submitForm(component);
expect(mockHtmlExporter.export).not.toHaveBeenCalled();
expect(htmlExporterInstance.export).not.toHaveBeenCalled();
});
it('exports when size limit is max', async () => {
@ -233,11 +299,32 @@ describe('<ExportDialog />', () => {
await setSizeLimit(component, 2000);
await submitForm(component);
expect(mockHtmlExporter.export).toHaveBeenCalled();
expect(htmlExporterInstance.export).toHaveBeenCalled();
});
it('does not render size limit input when set in ForceRoomExportParameters', () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
sizeMb: 10000,
});
const component = getComponent();
expect(getSizeInput(component).length).toBeFalsy();
});
/**
* 2000mb size limit does not apply when higher limit is configured in config
*/
it('exports when size limit set in ForceRoomExportParameters is larger than 2000', async () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
sizeMb: 10000,
});
const component = getComponent();
await submitForm(component);
expect(htmlExporterInstance.export).toHaveBeenCalled();
});
});
describe('include attachements', () => {
describe('include attachments', () => {
it('renders input with default value of false', () => {
const component = getComponent();
expect(getAttachmentsCheckbox(component).props().checked).toEqual(false);
@ -248,6 +335,14 @@ describe('<ExportDialog />', () => {
await setIncludeAttachments(component, true);
expect(getAttachmentsCheckbox(component).props().checked).toEqual(true);
});
it('does not render input when set in ForceRoomExportParameters', () => {
ChatExportMock.getForceChatExportParameters.mockReturnValue({
includeAttachments: false,
});
const component = getComponent();
expect(getAttachmentsCheckbox(component).length).toBeFalsy();
});
});
});

View file

@ -105,14 +105,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -235,7 +235,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -326,14 +326,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -456,7 +456,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -563,14 +563,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<div
className="mx_ExportDialog_options"
>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<StyledRadioGroup
definitions={
Array [
@ -790,12 +790,12 @@ Array [
</Field>
<StyledCheckbox
checked={false}
className=""
className="mx_ExportDialog_attachments-checkbox"
id="include-attachments"
onChange={[Function]}
>
<span
className="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
className="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
checked={false}
@ -962,14 +962,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -1092,7 +1092,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -1183,14 +1183,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -1313,7 +1313,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -1420,14 +1420,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<div
className="mx_ExportDialog_options"
>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<StyledRadioGroup
definitions={
Array [
@ -1647,12 +1647,12 @@ Array [
</Field>
<StyledCheckbox
checked={false}
className=""
className="mx_ExportDialog_attachments-checkbox"
id="include-attachments"
onChange={[Function]}
>
<span
className="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
className="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
checked={false}
@ -1806,14 +1806,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -1936,7 +1936,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -2027,14 +2027,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<div
class="mx_ExportDialog_options"
>
<span
class="mx_ExportDialog_subheading"
>
Format
</span>
<label
class="mx_StyledRadioButton mx_StyledRadioButton_enabled mx_StyledRadioButton_checked"
>
@ -2157,7 +2157,7 @@ Array [
</span>
</div>
<span
class="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
class="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
id="include-attachments"
@ -2264,14 +2264,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<div
className="mx_ExportDialog_options"
>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<StyledRadioGroup
definitions={
Array [
@ -2491,12 +2491,12 @@ Array [
</Field>
<StyledCheckbox
checked={false}
className=""
className="mx_ExportDialog_attachments-checkbox"
id="include-attachments"
onChange={[Function]}
>
<span
className="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
className="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
checked={false}
@ -2613,14 +2613,14 @@ Array [
<p>
Select from the options below to export chats from your timeline
</p>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<div
className="mx_ExportDialog_options"
>
<span
className="mx_ExportDialog_subheading"
>
Format
</span>
<StyledRadioGroup
definitions={
Array [
@ -2840,12 +2840,12 @@ Array [
</Field>
<StyledCheckbox
checked={false}
className=""
className="mx_ExportDialog_attachments-checkbox"
id="include-attachments"
onChange={[Function]}
>
<span
className="mx_Checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
className="mx_Checkbox mx_ExportDialog_attachments-checkbox mx_Checkbox_hasKind mx_Checkbox_kind_solid"
>
<input
checked={false}

View file

@ -50,24 +50,6 @@ describe('export', function() {
attachmentsIncluded: false,
};
const invalidExportOptions: IExportOptions[] = [
{
numberOfMessages: 10**9,
maxSize: 1024 * 1024 * 1024,
attachmentsIncluded: false,
},
{
numberOfMessages: -1,
maxSize: 4096 * 1024 * 1024,
attachmentsIncluded: false,
},
{
numberOfMessages: 0,
maxSize: 0,
attachmentsIncluded: false,
},
];
function createRoom() {
const room = new Room(generateRoomId(), null, client.getUserId());
return room;
@ -212,13 +194,28 @@ describe('export', function() {
).toBeTruthy();
});
it('checks if the export options are valid', function() {
for (const exportOption of invalidExportOptions) {
expect(
() =>
new PlainTextExporter(mockRoom, ExportType.Beginning, exportOption, null),
).toThrowError("Invalid export options");
}
const invalidExportOptions: [string, IExportOptions][] = [
['numberOfMessages exceeds max', {
numberOfMessages: 10 ** 9,
maxSize: 1024 * 1024 * 1024,
attachmentsIncluded: false,
}],
['maxSize exceeds 8GB', {
numberOfMessages: -1,
maxSize: 8001 * 1024 * 1024,
attachmentsIncluded: false,
}],
['maxSize is less than 1mb', {
numberOfMessages: 0,
maxSize: 0,
attachmentsIncluded: false,
}],
];
it.each(invalidExportOptions)('%s', (_d, options) => {
expect(
() =>
new PlainTextExporter(mockRoom, ExportType.Beginning, options, null),
).toThrowError("Invalid export options");
});
it('tests the file extension splitter', function() {

View file

@ -0,0 +1,26 @@
import { validateNumberInRange } from '../../../src/utils/validate';
describe('validateNumberInRange', () => {
const min = 1; const max = 10;
it('returns false when value is a not a number', () => {
expect(validateNumberInRange(min, max)('test' as unknown as number)).toEqual(false);
});
it('returns false when value is undefined', () => {
expect(validateNumberInRange(min, max)(undefined)).toEqual(false);
});
it('returns false when value is NaN', () => {
expect(validateNumberInRange(min, max)(NaN)).toEqual(false);
});
it('returns true when value is equal to min', () => {
expect(validateNumberInRange(min, max)(min)).toEqual(true);
});
it('returns true when value is equal to max', () => {
expect(validateNumberInRange(min, max)(max)).toEqual(true);
});
it('returns true when value is an int in range', () => {
expect(validateNumberInRange(min, max)(2)).toEqual(true);
});
it('returns true when value is a float in range', () => {
expect(validateNumberInRange(min, max)(2.2)).toEqual(true);
});
});