bookwyrm-mastodon/bookwyrm/activitypub/verbs.py

192 lines
4.5 KiB
Python
Raw Normal View History

""" activities that do things """
from dataclasses import dataclass, field
from typing import List
2021-02-16 19:35:28 -05:00
from django.apps import apps
2021-02-16 14:04:13 -05:00
from .base_activity import ActivityObject, Signature, resolve_remote_id
2021-04-08 17:16:34 -04:00
from .ordered_collection import CollectionItem
2021-02-16 00:41:08 -05:00
@dataclass(init=False)
class Verb(ActivityObject):
"""generic fields for activities """
2021-03-08 11:49:10 -05:00
actor: str
object: ActivityObject
2021-02-15 23:49:23 -05:00
def action(self):
""" usually we just want to update and save """
2021-02-15 23:49:23 -05:00
self.object.to_model()
@dataclass(init=False)
class Create(Verb):
2021-03-08 11:49:10 -05:00
""" Create activity """
to: List[str]
cc: List[str] = field(default_factory=lambda: [])
2021-02-02 12:37:46 -05:00
signature: Signature = None
2021-03-08 11:49:10 -05:00
type: str = "Create"
2020-10-14 11:38:51 -04:00
@dataclass(init=False)
class Delete(Verb):
2021-03-08 11:49:10 -05:00
""" Create activity """
to: List[str]
cc: List[str] = field(default_factory=lambda: [])
2021-03-08 11:49:10 -05:00
type: str = "Delete"
2020-10-14 11:38:51 -04:00
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" find and delete the activity object """
2021-02-15 23:49:23 -05:00
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
@dataclass(init=False)
class Update(Verb):
2021-03-08 11:49:10 -05:00
""" Update activity """
to: List[str]
2021-03-08 11:49:10 -05:00
type: str = "Update"
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" update a model instance from the dataclass """
2021-02-15 23:49:23 -05:00
self.object.to_model(allow_create=False)
@dataclass(init=False)
class Undo(Verb):
2021-03-08 11:49:10 -05:00
""" Undo an activity """
type: str = "Undo"
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" find and remove the activity object """
if isinstance(self.object, str):
# it may be that sometihng should be done with these, but idk what
# this seems just to be coming from pleroma
return
2021-02-16 19:35:28 -05:00
# this is so hacky but it does make it work....
# (because you Reject a request and Undo a follow
model = None
2021-03-08 11:49:10 -05:00
if self.object.type == "Follow":
model = apps.get_model("bookwyrm.UserFollows")
obj = self.object.to_model(model=model, save=False, allow_create=False)
if not obj:
# this could be a folloq request not a follow proper
model = apps.get_model("bookwyrm.UserFollowRequest")
obj = self.object.to_model(model=model, save=False, allow_create=False)
else:
obj = self.object.to_model(model=model, save=False, allow_create=False)
if not obj:
# if we don't have the object, we can't undo it. happens a lot with boosts
return
2021-02-15 23:49:23 -05:00
obj.delete()
@dataclass(init=False)
class Follow(Verb):
2021-03-08 11:49:10 -05:00
""" Follow activity """
2021-02-15 21:47:08 -05:00
object: str
2021-03-08 11:49:10 -05:00
type: str = "Follow"
2021-02-16 00:20:00 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" relationship save """
2021-02-16 00:20:00 -05:00
self.to_model()
2021-02-15 23:49:23 -05:00
2021-01-23 14:03:10 -05:00
@dataclass(init=False)
class Block(Verb):
2021-03-08 11:49:10 -05:00
""" Block activity """
2021-02-15 21:47:08 -05:00
object: str
2021-03-08 11:49:10 -05:00
type: str = "Block"
2021-02-16 00:20:00 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" relationship save """
2021-02-16 00:20:00 -05:00
self.to_model()
2021-02-15 23:49:23 -05:00
@dataclass(init=False)
class Accept(Verb):
2021-03-08 11:49:10 -05:00
""" Accept activity """
object: Follow
2021-03-08 11:49:10 -05:00
type: str = "Accept"
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" find and remove the activity object """
2021-02-15 23:49:23 -05:00
obj = self.object.to_model(save=False, allow_create=False)
obj.accept()
@dataclass(init=False)
class Reject(Verb):
2021-03-08 11:49:10 -05:00
""" Reject activity """
object: Follow
2021-03-08 11:49:10 -05:00
type: str = "Reject"
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" find and remove the activity object """
2021-02-15 23:49:23 -05:00
obj = self.object.to_model(save=False, allow_create=False)
obj.reject()
@dataclass(init=False)
class Add(Verb):
2021-03-08 11:49:10 -05:00
"""Add activity """
2021-04-08 17:16:34 -04:00
target: ActivityObject
object: CollectionItem
2021-03-08 11:49:10 -05:00
type: str = "Add"
def action(self):
""" figure out the target to assign the item to a collection """
target = resolve_remote_id(self.target)
item = self.object.to_model(save=False)
setattr(item, item.collection_field, target)
item.save()
@dataclass(init=False)
2021-04-08 17:16:34 -04:00
class Remove(Add):
2021-03-08 11:49:10 -05:00
"""Remove activity """
type: str = "Remove"
2021-02-15 23:49:23 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" find and remove the activity object """
obj = self.object.to_model(save=False, allow_create=False)
2021-02-15 23:49:23 -05:00
obj.delete()
2021-02-16 00:20:00 -05:00
@dataclass(init=False)
class Like(Verb):
2021-03-08 11:49:10 -05:00
""" a user faving an object """
2021-02-16 00:20:00 -05:00
object: str
2021-03-08 11:49:10 -05:00
type: str = "Like"
2021-02-16 00:20:00 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" like """
2021-02-16 00:20:00 -05:00
self.to_model()
@dataclass(init=False)
class Announce(Verb):
2021-03-08 11:49:10 -05:00
""" boosting a status """
published: str
to: List[str] = field(default_factory=lambda: [])
cc: List[str] = field(default_factory=lambda: [])
2021-02-16 00:20:00 -05:00
object: str
2021-03-08 11:49:10 -05:00
type: str = "Announce"
2021-02-16 00:20:00 -05:00
def action(self):
2021-03-08 11:49:10 -05:00
""" boost """
2021-02-16 00:20:00 -05:00
self.to_model()