def do_deactivate_stream(stream: Stream, log: bool=True, acting_user: Optional[UserProfile]=None) -> None:
    affected_user_ids = can_access_stream_user_ids(stream)
    get_active_subscriptions_for_stream_id(stream.id).update(active=False)
    was_invite_only = stream.invite_only
    stream.deactivated = True
    stream.invite_only = True
    old_name = stream.name
    new_name = ('!DEACTIVATED:' + old_name)[:Stream.MAX_NAME_LENGTH]
    for i in range(20):
        if stream_name_in_use(new_name, stream.realm_id):
            new_name = ('!' + new_name)[:Stream.MAX_NAME_LENGTH]
        else:
            break
    stream.name = new_name[:Stream.MAX_NAME_LENGTH]
    stream.save(update_fields=['name', 'deactivated', 'invite_only'])
    if DefaultStream.objects.filter(realm_id=stream.realm_id, stream_id=stream.id).exists():
        do_remove_default_stream(stream)
    default_stream_groups_for_stream = DefaultStreamGroup.objects.filter(streams__id=stream.id)
    for group in default_stream_groups_for_stream:
        do_remove_streams_from_default_stream_group(stream.realm, group, [stream])
    old_cache_key = get_stream_cache_key(old_name, stream.realm_id)
    cache_delete(old_cache_key)
    stream_dict = stream.to_dict()
    stream_dict.update(dict(name=old_name, invite_only=was_invite_only))
    event = dict(type='stream', op='delete', streams=[stream_dict])
    send_event(stream.realm, event, affected_user_ids)
    event_time = timezone_now()
    RealmAuditLog.objects.create(realm=stream.realm, acting_user=acting_user, modified_stream=stream, event_type=RealmAuditLog.STREAM_DEACTIVATED, event_time=event_time)
def do_deactivate_stream(stream: Stream, log: bool=True, acting_user: Optional[UserProfile]=None) -> None:
    affected_user_ids = can_access_stream_user_ids(stream)
    get_active_subscriptions_for_stream_id(stream.id).update(active=False)
    was_invite_only = stream.invite_only
    stream.deactivated = True
    stream.invite_only = True
    old_name = stream.name
    new_name = ('!DEACTIVATED:' + old_name)[:Stream.MAX_NAME_LENGTH]
    for i in range(20):
        if stream_name_in_use(new_name, stream.realm_id):
            new_name = ('!' + new_name)[:Stream.MAX_NAME_LENGTH]
        else:
            break
    stream.name = new_name[:Stream.MAX_NAME_LENGTH]
    stream.save(update_fields=['name', 'deactivated', 'invite_only'])
    if DefaultStream.objects.filter(realm_id=stream.realm_id, stream_id=stream.id).exists():
        do_remove_default_stream(stream)
    default_stream_groups_for_stream = DefaultStreamGroup.objects.filter(streams__id=stream.id)
    for group in default_stream_groups_for_stream:
        do_remove_streams_from_default_stream_group(stream.realm, group, [stream])
    old_cache_key = get_stream_cache_key(old_name, stream.realm_id)
    cache_delete(old_cache_key)
    stream_dict = stream.to_dict()
    stream_dict.update(dict(name=old_name, invite_only=was_invite_only))
    event = dict(type='stream', op='delete', streams=[stream_dict])
    send_event(stream.realm, event, affected_user_ids)
    event_time = timezone_now()
    RealmAuditLog.objects.create(realm=stream.realm, acting_user=acting_user, modified_stream=stream, event_type=RealmAuditLog.STREAM_DEACTIVATED, event_time=event_time)