bots: Pass realm to get_system_bot calls in export/import.

This commit is contained in:
Mateusz Mandera 2021-07-26 17:17:10 +02:00 committed by Tim Abbott
parent 7a36ac6eca
commit 43329b6a34
2 changed files with 20 additions and 7 deletions

View File

@ -61,6 +61,7 @@ from zerver.models import (
UserPresence,
UserProfile,
get_display_recipient,
get_realm,
get_system_bot,
get_user_profile_by_id,
)
@ -898,6 +899,7 @@ def fetch_user_profile_cross_realm(response: TableData, config: Config, context:
if realm.string_id == settings.SYSTEM_BOT_REALM:
return
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
for bot in settings.INTERNAL_BOTS:
bot_name = bot["var_name"]
if bot_name not in bot_name_to_default_email:
@ -905,7 +907,7 @@ def fetch_user_profile_cross_realm(response: TableData, config: Config, context:
bot_email = bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,)
bot_default_email = bot_name_to_default_email[bot_name]
bot_user_id = get_system_bot(bot_email).id
bot_user_id = get_system_bot(bot_email, internal_realm.id).id
recipient_id = Recipient.objects.get(type_id=bot_user_id, type=Recipient.PERSONAL).id
response["zerver_userprofile_crossrealm"].append(
@ -1396,7 +1398,10 @@ def export_files_from_s3(
object_prefix = f"{realm.id}/"
if settings.EMAIL_GATEWAY_BOT is not None:
email_gateway_bot: Optional[UserProfile] = get_system_bot(settings.EMAIL_GATEWAY_BOT)
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
email_gateway_bot: Optional[UserProfile] = get_system_bot(
settings.EMAIL_GATEWAY_BOT, internal_realm.id
)
else:
email_gateway_bot = None
@ -1467,10 +1472,12 @@ def export_avatars_from_local(realm: Realm, local_dir: Path, output_dir: Path) -
records = []
users = list(UserProfile.objects.filter(realm=realm))
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
users += [
get_system_bot(settings.NOTIFICATION_BOT),
get_system_bot(settings.EMAIL_GATEWAY_BOT),
get_system_bot(settings.WELCOME_BOT),
get_system_bot(settings.NOTIFICATION_BOT, internal_realm.id),
get_system_bot(settings.EMAIL_GATEWAY_BOT, internal_realm.id),
get_system_bot(settings.WELCOME_BOT, internal_realm.id),
]
for user in users:
if user.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR:

View File

@ -67,6 +67,7 @@ from zerver.models import (
UserPresence,
UserProfile,
get_huddle_hash,
get_realm,
get_system_bot,
get_user_profile_by_id,
)
@ -930,9 +931,14 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
# Remap the user IDs for notification_bot and friends to their
# appropriate IDs on this server
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
for item in data["zerver_userprofile_crossrealm"]:
logging.info("Adding to ID map: %s %s", item["id"], get_system_bot(item["email"]).id)
new_user_id = get_system_bot(item["email"]).id
logging.info(
"Adding to ID map: %s %s",
item["id"],
get_system_bot(item["email"], internal_realm.id).id,
)
new_user_id = get_system_bot(item["email"], internal_realm.id).id
update_id_map(table="user_profile", old_id=item["id"], new_id=new_user_id)
new_recipient_id = Recipient.objects.get(type=Recipient.PERSONAL, type_id=new_user_id).id
update_id_map(table="recipient", old_id=item["recipient_id"], new_id=new_recipient_id)