mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
do_invite_users: Avoid creating broken PreregistrationUser objects.
While this may not have created a clear user-facing bug, it seems less confusing for do_invite_users to only create PreregistrationUser objects for users who actually received an email invitation.
This commit is contained in:
@@ -3463,7 +3463,7 @@ def user_email_is_unique(email):
|
|||||||
|
|
||||||
def do_invite_users(user_profile, invitee_emails, streams):
|
def do_invite_users(user_profile, invitee_emails, streams):
|
||||||
# type: (UserProfile, SizedTextIterable, Iterable[Stream]) -> Tuple[Optional[str], Dict[str, Union[List[Tuple[Text, str]], bool]]]
|
# type: (UserProfile, SizedTextIterable, Iterable[Stream]) -> Tuple[Optional[str], Dict[str, Union[List[Tuple[Text, str]], bool]]]
|
||||||
new_prereg_users = [] # type: List[PreregistrationUser]
|
validated_emails = [] # type: List[Text]
|
||||||
errors = [] # type: List[Tuple[Text, str]]
|
errors = [] # type: List[Tuple[Text, str]]
|
||||||
skipped = [] # type: List[Tuple[Text, str]]
|
skipped = [] # type: List[Tuple[Text, str]]
|
||||||
|
|
||||||
@@ -3499,16 +3499,7 @@ def do_invite_users(user_profile, invitee_emails, streams):
|
|||||||
skipped.append((email, _("Already has an account.")))
|
skipped.append((email, _("Already has an account.")))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# The logged in user is the referrer.
|
validated_emails.append(email)
|
||||||
prereg_user = PreregistrationUser(email=email, referred_by=user_profile)
|
|
||||||
|
|
||||||
# We save twice because you cannot associate a ManyToMany field
|
|
||||||
# on an unsaved object.
|
|
||||||
prereg_user.save()
|
|
||||||
prereg_user.streams = streams
|
|
||||||
prereg_user.save()
|
|
||||||
|
|
||||||
new_prereg_users.append(prereg_user)
|
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
ret_error = _("Some emails did not validate, so we didn't send any invitations.")
|
ret_error = _("Some emails did not validate, so we didn't send any invitations.")
|
||||||
@@ -3521,12 +3512,21 @@ def do_invite_users(user_profile, invitee_emails, streams):
|
|||||||
ret_error_data = {'errors': skipped, 'sent_invitations': False}
|
ret_error_data = {'errors': skipped, 'sent_invitations': False}
|
||||||
return ret_error, ret_error_data
|
return ret_error, ret_error_data
|
||||||
|
|
||||||
# If we encounter an exception at any point before now, there are no unwanted side-effects,
|
# Now that we are past all the possible errors, we actually create
|
||||||
# since it is totally fine to have duplicate PreregistrationUsers
|
# the PreregistrationUser objects and trigger the email invitations.
|
||||||
for user in new_prereg_users:
|
for email in validated_emails:
|
||||||
event = {"email": user.email, "referrer_email": user_profile.email}
|
# The logged in user is the referrer.
|
||||||
|
prereg_user = PreregistrationUser(email=email, referred_by=user_profile)
|
||||||
|
|
||||||
|
# We save twice because you cannot associate a ManyToMany field
|
||||||
|
# on an unsaved object.
|
||||||
|
prereg_user.save()
|
||||||
|
prereg_user.streams = streams
|
||||||
|
prereg_user.save()
|
||||||
|
|
||||||
|
event = {"email": prereg_user.email, "referrer_email": user_profile.email}
|
||||||
queue_json_publish("invites", event,
|
queue_json_publish("invites", event,
|
||||||
lambda event: do_send_confirmation_email(user, user_profile))
|
lambda event: do_send_confirmation_email(prereg_user, user_profile))
|
||||||
|
|
||||||
if skipped:
|
if skipped:
|
||||||
ret_error = _("Some of those addresses are already using Zulip, "
|
ret_error = _("Some of those addresses are already using Zulip, "
|
||||||
|
|||||||
Reference in New Issue
Block a user