Move activitypub serialization into a module
This commit is contained in:
parent
b6964dd8aa
commit
75ef3baabd
9 changed files with 206 additions and 192 deletions
5
fedireads/activitypub/__init__.py
Normal file
5
fedireads/activitypub/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
''' bring activitypub functions into the namespace '''
|
||||
from .actor import get_actor
|
||||
from .collection import get_add, get_remove
|
||||
from .create import get_create
|
||||
from .status import get_review, get_status
|
28
fedireads/activitypub/actor.py
Normal file
28
fedireads/activitypub/actor.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
''' actor serializer '''
|
||||
|
||||
def get_actor(user):
|
||||
''' activitypub actor from db User '''
|
||||
return {
|
||||
'@context': [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
'https://w3id.org/security/v1'
|
||||
],
|
||||
|
||||
'id': user.actor,
|
||||
'type': 'Person',
|
||||
'preferredUsername': user.localname,
|
||||
'name': user.name,
|
||||
'inbox': user.inbox,
|
||||
'followers': '%s/followers' % user.actor,
|
||||
'following': '%s/following' % user.actor,
|
||||
'summary': user.summary,
|
||||
'publicKey': {
|
||||
'id': '%s/#main-key' % user.actor,
|
||||
'owner': user.actor,
|
||||
'publicKeyPem': user.public_key,
|
||||
},
|
||||
'endpoints': {
|
||||
'sharedInbox': user.shared_inbox,
|
||||
}
|
||||
}
|
||||
|
34
fedireads/activitypub/collection.py
Normal file
34
fedireads/activitypub/collection.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
''' activitypub json for collections '''
|
||||
from uuid import uuid4
|
||||
|
||||
def get_add(*args):
|
||||
''' activitypub Add activity '''
|
||||
return get_add_remove(*args, action='Add')
|
||||
|
||||
|
||||
def get_remove(*args):
|
||||
''' activitypub Add activity '''
|
||||
return get_add_remove(*args, action='Remove')
|
||||
|
||||
|
||||
def get_add_remove(user, book, shelf, action='Add'):
|
||||
''' format an Add or Remove json blob '''
|
||||
uuid = uuid4()
|
||||
return {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': str(uuid),
|
||||
'type': action,
|
||||
'actor': user.actor,
|
||||
'object': {
|
||||
'type': 'Document',
|
||||
'name': book.data['title'],
|
||||
'url': book.openlibrary_key
|
||||
},
|
||||
'target': {
|
||||
'type': 'Collection',
|
||||
'name': shelf.name,
|
||||
'id': shelf.absolute_id,
|
||||
}
|
||||
}
|
||||
|
||||
|
33
fedireads/activitypub/create.py
Normal file
33
fedireads/activitypub/create.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
''' format Create activities and sign them '''
|
||||
from base64 import b64encode
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Signature import pkcs1_15
|
||||
from Crypto.Hash import SHA256
|
||||
|
||||
def get_create(user, status_json):
|
||||
''' create activitypub json for a Create activity '''
|
||||
signer = pkcs1_15.new(RSA.import_key(user.private_key))
|
||||
content = status_json['content']
|
||||
signed_message = signer.sign(SHA256.new(content.encode('utf8')))
|
||||
return {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
|
||||
'id': '%s/activity' % status_json['id'],
|
||||
'type': 'Create',
|
||||
'actor': user.actor,
|
||||
'published': status_json['published'],
|
||||
|
||||
'to': ['%s/followers' % user.actor],
|
||||
'cc': ['https://www.w3.org/ns/activitystreams#Public'],
|
||||
|
||||
'object': status_json,
|
||||
'signature': {
|
||||
'type': 'RsaSignature2017',
|
||||
'creator': '%s#main-key' % user.absolute_id,
|
||||
'created': status_json['published'],
|
||||
'signatureValue': b64encode(signed_message).decode('utf8'),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
45
fedireads/activitypub/status.py
Normal file
45
fedireads/activitypub/status.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
''' status serializers '''
|
||||
def get_review(review):
|
||||
''' fedireads json for book reviews '''
|
||||
status = get_status_json(review)
|
||||
status['inReplyTo'] = review.book.absolute_id
|
||||
status['fedireadsType'] = review.status_type,
|
||||
status['name'] = review.name
|
||||
status['rating'] = review.rating
|
||||
return status
|
||||
|
||||
|
||||
def get_status(status):
|
||||
''' create activitypub json for a status '''
|
||||
user = status.user
|
||||
uri = status.absolute_id
|
||||
reply_parent_id = status.reply_parent.id if status.reply_parent else None
|
||||
status_json = {
|
||||
'id': uri,
|
||||
'url': uri,
|
||||
'inReplyTo': reply_parent_id,
|
||||
'published': status.created_date.isoformat(),
|
||||
'attributedTo': user.actor,
|
||||
# TODO: assuming all posts are public -- should check privacy db field
|
||||
'to': ['https://www.w3.org/ns/activitystreams#Public'],
|
||||
'cc': ['%s/followers' % user.absolute_id],
|
||||
'sensitive': status.sensitive,
|
||||
'content': status.content,
|
||||
'type': status.activity_type,
|
||||
'attachment': [], # TODO: the book cover
|
||||
'replies': {
|
||||
'id': '%s/replies' % uri,
|
||||
'type': 'Collection',
|
||||
'first': {
|
||||
'type': 'CollectionPage',
|
||||
'next': '%s/replies?only_other_accounts=true&page=true' % uri,
|
||||
'partOf': '%s/replies' % uri,
|
||||
'items': [], # TODO: populate with replies
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status_json
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue