|
1 | 1 | const { dcApiEndpoint } = require("../../../environment"); |
2 | | -const { getWorkFileSets } = require("../../opensearch"); |
| 2 | +const { |
| 3 | + buildAnnotationTarget, |
| 4 | + buildSearchAnnotationBody, |
| 5 | +} = require("./search-helpers"); |
3 | 6 |
|
4 | | -async function transform(response, options = {}) { |
5 | | - const body = JSON.parse(response.body); |
6 | | - const fileSet = body._source; |
7 | | - const annotations = fileSet?.annotations ?? []; |
8 | | - |
9 | | - const workId = fileSet.work_id; |
10 | | - const fileSetId = body._id; |
11 | | - const fileSetIndex = await getFileSetIndex(workId, fileSetId, options); |
12 | | - |
13 | | - const canvasId = `${dcApiEndpoint()}/works/${workId}?as=iiif/canvas/${fileSetIndex}`; |
14 | | - const annotationPageId = `${dcApiEndpoint()}/file-sets/${ |
15 | | - fileSet.id |
16 | | - }/annotations?as=iiif`; |
17 | | - |
18 | | - // Build annotation items - filter for transcriptions only |
19 | | - // We currently will only have one annotation and it's a transcription |
20 | | - const items = annotations |
21 | | - .filter((annotation) => annotation.type === "transcription") |
22 | | - .map((annotation, idx) => { |
23 | | - const annotationId = `${annotationPageId}/a${idx}`; |
24 | | - return { |
25 | | - id: annotationId, |
26 | | - type: "Annotation", |
27 | | - motivation: "commenting", |
28 | | - body: { |
29 | | - type: "TextualBody", |
30 | | - value: annotation.content, |
31 | | - format: "text/plain", |
32 | | - language: annotation.language || "en", |
33 | | - }, |
34 | | - target: canvasId, |
35 | | - }; |
36 | | - }); |
37 | | - |
38 | | - const annotationPage = { |
39 | | - "@context": "http://iiif.io/api/presentation/3/context.json", |
40 | | - id: annotationPageId, |
41 | | - type: "AnnotationPage", |
42 | | - items: items, |
43 | | - }; |
| 7 | +function transform(annotation, fileSet) { |
| 8 | + const canvasId = `${dcApiEndpoint()}/file-sets/${fileSet.id}?as=iiif`; |
| 9 | + const annotationId = `${dcApiEndpoint()}/annotations/${ |
| 10 | + annotation.id |
| 11 | + }?as=iiif`; |
44 | 12 |
|
45 | 13 | return { |
46 | 14 | statusCode: 200, |
47 | | - headers: { |
48 | | - "content-type": "application/json", |
49 | | - }, |
50 | | - body: JSON.stringify(annotationPage), |
| 15 | + headers: { "content-type": "application/json" }, |
| 16 | + body: JSON.stringify({ |
| 17 | + "@context": "http://iiif.io/api/presentation/3/context.json", |
| 18 | + id: annotationId, |
| 19 | + type: "Annotation", |
| 20 | + // We have hardcoded motivations here, but in the future we may want to make this more dynamic based on the annotation type |
| 21 | + motivation: ["contentState", "commenting"], |
| 22 | + body: buildSearchAnnotationBody(annotation), |
| 23 | + target: buildAnnotationTarget(canvasId, fileSet.work_id), |
| 24 | + }), |
51 | 25 | }; |
52 | 26 | } |
53 | 27 |
|
54 | | -async function getFileSetIndex(workId, fileSetId, options) { |
55 | | - const fileSetsResponse = await getWorkFileSets(workId, { |
56 | | - allowPrivate: options.allowPrivate, |
57 | | - allowUnpublished: options.allowUnpublished, |
58 | | - role: "Access", |
59 | | - sortBy: "rank", |
60 | | - }); |
61 | | - |
62 | | - const fileSetBody = JSON.parse(fileSetsResponse.body); |
63 | | - const hits = fileSetBody?.hits?.hits || []; |
64 | | - |
65 | | - const index = hits.findIndex((hit) => hit._source.id === fileSetId); |
66 | | - |
67 | | - return index; |
68 | | -} |
69 | 28 | module.exports = { transform }; |
0 commit comments