1

so I'm investigating rdflib to use with ActivityStreams. My current main goal is to input an Object and get the same one back!

My current try looks like this

import rdflib


G = rdflib.Graph()
G.parse('https://raw.githubusercontent.com/HelgeKrueger/bovine/main/tests/data/mastodon_announce_1_undo.json', format='json-ld')
print(G.serialize(format='json-ld', 
                  auto_compact=False, 
                  use_native_types=True,
                  context="https://www.w3.org/ns/activitystreams", 
                  base="https://my_domain/activitypub/user/3c0281b7-bede-460a-a49b-3b6d7d4eb32f/activity"))

and gets the output

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "@graph": [
    {
      "actor": "https://first_domain/users/first",
      "id": "https://first_domain/users/first/statuses/1097854/activity/undo",
      "object": "https://first_domain/users/first/statuses/1097854/activity",
      "to": "as:Public",
      "type": "Undo"
    },
    {
      "actor": "https://first_domain/users/john",
      "cc": [
        "https://first_domain/users/john/followers",
        "https://second_domain/users/second"
      ],
      "id": "https://first_domain/users/first/statuses/1097854/activity",
      "object": "https://second_domain/users/second/statuses/109724234853",
      "published": "2023-01-31T19:11:46+00:00",
      "to": "as:Public",
      "type": "Announce"
    }
  ]
}

The signature is missing, because it's not part of the namespace -> This is as desired.

Unfortunately, there is the @graph property and the "Announce" is not represented as a subobject of

    {
      "actor": "https://first_domain/users/first",
      "id": "https://first_domain/users/first/statuses/1097854/activity/undo",
      "object": "https://first_domain/users/first/statuses/1097854/activity",
      "to": "as:Public",
      "type": "Undo"
    }

Is there a way to get rdflib to ouput it like:

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "actor": "https://first_domain/users/first",
  "id": "https://first_domain/users/first/statuses/1097854/activity/undo",
  "object": {
    "actor": "https://first_domain/users/john",
    "cc": [
      "https://first_domain/users/john/followers",
      "https://second_domain/users/second"
    ],
    "id": "https://first_domain/users/first/statuses/1097854/activity",
    "object": "https://second_domain/users/second/statuses/109724234853",
    "published": "2023-01-31T19:11:46+00:00",
    "to": "as:Public",
    "type": "Announce"
  },
  "to": "as:Public",
  "type": "Undo"
}

Thanks!

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65
Helge
  • 111
  • 1
  • 2

0 Answers0