mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-20 14:21:00 +00:00
Fix canonicalize (#590)
This commit is contained in:
parent
9038e498d5
commit
226a60bec7
3 changed files with 106 additions and 23 deletions
52
core/ld.py
52
core/ld.py
|
@ -547,6 +547,18 @@ schemas = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"schema.org": {
|
||||||
|
"contentType": "application/ld+json",
|
||||||
|
"documentUrl": "https://schema.org/docs/jsonldcontext.json",
|
||||||
|
"contextUrl": None,
|
||||||
|
"document": {
|
||||||
|
"@context": {
|
||||||
|
"schema": "http://schema.org/",
|
||||||
|
"PropertyValue": {"@id": "schema:PropertyValue"},
|
||||||
|
"value": {"@id": "schema:value"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.Z"
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.Z"
|
||||||
|
@ -592,24 +604,32 @@ def canonicalise(json_data: dict, include_security: bool = False) -> dict:
|
||||||
"""
|
"""
|
||||||
if not isinstance(json_data, dict):
|
if not isinstance(json_data, dict):
|
||||||
raise ValueError("Pass decoded JSON data into LDDocument")
|
raise ValueError("Pass decoded JSON data into LDDocument")
|
||||||
context = [
|
|
||||||
"https://www.w3.org/ns/activitystreams",
|
context = json_data.get("@context", [])
|
||||||
{
|
|
||||||
"blurhash": "toot:blurhash",
|
if not isinstance(context, list):
|
||||||
"Emoji": "toot:Emoji",
|
context = [context]
|
||||||
"focalPoint": {"@container": "@list", "@id": "toot:focalPoint"},
|
|
||||||
"Hashtag": "as:Hashtag",
|
if not context:
|
||||||
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
|
context.append("https://www.w3.org/ns/activitystreams")
|
||||||
"sensitive": "as:sensitive",
|
context.append(
|
||||||
"toot": "http://joinmastodon.org/ns#",
|
{
|
||||||
"votersCount": "toot:votersCount",
|
"blurhash": "toot:blurhash",
|
||||||
"featured": {"@id": "toot:featured", "@type": "@id"},
|
"Emoji": "toot:Emoji",
|
||||||
},
|
"focalPoint": {"@container": "@list", "@id": "toot:focalPoint"},
|
||||||
]
|
"Hashtag": "as:Hashtag",
|
||||||
|
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
|
||||||
|
"sensitive": "as:sensitive",
|
||||||
|
"toot": "http://joinmastodon.org/ns#",
|
||||||
|
"votersCount": "toot:votersCount",
|
||||||
|
"featured": {"@id": "toot:featured", "@type": "@id"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if include_security:
|
if include_security:
|
||||||
context.append("https://w3id.org/security/v1")
|
context.append("https://w3id.org/security/v1")
|
||||||
if "@context" not in json_data:
|
|
||||||
json_data["@context"] = context
|
json_data["@context"] = context
|
||||||
|
|
||||||
return jsonld.compact(jsonld.expand(json_data), context)
|
return jsonld.compact(jsonld.expand(json_data), context)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import datetime
|
||||||
|
|
||||||
from dateutil.tz import tzutc
|
from dateutil.tz import tzutc
|
||||||
|
|
||||||
from core.ld import parse_ld_date
|
from core.ld import canonicalise, parse_ld_date
|
||||||
|
|
||||||
|
|
||||||
def test_parse_ld_date():
|
def test_parse_ld_date():
|
||||||
|
@ -41,3 +41,68 @@ def test_parse_ld_date():
|
||||||
tzinfo=tzutc(),
|
tzinfo=tzutc(),
|
||||||
)
|
)
|
||||||
assert difference.total_seconds() == 0
|
assert difference.total_seconds() == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_canonicalise_single_attachment():
|
||||||
|
data = {
|
||||||
|
"@context": [
|
||||||
|
"https://www.w3.org/ns/activitystreams",
|
||||||
|
{
|
||||||
|
"schema": "http://schema.org#",
|
||||||
|
"PropertyValue": "schema:PropertyValue",
|
||||||
|
"value": "schema:value",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"attachment": [
|
||||||
|
{
|
||||||
|
"type": "http://schema.org#PropertyValue",
|
||||||
|
"name": "Location",
|
||||||
|
"http://schema.org#value": "Test Location",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed = canonicalise(data)
|
||||||
|
attachment = parsed["attachment"]
|
||||||
|
|
||||||
|
assert attachment["type"] == "PropertyValue"
|
||||||
|
assert attachment["name"] == "Location"
|
||||||
|
assert attachment["value"] == "Test Location"
|
||||||
|
|
||||||
|
|
||||||
|
def test_canonicalise_multiple_attachment():
|
||||||
|
data = {
|
||||||
|
"@context": [
|
||||||
|
"https://www.w3.org/ns/activitystreams",
|
||||||
|
{
|
||||||
|
"schema": "http://schema.org#",
|
||||||
|
"PropertyValue": "schema:PropertyValue",
|
||||||
|
"value": "schema:value",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"attachment": [
|
||||||
|
{
|
||||||
|
"type": "http://schema.org#PropertyValue",
|
||||||
|
"name": "Attachment 1",
|
||||||
|
"http://schema.org#value": "Test 1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "http://schema.org#PropertyValue",
|
||||||
|
"name": "Attachment 2",
|
||||||
|
"http://schema.org#value": "Test 2",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed = canonicalise(data)
|
||||||
|
attachment = parsed["attachment"]
|
||||||
|
|
||||||
|
assert len(attachment) == 2
|
||||||
|
|
||||||
|
assert attachment[0]["type"] == "PropertyValue"
|
||||||
|
assert attachment[0]["name"] == "Attachment 1"
|
||||||
|
assert attachment[0]["value"] == "Test 1"
|
||||||
|
|
||||||
|
assert attachment[1]["type"] == "PropertyValue"
|
||||||
|
assert attachment[1]["name"] == "Attachment 2"
|
||||||
|
assert attachment[1]["value"] == "Test 2"
|
||||||
|
|
|
@ -854,16 +854,14 @@ class Identity(StatorModel):
|
||||||
self.metadata = []
|
self.metadata = []
|
||||||
for attachment in get_list(document, "attachment"):
|
for attachment in get_list(document, "attachment"):
|
||||||
if (
|
if (
|
||||||
attachment["type"] == "http://schema.org#PropertyValue"
|
attachment["type"] == "PropertyValue"
|
||||||
and "name" in attachment
|
and "name" in attachment
|
||||||
and "http://schema.org#value" in attachment
|
and "value" in attachment
|
||||||
):
|
):
|
||||||
self.metadata.append(
|
self.metadata.append(
|
||||||
{
|
{
|
||||||
"name": attachment.get("name"),
|
"name": attachment["name"],
|
||||||
"value": FediverseHtmlParser(
|
"value": FediverseHtmlParser(attachment["value"]).html,
|
||||||
attachment.get("http://schema.org#value")
|
|
||||||
).html,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
# Now go do webfinger with that info to see if we can get a canonical domain
|
# Now go do webfinger with that info to see if we can get a canonical domain
|
||||||
|
|
Loading…
Reference in a new issue