|
1 | 1 | # apmodel |
2 | | -ActivityStreams 2.0 and nodeinfo 2.0/2.1 implementation for Python. |
3 | | -## Other ActivityPub Tools |
4 | | -- [apsig: Signature implementation used in ActivityPub](https://github.com/AmaseCocoa/apsig) |
5 | | -- [apkit: Powerful Toolkit for ActivityPub Implementations](https://github.com/AmaseCocoa/apkit) |
6 | | -## Links |
7 | | -- [Official Fedi Account](https://hollo.amase.cc/@apkit) |
| 2 | + |
| 3 | +[](https://badge.fury.io/py/apmodel) |
| 4 | +[](https://github.com/astral-sh/ruff) |
| 5 | + |
| 6 | +Pydantic models for ActivityStreams 2.0, NodeInfo, and other Fediverse-related vocabularies. |
| 7 | + |
| 8 | +`apmodel` provides model implementations for: |
| 9 | +- Activity Streams 2.0 |
| 10 | +- [NodeInfo](https://nodeinfo.diasporafoundation.org/protocol.html) (2.0 and 2.1) |
| 11 | +- [CryptographicKey](https://w3c-ccg.github.io/security-vocab/contexts/security-v1.jsonld) (Security Vocabulary v1) |
| 12 | +- [Multikey](https://w3c-ccg.github.io/multikey/) and [DataIntegrityProof](https://w3c-ccg.github.io/data-integrity-spec/) (Controlled Identifiers v1.0) |
| 13 | +- [PropertyValue](https://schema.org/PropertyValue) (schema.org) |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Type-Safe Models**: Leverages Pydantic for robust and type-safe data validation and manipulation. |
| 18 | +- **Automatic Model Loading**: A `load` function that automatically reads the `type` field from a dictionary and converts it to the correct Pydantic model. |
| 19 | +- **JSON-LD Aware**: Designed to work seamlessly with JSON-LD data, automatically handling expanded formats (e.g., values wrapped in arrays). |
| 20 | +- **Flexible Deserialization**: Handles unknown properties gracefully, allowing you to work with custom extensions in the ActivityPub ecosystem. Extra fields are attached directly to the model object. |
| 21 | +- **Comprehensive Vocabulary**: Includes a wide range of models from the ActivityStreams vocabulary, as well as common extensions used in the Fediverse. |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +```bash |
| 26 | +pip install apmodel |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Loading Objects |
| 32 | + |
| 33 | +Use the `apmodel.load()` function to parse a dictionary into a specific ActivityStreams model. The function inspects the `type` field to determine the correct model. |
| 34 | + |
| 35 | +```python |
| 36 | +import apmodel |
| 37 | + |
| 38 | +# Example ActivityStreams Note object as a dictionary |
| 39 | +note_data = { |
| 40 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 41 | + "type": "Note", |
| 42 | + "content": "This is a simple note.", |
| 43 | + "published": "2025-05-10T12:00:00Z", |
| 44 | + "to": ["https://www.w3.org/ns/activitystreams#Public"], |
| 45 | + "cc": ["https://example.com/users/alice/followers"] |
| 46 | +} |
| 47 | + |
| 48 | +# Load the dictionary into a Note object |
| 49 | +note_object = apmodel.load(note_data) |
| 50 | + |
| 51 | +# note_object is now an instance of apmodel.vocab.Note |
| 52 | +print(type(note_object)) |
| 53 | +# > <class 'apmodel.vocab.note.Note'> |
| 54 | + |
| 55 | +# You can access attributes with type safety |
| 56 | +print(note_object.content) |
| 57 | +# > This is a simple note. |
| 58 | + |
| 59 | +# The context is also parsed into an LDContext object |
| 60 | +print(note_object.context.to_json()) |
| 61 | +# > ["https://www.w3.org/ns/activitystreams"] |
| 62 | +``` |
| 63 | + |
| 64 | +### Creating Objects |
| 65 | + |
| 66 | +You can also create objects programmatically. |
| 67 | + |
| 68 | +```python |
| 69 | +from apmodel.vocab import Create |
| 70 | +from apmodel.vocab import Note |
| 71 | + |
| 72 | +note = Note(content="My new note!") |
| 73 | +create_activity = Create( |
| 74 | + actor="https://example.com/users/bob", |
| 75 | + object=note, |
| 76 | +) |
| 77 | + |
| 78 | +# The model can be converted back to a dictionary |
| 79 | +# (Note: a dedicated `dump` function is not yet implemented, |
| 80 | +# but you can use Pydantic's `model_dump` for now) |
| 81 | +activity_dict = create_activity.model_dump(by_alias=True, exclude_none=True) |
| 82 | + |
| 83 | +import json |
| 84 | +print(json.dumps(activity_dict, indent=2)) |
| 85 | +``` |
| 86 | + |
| 87 | +This will output: |
| 88 | +```json |
| 89 | +{ |
| 90 | + "@context": [ |
| 91 | + "https://www.w3.org/ns/activitystreams" |
| 92 | + ], |
| 93 | + "@type": "Create", |
| 94 | + "actor": "https://example.com/users/bob", |
| 95 | + "object": { |
| 96 | + "@context": [ |
| 97 | + "https://www.w3.org/ns/activitystreams" |
| 98 | + ], |
| 99 | + "@type": "Note", |
| 100 | + "content": "My new note!" |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Development |
| 106 | + |
| 107 | +This project uses [Task](https://taskfile.dev/) for running scripts. |
| 108 | + |
| 109 | +### Setup Dev Environment |
| 110 | + |
| 111 | +Installs development dependencies. |
| 112 | + |
| 113 | +```bash |
| 114 | +task |
| 115 | +``` |
| 116 | + |
| 117 | +### Run Tests |
| 118 | + |
| 119 | +```bash |
| 120 | +task test |
| 121 | +``` |
| 122 | + |
| 123 | +## License |
| 124 | + |
| 125 | +This project is licensed under the MIT License. |
0 commit comments