-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3076 from bookwyrm-social/move
Add Move activity for user migration (with small change)
- Loading branch information
Showing
30 changed files
with
788 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Generated by Django 3.2.20 on 2023-10-27 11:22 | ||
|
||
import bookwyrm.models.activitypub_mixin | ||
import bookwyrm.models.fields | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("bookwyrm", "0181_merge_20230806_2302"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="also_known_as", | ||
field=bookwyrm.models.fields.ManyToManyField(to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="moved_to", | ||
field=bookwyrm.models.fields.RemoteIdField( | ||
max_length=255, | ||
null=True, | ||
validators=[bookwyrm.models.fields.validate_remote_id], | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="notification", | ||
name="notification_type", | ||
field=models.CharField( | ||
choices=[ | ||
("FAVORITE", "Favorite"), | ||
("REPLY", "Reply"), | ||
("MENTION", "Mention"), | ||
("TAG", "Tag"), | ||
("FOLLOW", "Follow"), | ||
("FOLLOW_REQUEST", "Follow Request"), | ||
("BOOST", "Boost"), | ||
("IMPORT", "Import"), | ||
("ADD", "Add"), | ||
("REPORT", "Report"), | ||
("LINK_DOMAIN", "Link Domain"), | ||
("INVITE", "Invite"), | ||
("ACCEPT", "Accept"), | ||
("JOIN", "Join"), | ||
("LEAVE", "Leave"), | ||
("REMOVE", "Remove"), | ||
("GROUP_PRIVACY", "Group Privacy"), | ||
("GROUP_NAME", "Group Name"), | ||
("GROUP_DESCRIPTION", "Group Description"), | ||
("MOVE", "Move"), | ||
], | ||
max_length=255, | ||
), | ||
), | ||
migrations.CreateModel( | ||
name="Move", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_date", models.DateTimeField(auto_now_add=True)), | ||
("updated_date", models.DateTimeField(auto_now=True)), | ||
( | ||
"remote_id", | ||
bookwyrm.models.fields.RemoteIdField( | ||
max_length=255, | ||
null=True, | ||
validators=[bookwyrm.models.fields.validate_remote_id], | ||
), | ||
), | ||
("object", bookwyrm.models.fields.CharField(max_length=255)), | ||
( | ||
"origin", | ||
bookwyrm.models.fields.CharField( | ||
blank=True, default="", max_length=255, null=True | ||
), | ||
), | ||
( | ||
"user", | ||
bookwyrm.models.fields.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
bases=(bookwyrm.models.activitypub_mixin.ActivityMixin, models.Model), | ||
), | ||
migrations.CreateModel( | ||
name="MoveUser", | ||
fields=[ | ||
( | ||
"move_ptr", | ||
models.OneToOneField( | ||
auto_created=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
parent_link=True, | ||
primary_key=True, | ||
serialize=False, | ||
to="bookwyrm.move", | ||
), | ||
), | ||
( | ||
"target", | ||
bookwyrm.models.fields.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="move_target", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
bases=("bookwyrm.move",), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" move an object including migrating a user account """ | ||
from django.core.exceptions import PermissionDenied | ||
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 | ||
|
||
|
||
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, | ||
activitypub_field="object", | ||
) | ||
|
||
origin = fields.CharField( | ||
max_length=255, | ||
blank=True, | ||
null=True, | ||
default="", | ||
activitypub_field="origin", | ||
) | ||
|
||
activity_serializer = activitypub.Move | ||
|
||
|
||
class MoveUser(Move): | ||
"""migrating an activitypub user account""" | ||
|
||
target = fields.ForeignKey( | ||
"User", | ||
on_delete=models.PROTECT, | ||
related_name="move_target", | ||
activitypub_field="target", | ||
) | ||
|
||
def save(self, *args, **kwargs): | ||
"""update user info and broadcast it""" | ||
|
||
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.