def do_invite_users(user_profile: UserProfile, invitee_emails: SizedTextIterable, streams: Iterable[Stream], invite_as: int=PreregistrationUser.INVITE_AS['MEMBER']) -> None:
    check_invite_limit(user_profile.realm, len(invitee_emails))
    realm = user_profile.realm
    if (not realm.invite_required):
        min_age = datetime.timedelta(days=settings.INVITES_MIN_USER_AGE_DAYS)
        if ((user_profile.date_joined > (timezone_now() - min_age)) and (not user_profile.is_realm_admin)):
            raise InvitationError(_('Your account is too new to send invites for this organization. Ask an organization admin, or a more experienced user.'), [], sent_invitations=False)
    good_emails: Set[str] = set()
    errors: List[Tuple[(str, str, bool)]] = []
    validate_email_allowed_in_realm = get_realm_email_validator(user_profile.realm)
    for email in invitee_emails:
        if (email == ''):
            continue
        email_error = validate_email_is_valid(email, validate_email_allowed_in_realm)
        if email_error:
            errors.append((email, email_error, False))
        else:
            good_emails.add(email)
    "\n    good_emails are emails that look ok so far,\n    but we still need to make sure they're not\n    gonna conflict with existing users\n    "
    error_dict = get_existing_user_errors(user_profile.realm, good_emails)
    skipped: List[Tuple[(str, str, bool)]] = []
    for email in error_dict:
        (msg, deactivated) = error_dict[email]
        skipped.append((email, msg, deactivated))
        good_emails.remove(email)
    validated_emails = list(good_emails)
    if errors:
        raise InvitationError(_("Some emails did not validate, so we didn't send any invitations."), (errors + skipped), sent_invitations=False)
    if (skipped and (len(skipped) == len(invitee_emails))):
        raise InvitationError(_("We weren't able to invite anyone."), skipped, sent_invitations=False)
    do_increment_logging_stat(user_profile.realm, COUNT_STATS['invites_sent::day'], None, timezone_now(), increment=len(validated_emails))
    for email in validated_emails:
        prereg_user = PreregistrationUser(email=email, referred_by=user_profile, invited_as=invite_as, realm=user_profile.realm)
        prereg_user.save()
        stream_ids = [stream.id for stream in streams]
        prereg_user.streams.set(stream_ids)
        event = {'prereg_id': prereg_user.id, 'referrer_id': user_profile.id}
        queue_json_publish('invites', event)
    if skipped:
        raise InvitationError(_("Some of those addresses are already using Zulip, so we didn't send them an invitation. We did send invitations to everyone else!"), skipped, sent_invitations=True)
    notify_invites_changed(user_profile)
def do_invite_users(user_profile: UserProfile, invitee_emails: SizedTextIterable, streams: Iterable[Stream], invite_as: int=PreregistrationUser.INVITE_AS['MEMBER']) -> None:
    check_invite_limit(user_profile.realm, len(invitee_emails))
    realm = user_profile.realm
    if (not realm.invite_required):
        min_age = datetime.timedelta(days=settings.INVITES_MIN_USER_AGE_DAYS)
        if ((user_profile.date_joined > (timezone_now() - min_age)) and (not user_profile.is_realm_admin)):
            raise InvitationError(_('Your account is too new to send invites for this organization. Ask an organization admin, or a more experienced user.'), [], sent_invitations=False)
    good_emails: Set[str] = set()
    errors: List[Tuple[(str, str, bool)]] = []
    validate_email_allowed_in_realm = get_realm_email_validator(user_profile.realm)
    for email in invitee_emails:
        if (email == ):
            continue
        email_error = validate_email_is_valid(email, validate_email_allowed_in_realm)
        if email_error:
            errors.append((email, email_error, False))
        else:
            good_emails.add(email)
    "\n    good_emails are emails that look ok so far,\n    but we still need to make sure they're not\n    gonna conflict with existing users\n    "
    error_dict = get_existing_user_errors(user_profile.realm, good_emails)
    skipped: List[Tuple[(str, str, bool)]] = []
    for email in error_dict:
        (msg, deactivated) = error_dict[email]
        skipped.append((email, msg, deactivated))
        good_emails.remove(email)
    validated_emails = list(good_emails)
    if errors:
        raise InvitationError(_("Some emails did not validate, so we didn't send any invitations."), (errors + skipped), sent_invitations=False)
    if (skipped and (len(skipped) == len(invitee_emails))):
        raise InvitationError(_("We weren't able to invite anyone."), skipped, sent_invitations=False)
    do_increment_logging_stat(user_profile.realm, COUNT_STATS['invites_sent::day'], None, timezone_now(), increment=len(validated_emails))
    for email in validated_emails:
        prereg_user = PreregistrationUser(email=email, referred_by=user_profile, invited_as=invite_as, realm=user_profile.realm)
        prereg_user.save()
        stream_ids = [stream.id for stream in streams]
        prereg_user.streams.set(stream_ids)
        event = {'prereg_id': prereg_user.id, 'referrer_id': user_profile.id}
        queue_json_publish('invites', event)
    if skipped:
        raise InvitationError(_("Some of those addresses are already using Zulip, so we didn't send them an invitation. We did send invitations to everyone else!"), skipped, sent_invitations=True)
    notify_invites_changed(user_profile)