Commit 9bebcd60 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Use display name if possible, when exporting annotations to text

parent 4e316d54
......@@ -139,7 +139,11 @@ function ExportAnnotations({
case 'txt': {
const exportData = annotationsExporter.buildTextExportContent(
annotationsToExport,
group?.name,
{
groupName: group?.name,
defaultAuthority,
displayNamesEnabled,
},
);
downloadTextFile(exportData, filename);
break;
......
......@@ -21,6 +21,10 @@ describe('sidebar/helpers/annotation-user', () => {
});
});
afterEach(() => {
$imports.$restore();
});
const fakeAnnotations = {
withDisplayName: {
user: 'acct:albert@victoriana.com',
......
import { trimAndDedent } from '../../shared/trim-and-dedent';
import type { APIAnnotationData } from '../../types/api';
import { username } from '../helpers/account-id';
import {
documentMetadata,
isReply,
quote,
} from '../helpers/annotation-metadata';
import { annotationDisplayName } from '../helpers/annotation-user';
import { stripInternalProperties } from '../helpers/strip-internal-properties';
import { VersionData } from '../helpers/version-data';
import type { SidebarStore } from '../store';
......@@ -17,6 +17,13 @@ export type JSONExportContent = {
annotations: APIAnnotationData[];
};
export type TextExportOptions = {
defaultAuthority?: string;
displayNamesEnabled?: boolean;
groupName?: string;
now?: Date;
};
/**
* Generates annotations exports
*
......@@ -49,21 +56,26 @@ export class AnnotationsExporter {
buildTextExportContent(
annotations: APIAnnotationData[],
{
groupName = '',
displayNamesEnabled = false,
defaultAuthority = '',
/* istanbul ignore next - test seam */
now = new Date(),
}: TextExportOptions = {},
): string {
const [firstAnnotation] = annotations;
if (!firstAnnotation) {
throw new Error('No annotations to export');
}
const extractUsername = (annotation: APIAnnotationData) =>
annotationDisplayName(annotation, defaultAuthority, displayNamesEnabled);
const { uri, title } = documentMetadata(firstAnnotation);
const uniqueUsers = [
...new Set(
annotations
.map(annotation => username(annotation.user))
.filter(Boolean),
annotations.map(anno => extractUsername(anno)).filter(Boolean),
),
];
......@@ -74,7 +86,7 @@ export class AnnotationsExporter {
Annotation ${index + 1}:
${annotation.created}
${annotation.text}
${username(annotation.user)}
${extractUsername(annotation)}
"${quote(annotation)}"
Tags: ${annotation.tags.join(', ')}`,
)
......
......@@ -44,6 +44,18 @@ describe('AnnotationsExporter', () => {
});
describe('buildTextExportContent', () => {
let baseAnnotation;
beforeEach(() => {
baseAnnotation = {
...newAnnotation(),
...publicAnnotation(),
created: now.toISOString(),
};
// Title should actually be an array
baseAnnotation.document.title = [baseAnnotation.document.title];
});
it('throws error when empty list of annotations is provided', () => {
assert.throws(
() => exporter.buildTextExportContent([]),
......@@ -52,34 +64,26 @@ describe('AnnotationsExporter', () => {
});
it('generates text content with provided annotations', () => {
const isoDate = now.toISOString();
const annotation = {
...newAnnotation(),
...publicAnnotation(),
created: isoDate,
};
// Title should actually be an array
annotation.document.title = [annotation.document.title];
const isoDate = baseAnnotation.created;
const annotations = [
annotation,
annotation,
baseAnnotation,
baseAnnotation,
{
...annotation,
...baseAnnotation,
user: 'acct:jane@localhost',
tags: ['foo', 'bar'],
},
{
...annotation,
...baseAnnotation,
...newReply(),
},
];
const groupName = 'My group';
const result = exporter.buildTextExportContent(
annotations,
const result = exporter.buildTextExportContent(annotations, {
groupName,
now,
);
});
assert.equal(
result,
......@@ -118,6 +122,41 @@ ${isoDate}
Annotation text
bill
"null"
Tags: tag_1, tag_2`,
);
});
it('uses display names if `displayNamesEnabled` is set', () => {
const annotation = {
...baseAnnotation,
user_info: {
display_name: 'John Doe',
},
};
const isoDate = annotation.created;
const groupName = 'My group';
const result = exporter.buildTextExportContent([annotation], {
displayNamesEnabled: true,
groupName,
now,
});
assert.equal(
result,
`${isoDate}
A special document
http://example.com
Group: ${groupName}
Total users: 1
Users: John Doe
Total annotations: 1
Total replies: 0
Annotation 1:
${isoDate}
Annotation text
John Doe
"null"
Tags: tag_1, tag_2`,
);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment