rdflib.js icon indicating copy to clipboard operation
rdflib.js copied to clipboard

Support reading PeerTube application/activity+json

Open jg10-mastodon-social opened this issue 8 months ago • 5 comments

I managed to get use rdflib.js to read the JSON-LD in https://spectra.video/videos/watch/3a0159d9-4f12-4dd7-b025-afbb663cd19c

Here's what I ended up doing (from a SolidOS instance):

let kb=panes.UI.store
panes.UI.store.fetcher.mediatypes["application/activity+json"]={}
panes.UI.store.fetcher.handlers[6].pattern=new RegExp("application/activity\\+json")
panes.UI.store.fetcher.load(kb.sym("https://spectra.video/videos/watch/3a0159d9-4f12-4dd7-b025-afbb663cd19c"),{force:true,headers:{},credentials: "omit"}).then(
 ()=>console.log(kb.match(kb.sym("https://spectra.video/videos/watch/3a0159d9-4f12-4dd7-b025-afbb663cd19c")))
)

Specifying the media type is required to adjust the accept header, otherwise PeerTube returns html. The new accept header is image/*;q=0.9, */*;q=0.1, application/rdf+xml;q=0.9, application/xhtml+xml, text/xml;q=0.5, application/xml;q=0.5, text/html;q=0.9, text/plain;q=0.5, text/n3;q=1.0, text/turtle;q=1, application/ld+json;q=0.9, application/activity+json

Specifying the handler pattern is required so that load recognises the media type as parseable by the jsonldparser. To still support jsonld, this should probably be something like pattern=new RegExp("application/(ld|activity)\\+json")

I don't know why the options to load are needed but they seem to be.

jg10-mastodon-social avatar Apr 28 '25 12:04 jg10-mastodon-social

~I think the better push here is to get ActivityPub and PeerTube to respond with application/activity+ld+json content types. I've opened https://github.com/w3c/activitypub/issues/501 on the AP spec repo.~

Ignore: see Teds comment below

jeswr avatar Apr 28 '25 12:04 jeswr

Note that application/activity+ld+json is not and probably will never be a validly registered IANA media type. Multiple plus signs are disallowed.

TallTed avatar Apr 28 '25 16:04 TallTed

Peertube should already respond to the LD JSON header IMO, based on my reading of the spec. There's no harm in including the activity+json header as well though

nightpool avatar Apr 29 '25 04:04 nightpool

Ah, I see the issue. you're listing text/html and application/ld+json at the same priority level. So Peertube is going to respond with HTML to break the tie. Instead, you should put html at a lower priority level than ld+json

nightpool avatar Apr 29 '25 04:04 nightpool

Instead, you should put html at a lower priority level than ld+json

Can confirm that works:

let kb=panes.UI.store
panes.UI.store.fetcher.mediatypes["text/html"]={q:0.8}
panes.UI.store.fetcher.handlers[6].pattern=new RegExp("application/activity\\+json")
panes.UI.store.fetcher.load(kb.sym("https://spectra.video/videos/watch/3a0159d9-4f12-4dd7-b025-afbb663cd19c"),{force:true,headers:{},credentials: "omit"}).then(
 ()=>console.log(kb.match(kb.sym("https://spectra.video/videos/watch/3a0159d9-4f12-4dd7-b025-afbb663cd19c")))
)

jg10-mastodon-social avatar Apr 29 '25 09:04 jg10-mastodon-social