.activity_pub.muck_out
cattle_grid.activity_pub.muck_out
This package contains tools to turn ActivityPub messages into something cleaner. Its takes include
- Normalization
- Validation
attachment
normalize_attachment(attachment)
Normalizes an attachment
Source code in cattle_grid/activity_pub/muck_out/attachment.py
normalize
Routings to normalize an ActivityPub activity
normalize_activity(activity, actor=None)
Normalizes activities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity
|
dict
|
The activity being normalized |
required |
actor
|
str | None
|
Actor receiving this activity |
None
|
Returns:
| Type | Description |
|---|---|
Activity
|
|
Source code in cattle_grid/activity_pub/muck_out/normalize.py
normalize_id(activity)
Creates a normalized id
>>> normalize_id({"id": "http://id.example"})
'http://id.example'
>>> normalize_id({})
Traceback (most recent call last):
...
ValueError: Cannot fake id if actor is not present
Source code in cattle_grid/activity_pub/muck_out/normalize.py
normalize_object(obj)
Normalizes an object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict
|
The object to be normalized |
required |
Returns:
| Type | Description |
|---|---|
Object
|
|
Source code in cattle_grid/activity_pub/muck_out/normalize.py
normalize_to(value, actor)
Normalizes the to value
>>> normalize_to(None, "http://actor.example")
['http://actor.example']
>>> normalize_to("http://to.example", None)
['http://to.example']
>>> normalize_to(["http://alice.example", "http://bob.example"], None)
['http://alice.example', 'http://bob.example']
Source code in cattle_grid/activity_pub/muck_out/normalize.py
types
Activity
Bases: Common
This represents a first draft of a json-schema that every activities exchanged between servers MUST satisfy and be able to parse. Here ‘being able to parse’ means making it to the point, where depending on the type, you decide what side effects to perform.
Generally, the fields actor, to, and cc (and maybe bcc — not transported) represent how the message is being delivered. The fields actor, type, object, target, content represent how the message is processed by the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_context
|
str | List[Any] | None
|
|
None
|
id
|
str
|
id of the activity, can be assumed to be globally unique. Some activities such as a Follow request will require an id to be valid. Servers may assume an id to be required. As assigning an id is ‘trivial’, one should assign one. |
required |
to
|
List[str]
|
Array of actors this activity is addressed to. It is sane to assume that an activity is addressed to at least one person. |
required |
cc
|
List[str] | None
|
Array of actors this activity or object is carbon copied to. |
None
|
published
|
str | None
|
Moment of this activity or object being published |
None
|
type
|
str
|
Type of the activity. Side effects of this activity are determine by this type. |
required |
actor
|
str
|
id of the actor performing this activity. One can assume that the activity is signed by this actor (in some form). |
required |
object
|
str | Object | None
|
|
None
|
target
|
str | Dict[str, Any] | None
|
The target, not sure if needed, included for completeness |
None
|
content
|
str | None
|
The content used for example to represent the Emote for a like |
None
|
Source code in cattle_grid/activity_pub/muck_out/types.py
Common
Bases: BaseModel
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_context
|
str | List[Any] | None
|
|
None
|
id
|
str
|
id of the activity or object, can be assumed to be globally unique. Some activities such as a Follow request will require an id to be valid. Servers may assume an id to be required. As assigning an id is ‘trivial’, one should assign one. |
required |
to
|
List[str]
|
Array of actors this activity or object is addressed to. It is sane to assume that an activity is addressed to at least one person. |
required |
cc
|
List[str] | None
|
Array of actors this activity or object is carbon copied to. |
None
|
published
|
str | None
|
Moment of this activity or object being published |
None
|
type
|
str
|
Type of the activity or activity. Side effects of this activity are determine by this type. |
required |
Source code in cattle_grid/activity_pub/muck_out/types.py
Object
Bases: Common
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_context
|
str | List[Any] | None
|
|
None
|
id
|
str
|
id of the activity, can be assumed to be globally unique. Some activities such as a Follow request will require an id to be valid. Servers may assume an id to be required. As assigning an id is ‘trivial’, one should assign one. |
required |
to
|
List[str]
|
Array of actors this activity is addressed to. It is sane to assume that an activity is addressed to at least one person. |
required |
cc
|
List[str] | None
|
Array of actors this activity or object is carbon copied to. |
None
|
published
|
str | None
|
Moment of this activity or object being published |
None
|
type
|
str
|
Type of the activity. Side effects of this activity are determine by this type. |
required |
attributedTo
|
str | None
|
id of the actor that authored this object |
None
|
content
|
str
|
|
required |
summary
|
str | None
|
|
None
|
name
|
str | None
|
|
None
|
attachment
|
List[Dict[str, Any]] | None
|
|
None
|
tag
|
List[Dict[str, Any]] | None
|
|
None
|
url
|
List[Union[Dict[str, Any], str]] | None
|
|
None
|
sensitive
|
bool | None
|
|
None
|
inReplyTo
|
str | None
|
|
None
|
Source code in cattle_grid/activity_pub/muck_out/types.py
attachment = None
class-attribute
instance-attribute
A list of objects that are attached to the original object
content
instance-attribute
The content of the object
inReplyTo = None
class-attribute
instance-attribute
The object being replied to. Currently a string. Not sure if this is what I want.
name = None
class-attribute
instance-attribute
The name of the object
sensitive = None
class-attribute
instance-attribute
Marks the object as sensitive. Currently, used by everyone, a better way would be an element of the tag list that labels the object as sensitive due a reason
summary = None
class-attribute
instance-attribute
The summary of the object
tag = None
class-attribute
instance-attribute
A list of objects that expand on the content of the object
url = None
class-attribute
instance-attribute
A list of urls that expand on the content of the object
utils
allowed_html_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul', 'p', 'br', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr']
module-attribute
The currently allowed list of html tags
list_from_value(value)
Transforms a list into a value
>>> list_from_value(["aaa"])
['aaa']
>>> list_from_value("aaa")
['aaa']
>>> list_from_value({"a": 1})
[{'a': 1}]
>>> list_from_value([])
>>> list_from_value(None)
Returns:
| Type | Description |
|---|---|
List[Any] | None
|
A list or None in case of an empty list or None as argument |
Source code in cattle_grid/activity_pub/muck_out/utils.py
sanitize_html(value)
Cleans html
>>> sanitize_html("<p>text</p>")
'<p>text</p>'
>>> sanitize_html("<script>alert('xss')</script>")
"<script>alert('xss')</script>"