2016-10-12 05:13:32 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import List, Optional, Set, Text
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
from zerver.decorator import authenticated_json_post_view
|
2016-10-12 05:15:02 +02:00
|
|
|
from zerver.lib.actions import do_invite_users, do_refer_friend, \
|
|
|
|
get_default_subs, internal_send_message
|
2016-10-12 05:13:32 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables, JsonableError
|
|
|
|
from zerver.lib.response import json_success, json_error
|
2017-01-30 03:09:04 +01:00
|
|
|
from zerver.lib.streams import access_stream_by_name
|
2016-10-12 05:13:32 +02:00
|
|
|
from zerver.lib.validator import check_string, check_list
|
2017-01-30 03:09:04 +01:00
|
|
|
from zerver.models import PreregistrationUser, Stream, UserProfile
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
2017-02-12 21:21:31 +01:00
|
|
|
def json_invite_users(request, user_profile,
|
|
|
|
invitee_emails_raw=REQ("invitee_emails"),
|
|
|
|
body=REQ("custom_body", default=None)):
|
|
|
|
# type: (HttpRequest, UserProfile, str, Optional[str]) -> HttpResponse
|
2016-10-12 05:13:32 +02:00
|
|
|
if not invitee_emails_raw:
|
|
|
|
return json_error(_("You must specify at least one email address."))
|
2017-02-12 21:21:31 +01:00
|
|
|
if body == '':
|
|
|
|
body = None
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
invitee_emails = get_invitee_emails_set(invitee_emails_raw)
|
|
|
|
|
|
|
|
stream_names = request.POST.getlist('stream')
|
|
|
|
if not stream_names:
|
|
|
|
return json_error(_("You must specify at least one stream for invitees to join."))
|
|
|
|
|
|
|
|
# We unconditionally sub you to the notifications stream if it
|
|
|
|
# exists and is public.
|
2017-02-11 05:45:39 +01:00
|
|
|
notifications_stream = user_profile.realm.notifications_stream # type: Optional[Stream]
|
2016-10-12 05:13:32 +02:00
|
|
|
if notifications_stream and not notifications_stream.invite_only:
|
|
|
|
stream_names.append(notifications_stream.name)
|
|
|
|
|
|
|
|
streams = [] # type: List[Stream]
|
|
|
|
for stream_name in stream_names:
|
2017-01-30 03:09:04 +01:00
|
|
|
try:
|
|
|
|
(stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
|
|
|
|
except JsonableError:
|
2016-10-12 05:13:32 +02:00
|
|
|
return json_error(_("Stream does not exist: %s. No invites were sent.") % (stream_name,))
|
|
|
|
streams.append(stream)
|
|
|
|
|
2017-02-12 21:21:31 +01:00
|
|
|
ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams, body)
|
2016-10-12 05:13:32 +02:00
|
|
|
|
|
|
|
if ret_error is not None:
|
|
|
|
return json_error(data=error_data, msg=ret_error)
|
|
|
|
else:
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def get_invitee_emails_set(invitee_emails_raw):
|
|
|
|
# type: (str) -> Set[str]
|
|
|
|
invitee_emails_list = set(re.split(r'[,\n]', invitee_emails_raw))
|
|
|
|
invitee_emails = set()
|
|
|
|
for email in invitee_emails_list:
|
|
|
|
is_email_with_name = re.search(r'<(?P<email>.*)>', email)
|
|
|
|
if is_email_with_name:
|
|
|
|
email = is_email_with_name.group('email')
|
|
|
|
invitee_emails.add(email.strip())
|
|
|
|
return invitee_emails
|
|
|
|
|
|
|
|
@has_request_variables
|
2017-01-09 23:23:09 +01:00
|
|
|
def bulk_invite_users(request, user_profile,
|
|
|
|
invitee_emails_list=REQ('invitee_emails',
|
|
|
|
validator=check_list(check_string))):
|
2016-10-12 05:13:32 +02:00
|
|
|
# type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
|
|
|
|
invitee_emails = set(invitee_emails_list)
|
|
|
|
streams = get_default_subs(user_profile)
|
|
|
|
|
|
|
|
ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)
|
|
|
|
|
|
|
|
if ret_error is not None:
|
|
|
|
return json_error(data=error_data, msg=ret_error)
|
|
|
|
else:
|
|
|
|
# Report bulk invites to internal Zulip.
|
|
|
|
invited = PreregistrationUser.objects.filter(referred_by=user_profile)
|
|
|
|
internal_message = "%s <`%s`> invited %d people to Zulip." % (
|
|
|
|
user_profile.full_name, user_profile.email, invited.count())
|
2017-01-22 05:23:36 +01:00
|
|
|
internal_send_message(user_profile.realm, settings.NEW_USER_BOT, "stream",
|
2017-03-13 18:00:38 +01:00
|
|
|
"signups", user_profile.realm.string_id, internal_message)
|
2016-10-12 05:13:32 +02:00
|
|
|
return json_success()
|
|
|
|
|
2016-10-12 05:15:02 +02:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
|
|
|
def json_refer_friend(request, user_profile, email=REQ()):
|
|
|
|
# type: (HttpRequest, UserProfile, str) -> HttpResponse
|
|
|
|
if not email:
|
|
|
|
return json_error(_("No email address specified"))
|
|
|
|
if user_profile.invites_granted - user_profile.invites_used <= 0:
|
|
|
|
return json_error(_("Insufficient invites"))
|
|
|
|
|
2016-11-09 13:44:29 +01:00
|
|
|
do_refer_friend(user_profile, email)
|
2016-10-12 05:15:02 +02:00
|
|
|
|
|
|
|
return json_success()
|