Commit e225bc09 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Make sure imported annotations use the new document metadata

parent 1d26c5c6
import type { Annotation, APIAnnotationData } from '../../types/api';
import { quote } from '../helpers/annotation-metadata';
import type { SidebarStore } from '../store';
import type { Frame } from '../store/modules/frames';
import type { AnnotationsService } from './annotations';
import type { ToastMessengerService } from './toast-messenger';
......@@ -35,15 +36,19 @@ type ImportData = Pick<
/**
* Return a copy of `ann` that contains only fields which can be preserved by
* an import performed on the client.
* an import performed on the client, overwriting some of them with the ones
* from current frame, if provided
*/
function getImportData(ann: APIAnnotationData, uri?: string): ImportData {
function getImportData(
ann: APIAnnotationData,
currentFrame: Frame | null,
): ImportData {
return {
target: ann.target,
tags: ann.tags,
text: ann.text,
uri: uri ?? ann.uri,
document: ann.document,
uri: currentFrame?.uri ?? ann.uri,
document: currentFrame?.metadata ?? ann.document,
extra: {
source: 'import',
original_id: ann.id,
......@@ -150,7 +155,7 @@ export class ImportAnnotationsService {
this._store.beginImport(anns.length);
const existingAnns = this._store.allAnnotations();
const currentUri = this._store.mainFrame()?.uri;
const currentFrame = this._store.mainFrame();
const importAnn = async (ann: APIAnnotationData): Promise<ImportResult> => {
const existingAnn = existingAnns.find(ex => duplicateMatch(ann, ex));
......@@ -161,7 +166,7 @@ export class ImportAnnotationsService {
try {
// Strip out all the fields that are ignored in an import, and overwrite
// the URI with current document's URI.
const importData = getImportData(ann, currentUri);
const importData = getImportData(ann, currentFrame);
// Fill out the annotation with default values for the current user and
// group.
......
......@@ -13,7 +13,7 @@ describe('ImportAnnotationsService', () => {
allAnnotations: sinon.stub().returns([]),
beginImport: sinon.stub(),
completeImport: sinon.stub(),
mainFrame: sinon.stub(),
mainFrame: sinon.stub().returns(null),
};
fakeToastMessenger = {
......@@ -59,13 +59,13 @@ describe('ImportAnnotationsService', () => {
}
/** Return the expected imported annotation for a given annotation. */
function importedAnnotation(ann) {
function importedAnnotation(ann, uri = undefined, document = undefined) {
return {
document: ann.document,
document: document ?? ann.document,
tags: ann.tags,
target: ann.target,
text: ann.text,
uri: ann.uri,
uri: uri ?? ann.uri,
extra: {
source: 'import',
original_id: ann.id,
......@@ -101,9 +101,13 @@ describe('ImportAnnotationsService', () => {
});
});
it('overwrites annotation URI with current document one', async () => {
it('sets annotation URI and document metadata to match current document', async () => {
const newUri = 'new_document_uri';
fakeStore.mainFrame.returns({ uri: newUri });
const newTitle = 'new_document_title';
fakeStore.mainFrame.returns({
uri: newUri,
metadata: { title: newTitle },
});
const svc = createService();
const ann = generateAnnotation();
......@@ -112,8 +116,7 @@ describe('ImportAnnotationsService', () => {
assert.calledWith(fakeAnnotationsService.save, {
$tag: 'dummy',
...importedAnnotation(ann),
uri: newUri,
...importedAnnotation(ann, newUri, { title: newTitle }),
});
});
......
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