1+ import { ODRL } from "@solidlab/ucp" ;
12import { Type , array , string , optional , any , union } from "../util/ReType" ;
23
34export const JsonLdIdentifier = {
45 '@id' : string
56}
67
78export const StringOrJsonLdIdentifier = union ( string , JsonLdIdentifier )
9+ export const IdentifierSet = union ( StringOrJsonLdIdentifier , array ( StringOrJsonLdIdentifier ) )
10+ export const ODRLAssetCollection = {
11+ "@type" : string ,
12+ "source" : string ,
13+ }
14+ export const ODRLTargetOrAssetCollection = union ( StringOrJsonLdIdentifier , ODRLAssetCollection )
15+
816
917export const ODRLConstraint = {
1018 "@type" : optional ( string ) ,
@@ -20,7 +28,7 @@ export const ODRLPermission = {
2028 "@id" : optional ( string ) ,
2129 uid : optional ( string ) ,
2230 action : StringOrJsonLdIdentifier ,
23- target : StringOrJsonLdIdentifier , // resourceURL
31+ target : ODRLTargetOrAssetCollection , // resourceURL
2432 assigner : StringOrJsonLdIdentifier , // user WebID
2533 assignee : StringOrJsonLdIdentifier , // target WebID
2634 constraint : optional ( array ( ODRLConstraint ) )
@@ -41,9 +49,49 @@ export type ODRLPermission = Type<typeof ODRLPermission>;
4149export type ODRLContract = Type < typeof ODRLContract > ;
4250export type JsonLdIdentifier = Type < typeof JsonLdIdentifier > ;
4351export type StringOrJsonLdIdentifier = Type < typeof StringOrJsonLdIdentifier > ;
52+ export type IdentifierSet = Type < typeof IdentifierSet > ;
53+ export type ODRLAssetCollection = Type < typeof ODRLAssetCollection > ;
54+ export type ODRLTargetOrAssetCollection = Type < typeof ODRLTargetOrAssetCollection > ;
4455
4556
4657export function convertStringOrJsonLdIdentifierToString ( x : StringOrJsonLdIdentifier ) : string {
4758 const id = ( x as JsonLdIdentifier ) [ "@id" ]
4859 return id ? id : x as string
60+ }
61+
62+ /**
63+ * Note: This check makes the assumption of slash-semantics based resource ordering!
64+ * @param url
65+ * @param policyTarget
66+ * @returns
67+ */
68+ export function isPolicyTarget ( url : string , policyTarget : ODRLTargetOrAssetCollection ) {
69+ // AssetCollection
70+ const assetCollectionType = ( policyTarget as ODRLAssetCollection ) [ "@type" ]
71+ if ( assetCollectionType && assetCollectionType === ODRL . namespace + "AssetCollection" ) {
72+ return url . startsWith ( ( policyTarget as ODRLAssetCollection ) . source )
73+ }
74+
75+ // @id identfier
76+ const id = ( policyTarget as JsonLdIdentifier ) [ "@id" ]
77+ if ( id && url === id ) return true
78+
79+ // string
80+ return ( policyTarget as string ) === url
81+
82+ }
83+
84+ export function getPolicyTargets ( policyTarget : ODRLTargetOrAssetCollection ) : string {
85+ // AssetCollection
86+ const assetCollectionType = ( policyTarget as ODRLAssetCollection ) [ "@type" ]
87+ if ( assetCollectionType && assetCollectionType === ODRL . namespace + "AssetCollection" ) {
88+ return ( policyTarget as ODRLAssetCollection ) . source
89+ }
90+
91+ // @id identfier
92+ const id = ( policyTarget as JsonLdIdentifier ) [ "@id" ]
93+ if ( id ) return id
94+
95+ // string
96+ return policyTarget as string
4997}
0 commit comments