Skip to content

Commit

Permalink
Annotations: Add a mapping's content identifier if possible (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed May 6, 2022
1 parent 854db8d commit 57a9616
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions services/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require("lodash")
const validate = require("jskos-validate")

const Annotation = require("../models/annotations")
const Mapping = require("../models/mappings")
const { EntityNotFoundError, DatabaseAccessError, InvalidBodyError, MalformedBodyError, MalformedRequestError, ForbiddenAccessError } = require("../errors")
const { bulkOperationForEntities } = require("../utils")

Expand Down Expand Up @@ -111,7 +112,7 @@ class MappingService {
let response

// Adjust all mappings
annotations = annotations.map(annotation => {
annotations = await Promise.all(annotations.map(async annotation => {
try {
// For type moderating, check if user is on the whitelist (except for admin=true).
if (!admin && annotation.motivation == "moderating") {
Expand Down Expand Up @@ -151,6 +152,20 @@ class MappingService {
if (config.env === "production") {
annotation.id = annotation.id.replace("http:", "https:")
}
// If it annotates a mapping from same instance, then add state to target
const target = _.get(annotation, "target.id", annotation.target)
if (target && target.startsWith && target.startsWith(config.baseUrl + "mappings/") && !_.get(annotation, "target.state.id")) {
const mapping = await Mapping.findOne({ uri: target })
const contentId = mapping && (mapping.identifier || []).find(id => id.startsWith("urn:jskos:mapping:content:"))
if (contentId) {
annotation.target = {
id: target,
state: {
id: contentId,
},
}
}
}

return annotation
} catch(error) {
Expand All @@ -159,7 +174,7 @@ class MappingService {
}
throw error
}
})
}))
annotations = annotations.filter(a => a)

if (bulk) {
Expand Down

0 comments on commit 57a9616

Please sign in to comment.