bookwyrm/bookwyrm/activitypub/verbs.py

182 lines
3.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" undo wrapper activity """
from dataclasses import dataclass
from typing import List
2021-02-17 00:35:28 +00:00
from django.apps import apps
2021-02-16 19:04:13 +00:00
from .base_activity import ActivityObject, Signature, resolve_remote_id
2020-12-16 02:57:17 +00:00
from .book import Edition
2021-02-16 05:41:08 +00:00
@dataclass(init=False)
class Verb(ActivityObject):
2021-03-08 16:49:10 +00:00
"""generic fields for activities - maybe an unecessary level of
abstraction but w/e"""
actor: str
object: ActivityObject
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" usually we just want to save, this can be overridden as needed """
2021-02-16 04:49:23 +00:00
self.object.to_model()
@dataclass(init=False)
class Create(Verb):
2021-03-08 16:49:10 +00:00
""" Create activity """
to: List
cc: List
2021-02-02 17:37:46 +00:00
signature: Signature = None
2021-03-08 16:49:10 +00:00
type: str = "Create"
2020-10-14 15:38:51 +00:00
@dataclass(init=False)
class Delete(Verb):
2021-03-08 16:49:10 +00:00
""" Create activity """
2020-10-14 15:38:51 +00:00
to: List
cc: List
2021-03-08 16:49:10 +00:00
type: str = "Delete"
2020-10-14 15:38:51 +00:00
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" find and delete the activity object """
2021-02-16 04:49:23 +00:00
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
@dataclass(init=False)
class Update(Verb):
2021-03-08 16:49:10 +00:00
""" Update activity """
to: List
2021-03-08 16:49:10 +00:00
type: str = "Update"
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" update a model instance from the dataclass """
2021-02-16 04:49:23 +00:00
self.object.to_model(allow_create=False)
@dataclass(init=False)
class Undo(Verb):
2021-03-08 16:49:10 +00:00
""" Undo an activity """
type: str = "Undo"
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" find and remove the activity object """
2021-02-17 00:35:28 +00: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 16:49:10 +00:00
if self.object.type == "Follow":
model = apps.get_model("bookwyrm.UserFollows")
2021-02-17 00:35:28 +00:00
obj = self.object.to_model(model=model, save=False, allow_create=False)
2021-02-16 04:49:23 +00:00
obj.delete()
@dataclass(init=False)
class Follow(Verb):
2021-03-08 16:49:10 +00:00
""" Follow activity """
2021-02-16 02:47:08 +00:00
object: str
2021-03-08 16:49:10 +00:00
type: str = "Follow"
2021-02-16 05:20:00 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" relationship save """
2021-02-16 05:20:00 +00:00
self.to_model()
2021-02-16 04:49:23 +00:00
2021-01-23 19:03:10 +00:00
@dataclass(init=False)
class Block(Verb):
2021-03-08 16:49:10 +00:00
""" Block activity """
2021-02-16 02:47:08 +00:00
object: str
2021-03-08 16:49:10 +00:00
type: str = "Block"
2021-02-16 05:20:00 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" relationship save """
2021-02-16 05:20:00 +00:00
self.to_model()
2021-02-16 04:49:23 +00:00
@dataclass(init=False)
class Accept(Verb):
2021-03-08 16:49:10 +00:00
""" Accept activity """
object: Follow
2021-03-08 16:49:10 +00:00
type: str = "Accept"
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" find and remove the activity object """
2021-02-16 04:49:23 +00:00
obj = self.object.to_model(save=False, allow_create=False)
obj.accept()
@dataclass(init=False)
class Reject(Verb):
2021-03-08 16:49:10 +00:00
""" Reject activity """
object: Follow
2021-03-08 16:49:10 +00:00
type: str = "Reject"
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" find and remove the activity object """
2021-02-16 04:49:23 +00:00
obj = self.object.to_model(save=False, allow_create=False)
obj.reject()
@dataclass(init=False)
class Add(Verb):
2021-03-08 16:49:10 +00:00
"""Add activity """
target: str
2021-02-16 19:04:13 +00:00
object: Edition
2021-03-08 16:49:10 +00:00
type: str = "Add"
2021-02-23 23:51:02 +00:00
notes: str = None
order: int = 0
approved: bool = True
2021-02-16 05:20:00 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" add obj to collection """
2021-02-16 19:04:13 +00:00
target = resolve_remote_id(self.target, refresh=False)
# we want to related field that isn't the book, this is janky af sorry
2021-03-08 16:49:10 +00:00
model = [t for t in type(target)._meta.related_objects if t.name != "edition"][
0
].related_model
2021-02-16 19:04:13 +00:00
self.to_model(model=model)
2021-02-16 05:20:00 +00:00
@dataclass(init=False)
class Remove(Verb):
2021-03-08 16:49:10 +00:00
"""Remove activity """
target: ActivityObject
2021-03-08 16:49:10 +00:00
type: str = "Remove"
2021-02-16 04:49:23 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" find and remove the activity object """
2021-02-16 04:49:23 +00:00
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
2021-02-16 05:20:00 +00:00
@dataclass(init=False)
class Like(Verb):
2021-03-08 16:49:10 +00:00
""" a user faving an object """
2021-02-16 05:20:00 +00:00
object: str
2021-03-08 16:49:10 +00:00
type: str = "Like"
2021-02-16 05:20:00 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" like """
2021-02-16 05:20:00 +00:00
self.to_model()
@dataclass(init=False)
class Announce(Verb):
2021-03-08 16:49:10 +00:00
""" boosting a status """
2021-02-16 05:20:00 +00:00
object: str
2021-03-08 16:49:10 +00:00
type: str = "Announce"
2021-02-16 05:20:00 +00:00
def action(self):
2021-03-08 16:49:10 +00:00
""" boost """
2021-02-16 05:20:00 +00:00
self.to_model()