feat: Keep track of last modified date of resources

This commit is contained in:
Joachim Van Herwegen
2021-08-13 16:48:44 +02:00
parent 47b3a2d77f
commit 97c534b2bf
6 changed files with 130 additions and 50 deletions

View File

@@ -3,8 +3,9 @@ import { BasicRepresentation } from '../ldp/representation/BasicRepresentation';
import type { Representation } from '../ldp/representation/Representation';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import { guardedStreamFrom } from './StreamUtil';
import { toLiteral } from './TermUtil';
import { LDP, RDF } from './Vocabularies';
import { DC, LDP, RDF, XSD } from './Vocabularies';
/**
* Helper function to generate type quads for a Container or Resource.
@@ -21,6 +22,18 @@ export function addResourceMetadata(metadata: RepresentationMetadata, isContaine
metadata.add(RDF.terms.type, LDP.terms.Resource);
}
/**
* Updates the dc:modified time to the given time.
* @param metadata - Metadata to update.
* @param date - Last modified date. Defaults to current time.
*/
export function updateModifiedDate(metadata: RepresentationMetadata, date = new Date()): void {
// Milliseconds get lost in some serializations, potentially causing mismatches
const lastModified = new Date(date);
lastModified.setMilliseconds(0);
metadata.set(DC.terms.modified, toLiteral(lastModified.toISOString(), XSD.terms.dateTime));
}
/**
* Helper function to clone a representation, the original representation can still be used.
* This function loads the entire stream in memory.