Apply suggestions

This commit is contained in:
Jaiwanth 2021-06-29 10:57:02 +05:30
parent 025a7eb9a5
commit e404fa3cae
4 changed files with 161 additions and 165 deletions

View file

@ -212,10 +212,10 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
const sizePostFix = <span>{ _t("MB") }</span>; const sizePostFix = <span>{ _t("MB") }</span>;
let componentToDisplay: JSX.Element;
if (exportCancelled) { if (exportCancelled) {
// Display successful cancellation message // Display successful cancellation message
return <BaseDialog return (
<BaseDialog
title={_t("Export Cancelled")} title={_t("Export Cancelled")}
className="mx_ExportDialog" className="mx_ExportDialog"
contentId="mx_Dialog_content" contentId="mx_Dialog_content"
@ -225,14 +225,16 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
<p>{ _t("The export was cancelled successfully") }</p> <p>{ _t("The export was cancelled successfully") }</p>
<DialogButtons <DialogButtons
primaryButton={_t("Okay")} primaryButton={ _t("Okay") }
hasCancel={false} hasCancel={false}
onPrimaryButtonClick={onFinished} onPrimaryButtonClick={onFinished}
/> />
</BaseDialog> </BaseDialog>
);
} else if (exportSuccessful) { } else if (exportSuccessful) {
// Display successful export message // Display successful export message
return <BaseDialog return (
<BaseDialog
title={_t("Export Successful")} title={_t("Export Successful")}
className="mx_ExportDialog" className="mx_ExportDialog"
contentId="mx_Dialog_content" contentId="mx_Dialog_content"
@ -247,8 +249,10 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
onPrimaryButtonClick={onFinished} onPrimaryButtonClick={onFinished}
/> />
</BaseDialog> </BaseDialog>
);
} else if (!isExporting) { } else if (!isExporting) {
// Display export settings // Display export settings
return (
<BaseDialog <BaseDialog
title={_t("Export Chat")} title={_t("Export Chat")}
className="mx_ExportDialog" className="mx_ExportDialog"
@ -258,9 +262,7 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
fixedWidth={true} fixedWidth={true}
> >
<p> <p>
{_t( { _t("Select from the options below to export chats from your timeline") }
"Select from the options below to export chats from your timeline",
)}
</p> </p>
<span className="mx_ExportDialog_subheading">{ _t("Format") }</span> <span className="mx_ExportDialog_subheading">{ _t("Format") }</span>
@ -285,9 +287,7 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
</Field> </Field>
{ messageCount } { messageCount }
<span className="mx_ExportDialog_subheading"> <span className="mx_ExportDialog_subheading">{ _t("Size Limit") }</span>
{ _t("Size Limit") }
</span>
<Field <Field
type="number" type="number"
@ -315,8 +315,10 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
onCancel={() => onFinished(false)} onCancel={() => onFinished(false)}
/> />
</BaseDialog> </BaseDialog>
);
} else if (displayCancel) { } else if (displayCancel) {
// Display cancel warning // Display cancel warning
return (
<BaseDialog <BaseDialog
title={_t("Warning")} title={_t("Warning")}
className="mx_ExportDialog" className="mx_ExportDialog"
@ -338,9 +340,11 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
onPrimaryButtonClick={confirmCanel} onPrimaryButtonClick={confirmCanel}
/> />
</BaseDialog> </BaseDialog>
);
} else { } else {
// Display progress dialog // Display progress dialog
return <BaseDialog return (
<BaseDialog
title={_t("Exporting your data...")} title={_t("Exporting your data...")}
className="mx_ExportDialog" className="mx_ExportDialog"
contentId="mx_Dialog_content" contentId="mx_Dialog_content"
@ -354,9 +358,8 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
onPrimaryButtonClick={onCancel} onPrimaryButtonClick={onCancel}
/> />
</BaseDialog> </BaseDialog>
);
} }
return componentToDisplay;
}; };
export default ExportDialog; export default ExportDialog;

View file

