diff options
author | Andrew Godwin | 2022-11-06 00:07:38 -0600 |
---|---|---|
committer | Andrew Godwin | 2022-11-06 00:07:38 -0600 |
commit | 8aec395331a1e9ec4ef1ea38aa20b8517131133b (patch) | |
tree | 3fde4cecff9f24ac1c9fdc46fae1ac5714c76ac7 /core | |
parent | a2404e01cdbeef2ba332e147a5f2f1ca0a0310d7 (diff) | |
download | takahe-8aec395331a1e9ec4ef1ea38aa20b8517131133b.tar.gz takahe-8aec395331a1e9ec4ef1ea38aa20b8517131133b.tar.bz2 takahe-8aec395331a1e9ec4ef1ea38aa20b8517131133b.zip |
Move to the more sensible JSON-LD repr
Diffstat (limited to 'core')
-rw-r--r-- | core/ld.py | 60 |
1 files changed, 13 insertions, 47 deletions
@@ -6,7 +6,7 @@ from pyld.jsonld import JsonLdError schemas = { "www.w3.org/ns/activitystreams": { "contentType": "application/ld+json", - "documentUrl": "https://www.w3.org/ns/activitystreams", + "documentUrl": "http://www.w3.org/ns/activitystreams", "contextUrl": None, "document": { "@context": { @@ -177,7 +177,7 @@ schemas = { }, "w3id.org/security/v1": { "contentType": "application/ld+json", - "documentUrl": "https://w3id.org/security/v1", + "documentUrl": "http://w3id.org/security/v1", "contextUrl": None, "document": { "@context": { @@ -252,51 +252,17 @@ def builtin_document_loader(url: str, options={}): ) -class LDDocument: +def canonicalise(json_data): """ - Utility class for dealing with a document a bit more easily - """ - - def __init__(self, json_data): - self.items = {} - for entry in jsonld.flatten(jsonld.expand(json_data)): - item = LDItem(self, entry) - self.items[item.id] = item - - def by_type(self, type): - for item in self.items.values(): - if item.type == type: - yield item - + Given an ActivityPub JSON-LD document, round-trips it through the LD + systems to end up in a canonicalised, compacted format. -class LDItem: + For most well-structured incoming data this won't actually do anything, + but it's probably good to abide by the spec. """ - Represents a single item in an LDDocument - """ - - def __init__(self, document, data): - self.data = data - self.document = document - self.id = self.data["@id"] - if "@type" in self.data: - self.type = self.data["@type"][0] - else: - self.type = None - - def get(self, key): - """ - Gets the first value of the given key, or None if it's not present. - If it's an ID reference, returns the other Item if possible, or the raw - ID if it's not supplied. - """ - contents = self.data.get(key) - if not contents: - return None - id = contents[0].get("@id") - value = contents[0].get("@value") - if value is not None: - return value - if id in self.document.items: - return self.document.items[id] - else: - return id + if not isinstance(json_data, (dict, list)): + raise ValueError("Pass decoded JSON data into LDDocument") + return jsonld.compact( + jsonld.expand(json_data), + ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"], + ) |