Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions controllers/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { configureRerumOptions } from '../versioning.js'
import { isDeleted } from '../predicates.js'
import config from '../config/index.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'

Expand Down Expand Up @@ -72,7 +73,7 @@ const bulkCreate = async function (req, res, next) {
if(Object.keys(d).length === 0) continue
const providedID = d?._id
const id = isValidID(providedID) ? providedID : ObjectID()
d = utils.configureRerumOptions(generatorAgent, d)
d = configureRerumOptions(generatorAgent, d)
// id is also protected in this case, so it can't be set.
if(_contextid(d["@context"])) delete d.id
d._id = id
Expand Down Expand Up @@ -159,10 +160,10 @@ const bulkUpdate = async function (req, res, next) {
return
}
if (null === originalObject) continue
if (utils.isDeleted(originalObject)) continue
if (isDeleted(originalObject)) continue
id = ObjectID()
let context = objectReceived["@context"] ? { "@context": objectReceived["@context"] } : {}
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
let rerumProp = { "__rerum": configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
delete objectReceived["__rerum"]
delete objectReceived["_id"]
delete objectReceived["@id"]
Expand Down
14 changes: 8 additions & 6 deletions controllers/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @author Claude Sonnet 4, cubap, thehabes
*/
import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
// helpers used by many controllers have been split into focused modules
import { configureWebAnnoHeadersFor, configureLDHeadersFor, configureLastModifiedHeader } from '../headers.js'
import { configureRerumOptions } from '../versioning.js'
import config from '../config/index.js'
import { _contextid, idNegotiation, generateSlugId, ObjectID, createExpressError, getAgentClaim, parseDocumentID } from './utils.js'

Expand All @@ -31,7 +33,7 @@ const create = async function (req, res, next) {
let generatorAgent = getAgentClaim(req, next)
let context = req.body["@context"] ? { "@context": req.body["@context"] } : {}
let provided = JSON.parse(JSON.stringify(req.body))
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, provided, false, false)["__rerum"] }
let rerumProp = { "__rerum": configureRerumOptions(generatorAgent, provided, false, false)["__rerum"] }
if(slug){
rerumProp.__rerum.slug = slug
}
Expand All @@ -47,7 +49,7 @@ const create = async function (req, res, next) {
console.log("CREATE")
try {
let result = await db.insertOne(newObject)
res.set(utils.configureWebAnnoHeadersFor(newObject))
res.set(configureWebAnnoHeadersFor(newObject))
newObject = idNegotiation(newObject)
newObject.new_obj_state = JSON.parse(JSON.stringify(newObject))
res.location(newObject[_contextid(newObject["@context"]) ? "id":"@id"])
Expand Down Expand Up @@ -82,7 +84,7 @@ const query = async function (req, res, next) {
try {
let matches = await db.find(props).limit(limit).skip(skip).toArray()
matches = matches.map(o => idNegotiation(o))
res.set(utils.configureLDHeadersFor(matches))
res.set(configureLDHeadersFor(matches))
res.json(matches)
} catch (error) {
next(createExpressError(error))
Expand All @@ -100,11 +102,11 @@ const id = async function (req, res, next) {
try {
let match = await db.findOne({"$or": [{"_id": id}, {"__rerum.slug": id}]})
if (match) {
res.set(utils.configureWebAnnoHeadersFor(match))
res.set(configureWebAnnoHeadersFor(match))
//Support built in browser caching
res.set("Cache-Control", "max-age=86400, must-revalidate")
//Support requests with 'If-Modified_Since' headers
res.set(utils.configureLastModifiedHeader(match))
res.set(configureLastModifiedHeader(match))
// Include current version for optimistic locking
const currentVersion = match.__rerum?.isOverwritten ?? ""
res.set('Current-Overwritten-Version', currentVersion)
Expand Down
8 changes: 4 additions & 4 deletions controllers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Claude Sonnet 4, cubap, thehabes
*/
import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { isDeleted, isReleased, isGenerator } from '../predicates.js'
import config from '../config/index.js'
import { createExpressError, getAgentClaim, parseDocumentID, getAllVersions, getAllDescendants } from './utils.js'

Expand Down Expand Up @@ -40,19 +40,19 @@ const deleteObj = async function(req, res, next) {
}
if (null !== originalObject) {
let safe_original = JSON.parse(JSON.stringify(originalObject))
if (utils.isDeleted(safe_original)) {
if (isDeleted(safe_original)) {
err = Object.assign(err, {
message: `The object you are trying to delete is already deleted. ${err.message}`,
status: 403
})
}
else if (utils.isReleased(safe_original)) {
else if (isReleased(safe_original)) {
err = Object.assign(err, {
message: `The object you are trying to delete is released. Fork to make changes. ${err.message}`,
status: 403
})
}
else if (!utils.isGenerator(safe_original, agentRequestingDelete)) {
else if (!isGenerator(safe_original, agentRequestingDelete)) {
err = Object.assign(err, {
message: `You are not the generating agent for this object and so are not authorized to delete it. ${err.message}`,
status: 401
Expand Down
6 changes: 3 additions & 3 deletions controllers/gog.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { configureLDHeadersFor } from '../headers.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'

/**
Expand Down Expand Up @@ -133,7 +133,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
// console.log(witnessFragments.length+" fragments found for this Manuscript")
// const end = Date.now()
// console.log(`Total Execution time: ${end - start} ms`)
res.set(utils.configureLDHeadersFor(witnessFragments))
res.set(configureLDHeadersFor(witnessFragments))
res.json(witnessFragments)
}
catch (error) {
Expand Down Expand Up @@ -295,7 +295,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) {
// console.log(glosses.length+" Glosses found for this Manuscript")
// const end = Date.now()
// console.log(`Total Execution time: ${end - start} ms`)
res.set(utils.configureLDHeadersFor(glosses))
res.set(configureLDHeadersFor(glosses))
res.json(glosses)
}
catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions controllers/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { configureLDHeadersFor } from '../headers.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation, getAllVersions, getAllAncestors, getAllDescendants } from './utils.js'

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ const since = async function (req, res, next) {
let descendants = getAllDescendants(all, obj, [])
descendants =
descendants.map(o => idNegotiation(o))
res.set(utils.configureLDHeadersFor(descendants))
res.set(configureLDHeadersFor(descendants))
res.json(descendants)
}

Expand Down Expand Up @@ -79,7 +79,7 @@ const history = async function (req, res, next) {
let ancestors = getAllAncestors(all, obj, [])
ancestors =
ancestors.map(o => idNegotiation(o))
res.set(utils.configureLDHeadersFor(ancestors))
res.set(configureLDHeadersFor(ancestors))
res.json(ancestors)
}

Expand Down
11 changes: 6 additions & 5 deletions controllers/overwrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { isDeleted, isReleased, isGenerator } from '../predicates.js'
import { configureWebAnnoHeadersFor } from '../headers.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'

/**
Expand Down Expand Up @@ -38,19 +39,19 @@ const overwrite = async function (req, res, next) {
status: 404
})
}
else if (utils.isDeleted(originalObject)) {
else if (isDeleted(originalObject)) {
err = Object.assign(err, {
message: `The object you are trying to overwrite is deleted. ${err.message}`,
status: 403
})
}
else if (utils.isReleased(originalObject)) {
else if (isReleased(originalObject)) {
err = Object.assign(err, {
message: `The object you are trying to overwrite is released. Fork with /update to make changes. ${err.message}`,
status: 403
})
}
else if (!utils.isGenerator(originalObject, agentRequestingOverwrite)) {
else if (!isGenerator(originalObject, agentRequestingOverwrite)) {
err = Object.assign(err, {
message: `You are not the generating agent for this object. You cannot overwrite it. Fork with /update to make changes. ${err.message}`,
status: 401
Expand Down Expand Up @@ -93,7 +94,7 @@ const overwrite = async function (req, res, next) {
}
// Include current version in response headers for future optimistic locking
res.set('Current-Overwritten-Version', rerumProp["__rerum"].isOverwritten)
res.set(utils.configureWebAnnoHeadersFor(newObject))
res.set(configureWebAnnoHeadersFor(newObject))
newObject = idNegotiation(newObject)
newObject.new_obj_state = JSON.parse(JSON.stringify(newObject))
res.location(newObject[_contextid(newObject["@context"]) ? "id":"@id"])
Expand Down
12 changes: 7 additions & 5 deletions controllers/patchSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { isDeleted } from '../predicates.js'
import { configureRerumOptions } from '../versioning.js'
import { configureWebAnnoHeadersFor } from '../headers.js'
import config from '../config/index.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation, alterHistoryNext } from './utils.js'

Expand Down Expand Up @@ -44,7 +46,7 @@ const patchSet = async function (req, res, next) {
status: 501
})
}
else if (utils.isDeleted(originalObject)) {
else if (isDeleted(originalObject)) {
err = Object.assign(err, {
message: `The object you are trying to update is deleted. ${err.message}`,
status: 403
Expand Down Expand Up @@ -72,7 +74,7 @@ const patchSet = async function (req, res, next) {
if (Object.keys(objectReceived).length === 0) {
//Then you aren't actually changing anything...there are no new properties
//Just hand back the object. The resulting of setting nothing is the object from the request body.
res.set(utils.configureWebAnnoHeadersFor(originalObject))
res.set(configureWebAnnoHeadersFor(originalObject))
originalObject = idNegotiation(originalObject)
originalObject.new_obj_state = JSON.parse(JSON.stringify(originalObject))
res.location(originalObject[_contextid(originalObject["@context"]) ? "id":"@id"])
Expand All @@ -82,7 +84,7 @@ const patchSet = async function (req, res, next) {
}
const id = ObjectID()
let context = patchedObject["@context"] ? { "@context": patchedObject["@context"] } : {}
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
let rerumProp = { "__rerum": configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
delete patchedObject["__rerum"]
delete patchedObject["_id"]
delete patchedObject["@id"]
Expand All @@ -92,7 +94,7 @@ const patchSet = async function (req, res, next) {
let result = await db.insertOne(newObject)
if (alterHistoryNext(originalObject, newObject["@id"])) {
//Success, the original object has been updated.
res.set(utils.configureWebAnnoHeadersFor(newObject))
res.set(configureWebAnnoHeadersFor(newObject))
newObject = idNegotiation(newObject)
newObject.new_obj_state = JSON.parse(JSON.stringify(newObject))
res.location(newObject[_contextid(newObject["@context"]) ? "id":"@id"])
Expand Down
12 changes: 7 additions & 5 deletions controllers/patchUnset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { isDeleted } from '../predicates.js'
import { configureRerumOptions } from '../versioning.js'
import { configureWebAnnoHeadersFor } from '../headers.js'
import config from '../config/index.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation, alterHistoryNext } from './utils.js'

Expand Down Expand Up @@ -43,7 +45,7 @@ const patchUnset = async function (req, res, next) {
status: 501
})
}
else if (utils.isDeleted(originalObject)) {
else if (isDeleted(originalObject)) {
err = Object.assign(err, {
message: `The object you are trying to update is deleted. ${err.message}`,
status: 403
Expand Down Expand Up @@ -74,7 +76,7 @@ const patchUnset = async function (req, res, next) {
if (Object.keys(objectReceived).length === 0) {
//Then you aren't actually changing anything...no properties in the request body were removed from the original object.
//Just hand back the object. The resulting of unsetting nothing is the object.
res.set(utils.configureWebAnnoHeadersFor(originalObject))
res.set(configureWebAnnoHeadersFor(originalObject))
originalObject = idNegotiation(originalObject)
originalObject.new_obj_state = JSON.parse(JSON.stringify(originalObject))
res.location(originalObject[_contextid(originalObject["@context"]) ? "id":"@id"])
Expand All @@ -84,7 +86,7 @@ const patchUnset = async function (req, res, next) {
}
const id = ObjectID()
let context = patchedObject["@context"] ? { "@context": patchedObject["@context"] } : {}
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
let rerumProp = { "__rerum": configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
delete patchedObject["__rerum"]
delete patchedObject["_id"]
delete patchedObject["@id"]
Expand All @@ -97,7 +99,7 @@ const patchUnset = async function (req, res, next) {
let result = await db.insertOne(newObject)
if (alterHistoryNext(originalObject, newObject["@id"])) {
//Success, the original object has been updated.
res.set(utils.configureWebAnnoHeadersFor(newObject))
res.set(configureWebAnnoHeadersFor(newObject))
newObject = idNegotiation(newObject)
newObject.new_obj_state = JSON.parse(JSON.stringify(newObject))
res.location(newObject[_contextid(newObject["@context"]) ? "id":"@id"])
Expand Down
12 changes: 7 additions & 5 deletions controllers/patchUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

import { newID, isValidID, db } from '../database/client.js'
import utils from '../utils.js'
import { isDeleted } from '../predicates.js'
import { configureRerumOptions } from '../versioning.js'
import { configureWebAnnoHeadersFor } from '../headers.js'
import config from '../config/index.js'
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation, alterHistoryNext } from './utils.js'

Expand Down Expand Up @@ -42,7 +44,7 @@ const patchUpdate = async function (req, res, next) {
status: 501
})
}
else if (utils.isDeleted(originalObject)) {
else if (isDeleted(originalObject)) {
err = Object.assign(err, {
message: `The object you are trying to update is deleted. ${err.message}`,
status: 403
Expand Down Expand Up @@ -73,7 +75,7 @@ const patchUpdate = async function (req, res, next) {
if (Object.keys(objectReceived).length === 0) {
//Then you aren't actually changing anything...only @id came through
//Just hand back the object. The resulting of patching nothing is the object unchanged.
res.set(utils.configureWebAnnoHeadersFor(originalObject))
res.set(configureWebAnnoHeadersFor(originalObject))
originalObject = idNegotiation(originalObject)
originalObject.new_obj_state = JSON.parse(JSON.stringify(originalObject))
res.location(originalObject[_contextid(originalObject["@context"]) ? "id":"@id"])
Expand All @@ -83,7 +85,7 @@ const patchUpdate = async function (req, res, next) {
}
const id = ObjectID()
let context = patchedObject["@context"] ? { "@context": patchedObject["@context"] } : {}
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
let rerumProp = { "__rerum": configureRerumOptions(generatorAgent, originalObject, true, false)["__rerum"] }
delete patchedObject["__rerum"]
delete patchedObject["_id"]
delete patchedObject["@id"]
Expand All @@ -96,7 +98,7 @@ const patchUpdate = async function (req, res, next) {
let result = await db.insertOne(newObject)
if (alterHistoryNext(originalObject, newObject["@id"])) {
//Success, the original object has been updated.
res.set(utils.configureWebAnnoHeadersFor(newObject))
res.set(configureWebAnnoHeadersFor(newObject))
newObject = idNegotiation(newObject)
newObject.new_obj_state = JSON.parse(JSON.stringify(newObject))
res.location(newObject[_contextid(newObject["@context"]) ? "id":"@id"])
Expand Down
Loading
Loading