@ -73,8 +73,7 @@ export default class ReplyThread extends React.Component {
this.unmounted = false; this.unmounted = false;
if (this.props.forExport) return; if (this.props.forExport) {
this.context.on("Event.replaced", this.onEventReplaced); this.context.on("Event.replaced", this.onEventReplaced);
this.room = this.context.getRoom(this.props.parentEv.getRoomId()); this.room = this.context.getRoom(this.props.parentEv.getRoomId());
this.room.on("Room.redaction", this.onRoomRedaction); this.room.on("Room.redaction", this.onRoomRedaction);
@ -84,6 +83,7 @@ export default class ReplyThread extends React.Component {
this.canCollapse = this.canCollapse.bind(this); this.canCollapse = this.canCollapse.bind(this);
this.collapse = this.collapse.bind(this); this.collapse = this.collapse.bind(this);
} }
}
static getParentEventId(ev) { static getParentEventId(ev) {
if (!ev || ev.isRedacted()) return; if (!ev || ev.isRedacted()) return;

View file

@ -2261,19 +2261,19 @@
"Enter a number between %(min)s and %(max)s": "Enter a number between %(min)s and %(max)s", "Enter a number between %(min)s and %(max)s": "Enter a number between %(min)s and %(max)s",
"Number of messages": "Number of messages", "Number of messages": "Number of messages",
"MB": "MB", "MB": "MB",
"Are you sure you want to stop exporting your data? If you do, you'll need to start over.": "Are you sure you want to stop exporting your data? If you do, you'll need to start over.", "Export Cancelled": "Export Cancelled",
"Abort export process": "Abort export process", "The export was cancelled successfully": "The export was cancelled successfully",
"Okay": "Okay",
"Export Successful": "Export Successful",
"Your messages were successfully exported": "Your messages were successfully exported",
"Export Chat": "Export Chat", "Export Chat": "Export Chat",
"Select from the options below to export chats from your timeline": "Select from the options below to export chats from your timeline", "Select from the options below to export chats from your timeline": "Select from the options below to export chats from your timeline",
"Format": "Format", "Format": "Format",
"Size Limit": "Size Limit", "Size Limit": "Size Limit",
"Include Attachments": "Include Attachments", "Include Attachments": "Include Attachments",
"Export": "Export", "Export": "Export",
"Export Successful": "Export Successful", "Are you sure you want to stop exporting your data? If you do, you'll need to start over.": "Are you sure you want to stop exporting your data? If you do, you'll need to start over.",
"Your messages were successfully exported": "Your messages were successfully exported", "Abort export process": "Abort export process",
"Okay": "Okay",
"Export Cancelled": "Export Cancelled",
"The export was cancelled successfully": "The export was cancelled successfully",
"Exporting your data...": "Exporting your data...", "Exporting your data...": "Exporting your data...",
"Feedback sent": "Feedback sent", "Feedback sent": "Feedback sent",
"Rate %(brand)s": "Rate %(brand)s", "Rate %(brand)s": "Rate %(brand)s",

View file

@ -10,35 +10,30 @@ import { EventType } from "matrix-js-sdk/src/@types/event";
export default class JSONExporter extends Exporter { export default class JSONExporter extends Exporter {
protected totalSize: number; protected totalSize: number;
protected messages: any[];
constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) { constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) {
super(room, exportType, exportOptions); super(room, exportType, exportOptions);
this.totalSize = 0; this.totalSize = 0;
this.messages = [];
} }
protected wrapJSON(json: string): string { protected createJSONString(): string {
const exportDate = formatFullDateNoDayNoTime(new Date()); const exportDate = formatFullDateNoDayNoTime(new Date());
const creator = this.room.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender(); const creator = this.room.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender();
const creatorName = this.room?.getMember(creator)?.rawDisplayName || creator; const creatorName = this.room?.getMember(creator)?.rawDisplayName || creator;
const topic = this.room.currentState.getStateEvents(EventType.RoomTopic, "")?.getContent()?.topic || ""; const topic = this.room.currentState.getStateEvents(EventType.RoomTopic, "")?.getContent()?.topic || "";
const exporter = this.client.getUserId(); const exporter = this.client.getUserId();
const exporterName = this.room?.getMember(exporter)?.rawDisplayName || exporter; const exporterName = this.room?.getMember(exporter)?.rawDisplayName || exporter;
return `{ const jsonObject = {
"room_name": "${this.room.name}", room_name: this.room.name,
"room_creator": "${creatorName}", room_creator: creatorName,
"topic": "${topic}", topic,
"export_date": "${exportDate}", export_date: exportDate,
"exported_by": "${exporterName}", exported_by: exporterName,
"messages": [ messages: this.messages,
${json}
]
}`
} }
return JSON.stringify(jsonObject, null, 2);
protected indentEachLine(JSONString: string, spaces: number) {
const indent = ' ';
const regex = /^(?!\s*$)/gm;
return JSONString.replace(regex, indent.repeat(spaces));
} }
protected async getJSONString(mxEv: MatrixEvent) { protected async getJSONString(mxEv: MatrixEvent) {
@ -57,18 +52,16 @@ ${json}
} }
const jsonEvent: any = mxEv.toJSON(); const jsonEvent: any = mxEv.toJSON();
const clearEvent = mxEv.isEncrypted() ? jsonEvent.decrypted : jsonEvent; const clearEvent = mxEv.isEncrypted() ? jsonEvent.decrypted : jsonEvent;
const jsonString = JSON.stringify(clearEvent, null, 2); return clearEvent;
return jsonString.length > 2 ? jsonString + ",\n" : "";
} }
protected async createOutput(events: MatrixEvent[]) { protected async createOutput(events: MatrixEvent[]) {
let content = "";
for (const event of events) { for (const event of events) {
if (this.cancelled) return this.cleanUp(); if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue; if (!haveTileForEvent(event)) continue;
content += await this.getJSONString(event); this.messages.push(await this.getJSONString(event));
} }
return this.wrapJSON(this.indentEachLine(content.slice(0, -2), 2)); return this.createJSONString();
} }
public async export() { public async export() {