bookwyrm/bookwyrm/models/move.py

73 lines
2 KiB
Python
Raw Normal View History

2023-08-29 11:07:41 +00:00
""" move an object including migrating a user account """
from django.core.exceptions import PermissionDenied
2023-08-29 11:07:41 +00:00
from django.db import models
from bookwyrm import activitypub
from .activitypub_mixin import ActivityMixin
from .base_model import BookWyrmModel
from . import fields
from .notification import Notification
2023-08-29 11:07:41 +00:00
2023-09-25 05:49:25 +00:00
2023-08-29 11:07:41 +00:00
class Move(ActivityMixin, BookWyrmModel):
"""migrating an activitypub user account"""
user = fields.ForeignKey(
"User", on_delete=models.PROTECT, activitypub_field="actor"
)
object = fields.CharField(
max_length=255,
blank=False,
null=False,
2023-08-29 11:07:41 +00:00
activitypub_field="object",
)
origin = fields.CharField(
max_length=255,
blank=True,
null=True,
default="",
activitypub_field="origin",
2023-08-29 11:07:41 +00:00
)
activity_serializer = activitypub.Move
2023-09-25 05:49:25 +00:00
class MoveUser(Move):
"""migrating an activitypub user account"""
target = fields.ForeignKey(
"User",
on_delete=models.PROTECT,
related_name="move_target",
activitypub_field="target",
)
2023-08-29 11:07:41 +00:00
def save(self, *args, **kwargs):
"""update user info and broadcast it"""
2023-08-29 11:07:41 +00:00
# only allow if the source is listed in the target's alsoKnownAs
if self.user in self.target.also_known_as.all():
self.user.also_known_as.add(self.target.id)
self.user.update_active_date()
self.user.moved_to = self.target.remote_id
self.user.save(update_fields=["moved_to"])
if self.user.local:
kwargs[
"broadcast"
] = True # Only broadcast if we are initiating the Move
super().save(*args, **kwargs)
for follower in self.user.followers.all():
if follower.local:
Notification.notify(
follower, self.user, notification_type=Notification.MOVE
)
else:
raise PermissionDenied()