moviewyrm/fedireads/activitypub_templates.py

122 lines
3.1 KiB
Python
Raw Normal View History

2020-01-27 01:55:02 +00:00
''' generates activitypub formatted objects '''
2020-01-27 02:49:57 +00:00
from uuid import uuid4
from fedireads.settings import DOMAIN
2020-01-27 04:57:48 +00:00
from datetime import datetime
2020-01-27 02:49:57 +00:00
2020-01-27 04:57:48 +00:00
def outbox_collection(user, size):
''' outbox okay cool '''
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': '%s/outbox' % user.actor,
'type': 'OrderedCollection',
'totalItems': size,
'first': '%s/outbox?page=true' % user.actor,
'last': '%s/outbox?min_id=0&page=true' % user.actor
}
2020-01-27 01:55:02 +00:00
2020-01-27 04:57:48 +00:00
def shelve_activity(user, book, shelf):
2020-01-27 01:55:02 +00:00
''' a user puts a book on a shelf.
activitypub action type Add
https://www.w3.org/ns/activitystreams#Add '''
book_title = book.data['title']
summary = '%s added %s to %s' % (
user.username,
book_title,
shelf.name
)
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'summary': summary,
'type': 'Add',
2020-01-27 03:50:22 +00:00
'actor': user.actor,
2020-01-27 01:55:02 +00:00
'object': {
'type': 'Document',
'name': book_title,
'url': book.openlibary_key
},
'target': {
'type': 'Collection',
'name': shelf.name,
'id': shelf.activitypub_id
}
}
2020-01-27 04:57:48 +00:00
def create_activity(user, obj):
''' wraps any object we're broadcasting '''
uuid = uuid4()
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': str(uuid),
'type': 'Create',
'actor': user.actor,
'to': ['%s/followers' % user.actor],
'cc': ['https://www.w3.org/ns/activitystreams#Public'],
'object': obj,
}
def note_object(user, content):
''' a lil post '''
uuid = uuid4()
return {
'id': str(uuid),
'type': 'Note',
'published': datetime.utcnow().isoformat(),
'attributedTo': user.actor,
'content': content,
'to': 'https://www.w3.org/ns/activitystreams#Public'
}
2020-01-27 03:50:22 +00:00
def follow_request(user, follow):
''' ask to be friends '''
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'summary': '',
'type': 'Follow',
'actor': user.actor,
'object': follow,
}
2020-01-27 02:49:57 +00:00
def accept_follow(activity, user):
''' say YES! to a user '''
uuid = uuid4()
return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': 'https://%s/%s' % (DOMAIN, uuid),
'type': 'Accept',
2020-01-27 03:50:22 +00:00
'actor': user.actor,
2020-01-27 02:49:57 +00:00
'object': activity,
}
2020-01-27 03:50:22 +00:00
def actor(user):
''' format an actor object from a user '''
return {
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'id': user.actor,
'type': 'Person',
'preferredUsername': user.username,
2020-01-27 04:57:48 +00:00
'inbox': inbox(user),
'followers': '%s/followers' % user.actor,
2020-01-27 03:50:22 +00:00
'publicKey': {
2020-01-27 04:57:48 +00:00
'id': '%s/#main-key' % user.actor,
'owner': user.actor,
2020-01-27 03:50:22 +00:00
'publicKeyPem': user.public_key,
}
}
def inbox(user):
''' describe an inbox '''
2020-01-27 04:57:48 +00:00
return '%s/inbox' % (user.actor)