mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-26 09:11:00 +00:00
Deal with unknown json-ld schemas (#644)
Rather than raising an error, returns an empty schema.
This commit is contained in:
parent
8c832383e0
commit
1e8a392e57
1 changed files with 14 additions and 14 deletions
28
core/ld.py
28
core/ld.py
|
@ -4,11 +4,18 @@ import urllib.parse as urllib_parse
|
||||||
|
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
from pyld import jsonld
|
from pyld import jsonld
|
||||||
from pyld.jsonld import JsonLdError
|
|
||||||
|
|
||||||
from core.exceptions import ActivityPubFormatError
|
from core.exceptions import ActivityPubFormatError, capture_message
|
||||||
|
|
||||||
schemas = {
|
schemas = {
|
||||||
|
"unknown": {
|
||||||
|
"contentType": "application/ld+json",
|
||||||
|
"documentUrl": "unknown",
|
||||||
|
"contextUrl": None,
|
||||||
|
"document": {
|
||||||
|
"@context": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
"www.w3.org/ns/activitystreams": {
|
"www.w3.org/ns/activitystreams": {
|
||||||
"contentType": "application/ld+json",
|
"contentType": "application/ld+json",
|
||||||
"documentUrl": "http://www.w3.org/ns/activitystreams",
|
"documentUrl": "http://www.w3.org/ns/activitystreams",
|
||||||
|
@ -622,12 +629,8 @@ def builtin_document_loader(url: str, options={}):
|
||||||
# Get URL without scheme
|
# Get URL without scheme
|
||||||
pieces = urllib_parse.urlparse(url)
|
pieces = urllib_parse.urlparse(url)
|
||||||
if pieces.hostname is None:
|
if pieces.hostname is None:
|
||||||
raise JsonLdError(
|
capture_message(f"No host name for json-ld schema: {url!r}")
|
||||||
f"No schema built-in for {url!r}",
|
return schemas["unknown"]
|
||||||
"jsonld.LoadDocumentError",
|
|
||||||
code="loading document failed",
|
|
||||||
cause="NoHostnameError",
|
|
||||||
)
|
|
||||||
key = pieces.hostname + pieces.path.rstrip("/")
|
key = pieces.hostname + pieces.path.rstrip("/")
|
||||||
try:
|
try:
|
||||||
return schemas[key]
|
return schemas[key]
|
||||||
|
@ -636,12 +639,9 @@ def builtin_document_loader(url: str, options={}):
|
||||||
key = "*" + pieces.path.rstrip("/")
|
key = "*" + pieces.path.rstrip("/")
|
||||||
return schemas[key]
|
return schemas[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise JsonLdError(
|
# return an empty context instead of throwing an error
|
||||||
f"No schema built-in for {key!r}",
|
capture_message(f"Ignoring unknown json-ld schema: {url!r}")
|
||||||
"jsonld.LoadDocumentError",
|
return schemas["unknown"]
|
||||||
code="loading document failed",
|
|
||||||
cause="KeyError",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def canonicalise(json_data: dict, include_security: bool = False) -> dict:
|
def canonicalise(json_data: dict, include_security: bool = False) -> dict:
|
||||||
|
|
Loading…
Reference in a new issue