From 088ed350408bd75f36b430959d1ff37237c648cc Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Thu, 29 Oct 2020 00:52:34 +0000 Subject: [PATCH] CreateActivitySerializer mostly working; to and cc are constants, which they shouldn't be. Test now passes. --- kepi/bowler_pub/serializers.py | 81 +++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/kepi/bowler_pub/serializers.py b/kepi/bowler_pub/serializers.py index 8bec1b7..c016c6a 100644 --- a/kepi/bowler_pub/serializers.py +++ b/kepi/bowler_pub/serializers.py @@ -17,6 +17,8 @@ from rest_framework_constant.fields import ConstantField from kepi.bowler_pub.utils import uri_to_url from django.conf import settings +PUBLIC = "https://www.w3.org/ns/activitystreams#Public" + ######################################### class StatusObjectSerializer(serializers.ModelSerializer): @@ -41,35 +43,62 @@ class StatusObjectSerializer(serializers.ModelSerializer): ) class CreateActivitySerializer(serializers.ModelSerializer): + class Meta: model = trilby_models.Status - fields = ( - 'type', - 'actor', - 'published', - 'to', - 'cc', - 'object', - ) - type = ConstantField( - value="Create", - ) - actor = serializers.CharField( - source = "account", - ) - published = serializers.DateTimeField( - source = "created_at", - ) - to = ConstantField( - value="FIXME", # FIXME - ) - cc = ConstantField( - value="FIXME", # FIXME - ) - object = ConstantField( - value="FIXME", # FIXME - ) + def _object_field_for_create(self, status, + to, cc, replies): + return { + 'id': status.url, + 'url': status.url, + 'type': 'Note', + 'summary': status.spoiler_text, + 'inReplyTo': status.in_reply_to, + 'published': status.created_at, + 'attributedTo': status.account.url, + 'to': to, + 'cc': cc, + 'sensitive': status.sensitive, + 'conversation': status.conversation, + 'content': status.content, + 'contentMap': { + status.language: status.content, + }, + 'attachment': status.media_attachments, + 'tag': status.tags, + 'replies': replies, + } + + def _object_field_for_reblog(self, status): + return status.reblog_of.url + + def to_representation(self, status): + + to = [PUBLIC] # FIXME + cc = [] # FIXME + + replies = [ + self.to_representation(x) + for x in status.replies.all()] + + if status.reblog_of is None: + type_field = 'Create' + object_field = self._object_field_for_create(status, + to=to, cc=cc, replies=replies) + else: + type_field = 'Announce' + object_field = self._object_field_for_reblog(status) + + return { + 'id': status.activity_url, + 'type': type_field, + 'actor': status.account.url, + 'published': status.created_at, + 'to': to, + 'cc': cc, + 'object': object_field, + } class ImageField(serializers.Field):