2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2020-02-08 18:38:45 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import migrations
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2020-02-08 18:38:45 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
|
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def fix_messages(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2020-02-08 18:38:45 +01:00
|
|
|
"""Conceptually, this migration cleans up the old NEW_USER_BOT and FEEDBACK_BOT
|
|
|
|
UserProfile objects (their implementations were removed long ago).
|
|
|
|
|
|
|
|
We do this by:
|
|
|
|
* Changing their sent messages to have been sent by NOTIFICATION_BOT.
|
|
|
|
* Changing their 1:1 PMs to be PMs with NOTIFICATION_BOT and deleting their
|
|
|
|
PM recipient object.
|
2020-06-09 00:58:42 +02:00
|
|
|
* Deleting any Huddles that involve them (zulip.com data suggests there are none,
|
2020-02-08 18:38:45 +01:00
|
|
|
so this is mainly out of caution) and Recipient objects (which will cascade to
|
|
|
|
associated Subscription, Message, and UserMessage objects if they exist).
|
|
|
|
* Deleting their UserProfile objects.
|
|
|
|
|
|
|
|
The end result if these users are completely removed, with any
|
|
|
|
messages that might have been associated with them transferred
|
|
|
|
to NOTIFICATION_BOT to preserve history.
|
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
UserProfile = apps.get_model("zerver", "UserProfile")
|
|
|
|
Huddle = apps.get_model("zerver", "Huddle")
|
|
|
|
Subscription = apps.get_model("zerver", "Subscription")
|
|
|
|
Recipient = apps.get_model("zerver", "Recipient")
|
2020-02-08 18:38:45 +01:00
|
|
|
RECIPIENT_HUDDLE = 3
|
2021-02-12 08:20:45 +01:00
|
|
|
Message = apps.get_model("zerver", "Message")
|
|
|
|
Realm = apps.get_model("zerver", "Realm")
|
2020-02-08 18:38:45 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
internal_realm = Realm.objects.get(string_id=settings.SYSTEM_BOT_REALM)
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
# Server not initialized, or no system bot realm. Either way, we shouldn't do anything.
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_bot_by_delivery_email(email: str) -> Any:
|
2021-02-12 08:19:30 +01:00
|
|
|
return UserProfile.objects.select_related().get(
|
|
|
|
delivery_email__iexact=email.strip(), realm=internal_realm
|
|
|
|
)
|
2020-02-08 18:38:45 +01:00
|
|
|
|
|
|
|
notification_bot = get_bot_by_delivery_email(settings.NOTIFICATION_BOT)
|
|
|
|
|
|
|
|
def fix_messages_by_bot(bot_profile: Any) -> None:
|
|
|
|
Message.objects.filter(sender=bot_profile).update(sender=notification_bot)
|
2021-02-12 08:19:30 +01:00
|
|
|
Message.objects.filter(recipient=bot_profile.recipient).update(
|
|
|
|
recipient=notification_bot.recipient
|
|
|
|
)
|
2020-02-08 18:38:45 +01:00
|
|
|
|
|
|
|
def clean_up_bot(bot_profile: Any) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
huddle_recipient_ids = Subscription.objects.filter(
|
|
|
|
user_profile_id=bot_profile.id, recipient__type=RECIPIENT_HUDDLE
|
2021-02-12 08:20:45 +01:00
|
|
|
).values_list("recipient_id", flat=True)
|
2020-02-08 18:38:45 +01:00
|
|
|
Huddle.objects.filter(recipient_id__in=huddle_recipient_ids).delete()
|
|
|
|
Recipient.objects.filter(id__in=huddle_recipient_ids).delete()
|
|
|
|
|
|
|
|
personal_recipient_id = bot_profile.recipient_id
|
|
|
|
bot_profile.delete()
|
|
|
|
Recipient.objects.filter(id=personal_recipient_id).delete()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
new_user_bot_email = getattr(settings, "NEW_USER_BOT", "new-user-bot@zulip.com")
|
2020-02-08 18:38:45 +01:00
|
|
|
try:
|
|
|
|
new_user_bot = get_bot_by_delivery_email(new_user_bot_email)
|
|
|
|
fix_messages_by_bot(new_user_bot)
|
|
|
|
clean_up_bot(new_user_bot)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
feedback_bot_email = getattr(settings, "FEEDBACK_BOT", "feedback@zulip.com")
|
2020-02-08 18:38:45 +01:00
|
|
|
try:
|
|
|
|
feedback_bot = get_bot_by_delivery_email(feedback_bot_email)
|
|
|
|
fix_messages_by_bot(feedback_bot)
|
|
|
|
clean_up_bot(feedback_bot)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-02-08 18:38:45 +01:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0272_realm_default_code_block_language"),
|
2020-02-08 18:38:45 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(fix_messages, reverse_code=migrations.RunPython.noop, elidable=True),
|
2020-02-08 18:38:45 +01:00
|
|
|
]
|