diff --git a/activitypub/activities/objects.py b/activitypub/activities/objects.py index 78cc9d5..3b43f27 100644 --- a/activitypub/activities/objects.py +++ b/activitypub/activities/objects.py @@ -15,11 +15,11 @@ class Object(object): for key in self.attributes: if key == "type": continue - + value = kwargs.get(key) if value is None: continue - + if isinstance(value, dict) and value.get("type"): value = as_activitystream(value) self.__setattr__(key, value) @@ -167,14 +167,13 @@ def as_activitystream(obj): if not type: msg = "Invalid ActivityStream object, the type is missing" raise errors.ASDecodeError(msg) - + if type in ALLOWED_TYPES: return ALLOWED_TYPES[type](**obj) - + raise errors.ASDecodeError("Invalid Type {0}".format(type)) def encode_activitystream(obj): if isinstance(obj, Object): return obj.to_json() raise errors.ASTypeError("Unknown ActivityStream Type") - diff --git a/activitypub/activities/verbs.py b/activitypub/activities/verbs.py index 6359b58..a0d2de0 100644 --- a/activitypub/activities/verbs.py +++ b/activitypub/activities/verbs.py @@ -15,7 +15,7 @@ class Activity(Object): value = getattr(self, attr, None) if not value: continue - + if isinstance(value, str): value = [value] audience += value diff --git a/activitypub/models.py b/activitypub/models.py index d448873..ea2b9a6 100644 --- a/activitypub/models.py +++ b/activitypub/models.py @@ -20,7 +20,7 @@ class URIs(object): class Person(Model): ap_id = TextField(null=True) remote = BooleanField(default=False) - + username = CharField(max_length=100) name = CharField(max_length=100) following = ManyToManyField('self', symmetrical=False, related_name='followers') @@ -29,7 +29,7 @@ class Person(Model): def uris(self): if self.remote: return URIs(id=self.ap_id) - + return URIs( id=uri("person", self.username), following=uri("following", self.username), @@ -61,7 +61,7 @@ class Note(Model): person = ForeignKey(Person, related_name='notes') content = CharField(max_length=500) likes = ManyToManyField(Person, related_name='liked') - + @property def uris(self): if self.remote: @@ -69,7 +69,7 @@ class Note(Model): else: ap_id = uri("note", self.person.username, self.id) return URIs(id=ap_id) - + def to_activitystream(self): return { "id": self.uris.id, @@ -83,4 +83,3 @@ def save_ap_id(sender, instance, created, **kwargs): if created and not instance.remote: instance.ap_id = instance.uris.id instance.save() - diff --git a/activitypub/views.py b/activitypub/views.py index 93cdf13..f12c0ae 100644 --- a/activitypub/views.py +++ b/activitypub/views.py @@ -35,9 +35,9 @@ def outbox(request, username): actor=person.uris.id, object=obj ) - + activity.validate() - + if activity.type == "Create": if activity.object.type != "Note": raise Exception("Sorry, you can only create Notes objects") @@ -156,4 +156,3 @@ def followers(request, username): actor = activities.Person(id="http://bob.local/@bob",name="Bob") followers.items.append(actor) return JsonResponse(followers.to_json(context=True)) -