Uses activitypub mixin in relationship models

plus tests
This commit is contained in:
Mouse Reeve
2020-10-16 10:37:33 -07:00
parent e8ef8f7101
commit 2a0af0138d
4 changed files with 167 additions and 22 deletions

View File

@ -2,10 +2,10 @@
from django.db import models
from bookwyrm import activitypub
from .base_model import BookWyrmModel
from .base_model import ActivitypubMixin, ActivityMapping, BookWyrmModel
class UserRelationship(BookWyrmModel):
class UserRelationship(ActivitypubMixin, BookWyrmModel):
''' many-to-many through table for followers '''
user_subject = models.ForeignKey(
'User',
@ -17,8 +17,6 @@ class UserRelationship(BookWyrmModel):
on_delete=models.PROTECT,
related_name='%(class)s_user_object'
)
# follow or follow_request for pending TODO: blocking?
relationship_id = models.CharField(max_length=100)
class Meta:
''' relationships should be unique '''
@ -34,25 +32,35 @@ class UserRelationship(BookWyrmModel):
)
]
def get_remote_id(self):
activity_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('actor', 'user_subject'),
ActivityMapping('object', 'user_object'),
]
activity_serializer = activitypub.Follow
def get_remote_id(self, status=None):
''' use shelf identifier in remote_id '''
status = status or 'follows'
base_path = self.user_subject.remote_id
return '%s#%s/%d' % (base_path, self.status, self.id)
return '%s#%s/%d' % (base_path, status, self.id)
def to_accept_activity(self):
''' generate an Accept for this follow request '''
return activitypub.Accept(
id='%s#accepts/follows/' % self.remote_id,
actor=self.user_subject.remote_id,
object=self.user_object.remote_id,
id=self.get_remote_id(status='accepts'),
actor=self.user_object.remote_id,
object=self.to_activity()
).serialize()
def to_reject_activity(self):
''' generate an Accept for this follow request '''
return activitypub.Reject(
id='%s#rejects/follows/' % self.remote_id,
actor=self.user_subject.remote_id,
object=self.user_object.remote_id,
id=self.get_remote_id(status='rejects'),
actor=self.user_object.remote_id,
object=self.to_activity()
).serialize()
@ -66,7 +74,7 @@ class UserFollows(UserRelationship):
return cls(
user_subject=follow_request.user_subject,
user_object=follow_request.user_object,
relationship_id=follow_request.relationship_id,
remote_id=follow_request.remote_id,
)
@ -74,14 +82,6 @@ class UserFollowRequest(UserRelationship):
''' following a user requires manual or automatic confirmation '''
status = 'follow_request'
def to_activity(self):
''' request activity '''
return activitypub.Follow(
id=self.remote_id,
actor=self.user_subject.remote_id,
object=self.user_object.remote_id,
).serialize()
class UserBlocks(UserRelationship):
''' prevent another user from following you and seeing your posts '''