Adds actions for all verbs

This commit is contained in:
Mouse Reeve
2021-02-15 20:49:23 -08:00
parent 12a3aa9667
commit a16b81a6eb
7 changed files with 507 additions and 506 deletions

View File

@ -50,6 +50,7 @@ def naive_parse(activity_objects, activity_json):
return serializer(activity_objects=activity_objects, **activity_json)
@dataclass(init=False)
class ActivityObject:
''' actor activitypub json '''
@ -79,7 +80,7 @@ class ActivityObject:
setattr(self, field.name, value)
def to_model(self, instance=None, save=True):
def to_model(self, instance=None, allow_create=True, save=True):
''' convert from an activity to a model instance '''
# figure out the right model -- wish I had a better way for this
models = apps.get_models()
@ -95,7 +96,10 @@ class ActivityObject:
return instance
# check for an existing instance, if we're not updating a known obj
instance = instance or model.find_existing(self.serialize()) or model()
instance = instance or model.find_existing(self.serialize())
if not instance and not allow_create:
return None
instance = instance or model()
for field in instance.simple_fields:
field.set_field_from_activity(instance, self)

View File

@ -12,6 +12,10 @@ class Verb(ActivityObject):
actor: str
object: ActivityObject
def action(self):
''' usually we just want to save, this can be overridden as needed '''
self.object.to_model()
@dataclass(init=False)
class Create(Verb):
@ -21,10 +25,6 @@ class Create(Verb):
signature: Signature = None
type: str = 'Create'
def action(self):
''' create the model instance from the dataclass '''
self.object.to_model()
@dataclass(init=False)
class Delete(Verb):
@ -33,6 +33,12 @@ class Delete(Verb):
cc: List
type: str = 'Delete'
def action(self):
''' find and delete the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
@dataclass(init=False)
class Update(Verb):
@ -40,12 +46,21 @@ class Update(Verb):
to: List
type: str = 'Update'
def action(self):
''' update a model instance from the dataclass '''
self.object.to_model(allow_create=False)
@dataclass(init=False)
class Undo(Verb):
''' Undo an activity '''
type: str = 'Undo'
def action(self):
''' find and remove the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
@dataclass(init=False)
class Follow(Verb):
@ -53,18 +68,25 @@ class Follow(Verb):
object: str
type: str = 'Follow'
@dataclass(init=False)
class Block(Verb):
''' Block activity '''
object: str
type: str = 'Block'
@dataclass(init=False)
class Accept(Verb):
''' Accept activity '''
object: Follow
type: str = 'Accept'
def action(self):
''' find and remove the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.accept()
@dataclass(init=False)
class Reject(Verb):
@ -72,6 +94,11 @@ class Reject(Verb):
object: Follow
type: str = 'Reject'
def action(self):
''' find and remove the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.reject()
@dataclass(init=False)
class Add(Verb):
@ -101,3 +128,8 @@ class Remove(Verb):
'''Remove activity '''
target: ActivityObject
type: str = 'Remove'
def action(self):
''' find and remove the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()