bookwyrm/bookwyrm/models/move.py

69 lines
1.9 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, NotificationType
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 object"""
2023-08-29 11:07:41 +00:00
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
2024-04-25 13:51:32 +00:00
if self.user not in self.target.also_known_as.all():
raise PermissionDenied()
2024-04-25 13:51:32 +00:00
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=NotificationType.MOVE
)