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({ ...@@ -139,7 +139,11 @@ function ExportAnnotations({
case 'txt': { case 'txt': {
const exportData = annotationsExporter.buildTextExportContent( const exportData = annotationsExporter.buildTextExportContent(
annotationsToExport, annotationsToExport,
group?.name, {
groupName: group?.name,
defaultAuthority,
displayNamesEnabled,
},
); );
downloadTextFile(exportData, filename); downloadTextFile(exportData, filename);
break; break;
......
...@@ -21,6 +21,10 @@ describe('sidebar/helpers/annotation-user', () => { ...@@ -21,6 +21,10 @@ describe('sidebar/helpers/annotation-user', () => {
}); });
}); });
afterEach(() => {
$imports.$restore();
});
const fakeAnnotations = { const fakeAnnotations = {
withDisplayName: { withDisplayName: {
user: 'acct:albert@victoriana.com', user: 'acct:albert@victoriana.com',
......
import { trimAndDedent } from '../../shared/trim-and-dedent'; import { trimAndDedent } from '../../shared/trim-and-dedent';
import type { APIAnnotationData } from '../../types/api'; import type { APIAnnotationData } from '../../types/api';
import { username } from '../helpers/account-id';
import { import {
documentMetadata, documentMetadata,
isReply, isReply,
quote, quote,
} from '../helpers/annotation-metadata'; } from '../helpers/annotation-metadata';
import { annotationDisplayName } from '../helpers/annotation-user';
import { stripInternalProperties } from '../helpers/strip-internal-properties'; import { stripInternalProperties } from '../helpers/strip-internal-properties';
import { VersionData } from '../helpers/version-data'; import { VersionData } from '../helpers/version-data';
import type { SidebarStore } from '../store'; import type { SidebarStore } from '../store';
...@@ -17,6 +17,13 @@ export type JSONExportContent = { ...@@ -17,6 +17,13 @@ export type JSONExportContent = {
annotations: APIAnnotationData[]; annotations: APIAnnotationData[];
}; };
export type TextExportOptions = {
defaultAuthority?: string;
displayNamesEnabled?: boolean;
groupName?: string;
now?: Date;
};
/** /**
* Generates annotations exports * Generates annotations exports
* *
...@@ -49,21 +56,26 @@ export class AnnotationsExporter { ...@@ -49,21 +56,26 @@ export class AnnotationsExporter {
buildTextExportContent( buildTextExportContent(
annotations: APIAnnotationData[], annotations: APIAnnotationData[],
groupName = '', {
/* istanbul ignore next - test seam */ groupName = '',
now = new Date(), displayNamesEnabled = false,
defaultAuthority = '',
/* istanbul ignore next - test seam */
now = new Date(),
}: TextExportOptions = {},
): string { ): string {
const [firstAnnotation] = annotations; const [firstAnnotation] = annotations;
if (!firstAnnotation) { if (!firstAnnotation) {
throw new Error('No annotations to export'); throw new Error('No annotations to export');
} }
const extractUsername = (annotation: APIAnnotationData) =>
annotationDisplayName(annotation, defaultAuthority, displayNamesEnabled);
const { uri, title } = documentMetadata(firstAnnotation); const { uri, title } = documentMetadata(firstAnnotation);
const uniqueUsers = [ const uniqueUsers = [
...new Set( ...new Set(
annotations annotations.map(anno => extractUsername(anno)).filter(Boolean),
.map(annotation => username(annotation.user))
.filter(Boolean),
), ),
]; ];
...@@ -74,7 +86,7 @@ export class AnnotationsExporter { ...@@ -74,7 +86,7 @@ export class AnnotationsExporter {
Annotation ${index + 1}: Annotation ${index + 1}:
${annotation.created} ${annotation.created}
${annotation.text} ${annotation.text}
${username(annotation.user)} ${extractUsername(annotation)}
"${quote(annotation)}" "${quote(annotation)}"
Tags: ${annotation.tags.join(', ')}`, Tags: ${annotation.tags.join(', ')}`,
) )
......
...@@ -44,6 +44,18 @@ describe('AnnotationsExporter', () => { ...@@ -44,6 +44,18 @@ describe('AnnotationsExporter', () => {
}); });
describe('buildTextExportContent', () => { 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', () => { it('throws error when empty list of annotations is provided', () => {
assert.throws( assert.throws(
() => exporter.buildTextExportContent([]), () => exporter.buildTextExportContent([]),
...@@ -52,34 +64,26 @@ describe('AnnotationsExporter', () => { ...@@ -52,34 +64,26 @@ describe('AnnotationsExporter', () => {
}); });
it('generates text content with provided annotations', () => { it('generates text content with provided annotations', () => {
const isoDate = now.toISOString(); const isoDate = baseAnnotation.created;
const annotation = {
...newAnnotation(),
...publicAnnotation(),
created: isoDate,
};
// Title should actually be an array
annotation.document.title = [annotation.document.title];
const annotations = [ const annotations = [
annotation, baseAnnotation,
annotation, baseAnnotation,
{ {
...annotation, ...baseAnnotation,
user: 'acct:jane@localhost', user: 'acct:jane@localhost',
tags: ['foo', 'bar'], tags: ['foo', 'bar'],
}, },
{ {
...annotation, ...baseAnnotation,
...newReply(), ...newReply(),
}, },
]; ];
const groupName = 'My group'; const groupName = 'My group';
const result = exporter.buildTextExportContent( const result = exporter.buildTextExportContent(annotations, {
annotations,
groupName, groupName,
now, now,
); });
assert.equal( assert.equal(
result, result,
...@@ -118,6 +122,41 @@ ${isoDate} ...@@ -118,6 +122,41 @@ ${isoDate}
Annotation text Annotation text
bill bill
"null" "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`, 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