LemmyNet / activitypub-federation-rust

High-level Rust library for the Activitypub protocol
GNU Affero General Public License v3.0
430 stars 47 forks source link

How does this crate (and Lemmy) handle the dynamic nature of JSON-LD? #92

Closed anderspitman closed 7 months ago

anderspitman commented 11 months ago

As mentioned here, part of the difficulty of implementing AP with static languages is that a given JSON-LD property might be a reference (IRI) to an object, or the object itself. The crate docs say:

This means we don’t use json-ld which Activitypub is based on, but that doesn’t cause any problems in practice.

I'm curious how Lemmy sidesteps this issue. Following along the examples in the docs it seems to assume a given property will always return either an IRI or an object, which doesn't handle the full functionality of AP/JSON-LD according to the specs. Am I missing something here?

Nutomic commented 11 months ago

This crate doesnt handle JSON-LD at all, message parsing is left to each application. Instead it mainly takes care of network connections and signatures. In fact you dont need to implement JSON-LD for federation, the Activitypub spec explicitly says that documents can be understood as plain JSON:

Screenshot_20240105_104800

That is what Lemmy does, documents are handled as plain JSON using serde. You can see the parsing code here. With serde you can also support fields having different types like single value or array, see these helpers. And in practice it doesnt really matter which possible types the spec assigns to a field, but only what the existing platforms send. In most cases there is only one type used per field.

You could also use the activitystreams crate to parse things properly, but I found it way too verbose and impractical so I abandoned that approach.

anderspitman commented 3 months ago

@Nutomic sorry this is a super late response but just wanted to say thanks for the helpful reply. I'm back working on the project that prompted the question and this clarified things a lot.

Nutomic commented 2 months ago

Good to know! What is your project about?

anderspitman commented 2 months ago

Don't have anything to link you to yet, sorry. Basically I'm experimenting with rendering ActivityPub conversations in different layouts. Since replies and inReplyTo can be used to construct a tree, the user should be able to seamlessly switch between linear (twitter, forum, etc), chat (latest at bottom), tree (reddit, HN), or other views depending on the conversation they're looking at.