-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manifest enrichment #699
Open
stephenwf
wants to merge
10
commits into
feature/IDA-893-enrichment-integration
Choose a base branch
from
feature/IDA-1055-enrich-manifest
base: feature/IDA-893-enrichment-integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Manifest enrichment #699
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6e4a76b
Hotfix for webhook url
stephenwf e6e36d9
Merge branch 'main' into feature/IDA-893-enrichment-integration
stephenwf 9b561da
IDA-1055 - Added enrichment of manifests with webhook
stephenwf c8ab705
fix for trailing slash on enrichment endpoint
stephenwf 392cf58
Added correct url
stephenwf 796359f
Made internal
stephenwf 8d0f061
Fixed expires
stephenwf 4fe5eca
Update trailing
stephenwf f730b29
Added more detail to webhook response
stephenwf d903acc
Pipeline warnings
stephenwf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
services/madoc-ts/src/routes/enrichment/manifest-enrichment-pipeline.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import invariant from 'tiny-invariant'; | ||
import { api } from '../../gateway/api.server'; | ||
import { RouteMiddleware } from '../../types/route-middleware'; | ||
import { parseUrn } from '../../utility/parse-urn'; | ||
import { userWithScope } from '../../utility/user-with-scope'; | ||
import { IncomingWebhook, WebhookEventType } from '../../webhooks/webhook-types'; | ||
|
||
export const manifestEnrichmentPipeline: RouteMiddleware<{ id: number }> = async context => { | ||
const { siteId } = userWithScope(context, ['site.admin']); | ||
const site = await context.siteManager.getSiteById(siteId); | ||
const siteApi = api.asUser({ siteId }); | ||
|
||
// 12-hour token. | ||
const webhook = await context.webhookExtension.generateWebhookUrl( | ||
site, | ||
manifestEnrichmentPipelineEvent.event_id, | ||
12 * 3600 | ||
); | ||
context.response.body = await siteApi.enrichment.enrichManifestInternal(context.params.id, webhook); | ||
}; | ||
|
||
export const manifestEnrichmentPipelineEvent: WebhookEventType = { | ||
event_id: 'manifest-enrichment-pipeline.complete', | ||
body_variables: ['id'], | ||
}; | ||
|
||
export const manifestEnrichmentHook: IncomingWebhook = { | ||
type: 'manifest-enrichment-pipeline-task-ingest', | ||
event_id: 'manifest-enrichment-pipeline.complete', | ||
is_outgoing: false, | ||
execute: async (resp, siteApi) => { | ||
invariant(resp.id, 'Expected response to contain `id`'); | ||
|
||
const task = await siteApi.enrichment.getEnrichmentTask(resp.id); | ||
invariant(task.subject, 'Missing subject on task'); | ||
invariant(task.status === 3, 'Task is not yet complete'); | ||
|
||
if (task.task_type === 'ocr_madoc_resource') { | ||
const parsed = parseUrn(task.subject); | ||
invariant(parsed, 'Invalid subject'); | ||
invariant(parsed.type === 'canvas', 'Can only process canvases'); | ||
|
||
if (task.state && task.state.ocr_resources && task.state.ocr_resources[0]) { | ||
const first = task.state.ocr_resources[0]; | ||
const enrichmentPlaintext = await siteApi.enrichment.getEnrichmentPlaintext(first); | ||
invariant(enrichmentPlaintext, 'Missing plaintext from enrichment'); | ||
if (enrichmentPlaintext.plaintext) { | ||
const canvasId = parsed.id; // ?? | ||
return await siteApi.updateCanvasPlaintext(canvasId, enrichmentPlaintext.plaintext); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main part (other parts mostly filling gaps in Madocs API).
|
53 changes: 53 additions & 0 deletions
53
services/madoc-ts/src/routes/iiif/linking/add-plaintext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { api } from '../../../gateway/api.server'; | ||
import { RouteMiddleware } from '../../../types/route-middleware'; | ||
import { userWithScope } from '../../../utility/user-with-scope'; | ||
import { linkHash } from './convert-linking'; | ||
|
||
export const addPlaintext: RouteMiddleware<{ id: number }, { plaintext: string }> = async context => { | ||
const { siteId } = userWithScope(context, ['site.admin']); | ||
const canvasId = Number(context.query.id); | ||
const plaintext = context.requestBody.plaintext; | ||
|
||
const siteApi = api.asUser({ siteId }); | ||
const linking = await siteApi.getCanvasLinking(canvasId); | ||
|
||
if (!plaintext.trim()) { | ||
context.response.status = 200; | ||
context.response.body = { success: true, empty: true }; | ||
return; | ||
} | ||
|
||
const matchingPlaintexts = linking.linking.filter(singleLink => { | ||
return singleLink.property === 'seeAlso' && singleLink.link.format === 'text/plain'; | ||
}); | ||
|
||
if (matchingPlaintexts.length) { | ||
for (const matchingPlaintext of matchingPlaintexts) { | ||
// Delete the existing one, and continue; | ||
await siteApi.deleteLinkingProperty(matchingPlaintext.id); | ||
} | ||
} | ||
|
||
// Create new plaintext and insert it. | ||
const bucket = 'plaintext'; | ||
const filePath = `public/${canvasId}/${linkHash(plaintext)}.txt`; | ||
|
||
await siteApi.saveStoragePlainText(bucket, filePath, plaintext, true); | ||
|
||
await siteApi.addLinkToResource({ | ||
label: 'Plaintext', | ||
link: { | ||
id: `/public/storage/urn:madoc:site:${siteId}/${bucket}/${filePath}`, | ||
format: 'text/plain', | ||
label: 'Plaintext', | ||
type: 'Text', | ||
file_path: `public/${canvasId}/${linkHash(plaintext)}.txt`, | ||
file_bucket: bucket, | ||
}, | ||
resource_id: canvasId as any, | ||
property: 'seeAlso', | ||
}); | ||
|
||
context.response.status = 200; | ||
context.response.body = { success: true, empty: false }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { manifestEnrichmentPipelineEvent } from '../routes/enrichment/manifest-enrichment-pipeline'; | ||
import { WebhookEventType } from './webhook-types'; | ||
|
||
const testEvent = { | ||
event_id: 'test-event', | ||
body_variables: ['hello'] as const, | ||
}; | ||
|
||
export const webhookEvents: WebhookEventType[] = [testEvent]; | ||
export const webhookEvents: WebhookEventType[] = [testEvent, manifestEnrichmentPipelineEvent]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the call to kick off the enrichment pipeline