moviewyrm/bookwyrm/activitypub/verbs.py

169 lines
3.8 KiB
Python
Raw Normal View History

''' 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):
''' 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):
''' usually we just want to save, this can be overridden as needed '''
self.object.to_model()
@dataclass(init=False)
class Create(Verb):
''' Create activity '''
to: List
cc: List
2021-02-02 17:37:46 +00:00
signature: Signature = None
type: str = 'Create'
2020-10-14 15:38:51 +00:00
@dataclass(init=False)
class Delete(Verb):
''' Create activity '''
to: List
cc: List
type: str = 'Delete'
2021-02-16 04:49:23 +00:00
def action(self):
''' find and delete the activity object '''
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()
2020-10-14 15:38:51 +00:00
@dataclass(init=False)
class Update(Verb):
''' Update activity '''
to: List
type: str = 'Update'
2021-02-16 04:49:23 +00:00
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'
2021-02-16 04:49:23 +00:00
def action(self):
''' 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
if self.object.type == 'Follow':
model = apps.get_model('bookwyrm.UserFollows')
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):
''' Follow activity '''
2021-02-16 02:47:08 +00:00
object: str
type: str = 'Follow'
2021-02-16 05:20:00 +00:00
def action(self):
''' relationship save '''
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):
''' Block activity '''
2021-02-16 02:47:08 +00:00
object: str
2021-01-23 19:03:10 +00:00
type: str = 'Block'
2021-02-16 05:20:00 +00:00
def action(self):
''' relationship save '''
self.to_model()
2021-02-16 04:49:23 +00:00
@dataclass(init=False)
class Accept(Verb):
''' Accept activity '''
object: Follow
type: str = 'Accept'
2021-02-16 04:49:23 +00:00
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):
''' Reject activity '''
object: Follow
type: str = 'Reject'
2021-02-16 04:49:23 +00:00
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):
'''Add activity '''
target: str
2021-02-16 19:04:13 +00:00
object: Edition
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):
''' 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
model = [t for t in type(target)._meta.related_objects \
if t.name != 'edition'][0].related_model
self.to_model(model=model)
2021-02-16 05:20:00 +00:00
@dataclass(init=False)
class Remove(Verb):
'''Remove activity '''
target: ActivityObject
type: str = 'Remove'
2021-02-16 04:49:23 +00:00
def action(self):
''' find and remove the activity object '''
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):
''' a user faving an object '''
object: str
type: str = 'Like'
def action(self):
''' like '''
self.to_model()
@dataclass(init=False)
class Announce(Verb):
''' boosting a status '''
object: str
type: str = 'Announce'
def action(self):
''' boost '''
self.to_model()