def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile, log: bool=True) -> Dict[(str, str)]:
    old_name = stream.name
    stream.name = new_name
    stream.save(update_fields=['name'])
    if log:
        log_event({'type': 'stream_name_change', 'realm': stream.realm.string_id, 'new_name': new_name})
    recipient_id = stream.recipient_id
    messages = Message.objects.filter(recipient_id=recipient_id).only('id')
    old_cache_key = get_stream_cache_key(old_name, stream.realm_id)
    new_cache_key = get_stream_cache_key(stream.name, stream.realm_id)
    if (old_cache_key != new_cache_key):
        cache_delete(old_cache_key)
        cache_set(new_cache_key, stream)
    cache_set(display_recipient_cache_key(recipient_id), stream.name)
    cache_delete_many((to_dict_cache_key_id(message.id) for message in messages))
    new_email = encode_email_address(stream, show_sender=True)
    data_updates = [['email_address', new_email], ['name', new_name]]
    for (property, value) in data_updates:
        event = dict(op='update', type='stream', property=property, value=value, stream_id=stream.id, name=old_name)
        send_event(stream.realm, event, can_access_stream_user_ids(stream))
    sender = get_system_bot(settings.NOTIFICATION_BOT)
    with override_language(stream.realm.default_language):
        internal_send_stream_message(stream.realm, sender, stream, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, _('{user_name} renamed stream {old_stream_name} to {new_stream_name}.').format(user_name=f'@_**{user_profile.full_name}|{user_profile.id}**', old_stream_name=f'**{old_name}**', new_stream_name=f'**{new_name}**'))
    return {'email_address': new_email}
def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile, log: bool=True) -> Dict[(str, str)]:
    old_name = stream.name
    stream.name = new_name
    stream.save(update_fields=['name'])
    if log:
        log_event({'type': 'stream_name_change', 'realm': stream.realm.string_id, 'new_name': new_name})
    recipient_id = stream.recipient_id
    messages = Message.objects.filter(recipient_id=recipient_id).only('id')
    old_cache_key = get_stream_cache_key(old_name, stream.realm_id)
    new_cache_key = get_stream_cache_key(stream.name, stream.realm_id)
    if (old_cache_key != new_cache_key):
        cache_delete(old_cache_key)
        cache_set(new_cache_key, stream)
    cache_set(display_recipient_cache_key(recipient_id), stream.name)
    cache_delete_many((to_dict_cache_key_id(message.id) for message in messages))
    new_email = encode_email_address(stream, show_sender=True)
    data_updates = [['email_address', new_email], ['name', new_name]]
    for (property, value) in data_updates:
        event = dict(op='update', type='stream', property=property, value=value, stream_id=stream.id, name=old_name)
        send_event(stream.realm, event, can_access_stream_user_ids(stream))
    sender = get_system_bot(settings.NOTIFICATION_BOT)
    with override_language(stream.realm.default_language):
        internal_send_stream_message(stream.realm, sender, stream, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, _('{user_name} renamed stream {old_stream_name} to {new_stream_name}.').format(user_name=f'@_**{user_profile.full_name}|{user_profile.id}**', old_stream_name=f'**{old_name}**', new_stream_name=f'**{new_name}**'))
    return {'email_address': new_email}