invitations: Send 'invites_changed' event for invitations events.

Fixes #7665

In case of invitation events, 'invites_changed' event without
any real payload is sent to all the realm admins and the user.
The event is handled by reloading the list to view recent changes.

Commit tweaked by shubhamdhama:
* Send an `invite_changed` event when an user accept an invite.
    Also, added the test for the same.
* No need to delete the invite list in frontend, current logic
    handles the case when the invite data is changed properly.
* Extracted the common logic for sending an event into
    `notify_invites_changed`.
This commit is contained in:
Aastha Gupta
2017-12-15 02:52:17 +05:30
committed by Tim Abbott
parent 40efac2193
commit dfde4fac85
4 changed files with 77 additions and 1 deletions

View File

@@ -26,6 +26,12 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
event.hotspots; event.hotspots;
break; break;
case 'invites_changed':
if ($('#admin-invites-list').length) {
settings_invites.set_up();
}
break;
case 'muted_topics': case 'muted_topics':
muting_ui.handle_updates(event.muted_topics); muting_ui.handle_updates(event.muted_topics);
break; break;

View File

@@ -326,6 +326,11 @@ def send_signup_message(sender: UserProfile, admin_realm_signup_notifications_st
) )
) )
def notify_invites_changed(user_profile: UserProfile) -> None:
event = dict(type="invites_changed")
admin_ids = [user.id for user in user_profile.realm.get_admin_users()]
send_event(event, admin_ids)
def notify_new_user(user_profile: UserProfile, internal: bool=False) -> None: def notify_new_user(user_profile: UserProfile, internal: bool=False) -> None:
if settings.NOTIFICATION_BOT is not None: if settings.NOTIFICATION_BOT is not None:
send_signup_message(settings.NOTIFICATION_BOT, "signups", user_profile, internal) send_signup_message(settings.NOTIFICATION_BOT, "signups", user_profile, internal)
@@ -413,6 +418,8 @@ def process_new_human_user(user_profile: UserProfile,
if prereg_user is not None: if prereg_user is not None:
PreregistrationUser.objects.filter(email__iexact=user_profile.email).exclude( PreregistrationUser.objects.filter(email__iexact=user_profile.email).exclude(
id=prereg_user.id).update(status=0) id=prereg_user.id).update(status=0)
if prereg_user.referred_by is not None:
notify_invites_changed(user_profile)
else: else:
PreregistrationUser.objects.filter(email__iexact=user_profile.email).update(status=0) PreregistrationUser.objects.filter(email__iexact=user_profile.email).update(status=0)
@@ -4428,6 +4435,7 @@ def do_invite_users(user_profile: UserProfile,
"so we didn't send them an invitation. We did send " "so we didn't send them an invitation. We did send "
"invitations to everyone else!"), "invitations to everyone else!"),
skipped, sent_invitations=True) skipped, sent_invitations=True)
notify_invites_changed(user_profile)
def do_get_user_invites(user_profile: UserProfile) -> List[Dict[str, Any]]: def do_get_user_invites(user_profile: UserProfile) -> List[Dict[str, Any]]:
days_to_activate = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', 7) days_to_activate = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', 7)
@@ -4469,6 +4477,7 @@ def do_revoke_user_invite(prereg_user: PreregistrationUser) -> None:
object_id=prereg_user.id).delete() object_id=prereg_user.id).delete()
prereg_user.delete() prereg_user.delete()
clear_scheduled_invitation_emails(email) clear_scheduled_invitation_emails(email)
notify_invites_changed(prereg_user)
def do_resend_user_invite_email(prereg_user: PreregistrationUser) -> int: def do_resend_user_invite_email(prereg_user: PreregistrationUser) -> int:
check_invite_limit(prereg_user.referred_by, 1) check_invite_limit(prereg_user.referred_by, 1)

View File

@@ -607,6 +607,8 @@ def apply_event(state: Dict[str, Any],
elif event['type'] == "update_global_notifications": elif event['type'] == "update_global_notifications":
assert event['notification_name'] in UserProfile.notification_setting_types assert event['notification_name'] in UserProfile.notification_setting_types
state[event['notification_name']] = event['setting'] state[event['notification_name']] = event['setting']
elif event['type'] == "invites_changed":
pass
elif event['type'] == "user_group": elif event['type'] == "user_group":
if event['op'] == 'add': if event['op'] == 'add':
state['realm_user_groups'].append(event['group']) state['realm_user_groups'].append(event['group'])

View File

@@ -16,7 +16,7 @@ from zerver.models import (
get_client, get_realm, get_stream_recipient, get_stream, get_user, get_client, get_realm, get_stream_recipient, get_stream, get_user,
Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile, Message, RealmDomain, Recipient, UserMessage, UserPresence, UserProfile,
Realm, Subscription, Stream, flush_per_request_caches, UserGroup, Service, Realm, Subscription, Stream, flush_per_request_caches, UserGroup, Service,
Attachment, Attachment, PreregistrationUser,
) )
from zerver.lib.actions import ( from zerver.lib.actions import (
@@ -53,6 +53,7 @@ from zerver.lib.actions import (
do_deactivate_stream, do_deactivate_stream,
do_deactivate_user, do_deactivate_user,
do_delete_message, do_delete_message,
do_invite_users,
do_mark_hotspot_as_read, do_mark_hotspot_as_read,
do_mute_topic, do_mute_topic,
do_reactivate_user, do_reactivate_user,
@@ -67,6 +68,7 @@ from zerver.lib.actions import (
do_remove_realm_filter, do_remove_realm_filter,
do_remove_streams_from_default_stream_group, do_remove_streams_from_default_stream_group,
do_rename_stream, do_rename_stream,
do_revoke_user_invite,
do_set_realm_authentication_methods, do_set_realm_authentication_methods,
do_set_realm_message_editing, do_set_realm_message_editing,
do_set_realm_property, do_set_realm_property,
@@ -923,6 +925,63 @@ class EventsRegisterTest(ZulipTestCase):
error = schema_checker('events[0]', events[0]) error = schema_checker('events[0]', events[0])
self.assert_on_error(error) self.assert_on_error(error)
def test_invite_user_event(self) -> None:
schema_checker = self.check_events_dict([
('type', equals('invites_changed')),
])
self.user_profile = self.example_user('iago')
streams = []
for stream_name in ["Denmark", "Scotland"]:
streams.append(get_stream(stream_name, self.user_profile.realm))
events = self.do_test(
lambda: do_invite_users(self.user_profile, ["foo@zulip.com"], streams, False),
state_change_expected=False,
)
error = schema_checker('events[0]', events[0])
self.assert_on_error(error)
def test_revoke_user_invite_event(self) -> None:
schema_checker = self.check_events_dict([
('type', equals('invites_changed')),
])
self.user_profile = self.example_user('iago')
streams = []
for stream_name in ["Denmark", "Verona"]:
streams.append(get_stream(stream_name, self.user_profile.realm))
do_invite_users(self.user_profile, ["foo@zulip.com"], streams, False)
prereg_users = PreregistrationUser.objects.filter(referred_by__realm=self.user_profile.realm)
events = self.do_test(
lambda: do_revoke_user_invite(prereg_users[0]),
state_change_expected=False,
)
error = schema_checker('events[0]', events[0])
self.assert_on_error(error)
def test_invitation_accept_invite_event(self) -> None:
schema_checker = self.check_events_dict([
('type', equals('invites_changed')),
])
self.user_profile = self.example_user('iago')
streams = []
for stream_name in ["Denmark", "Scotland"]:
streams.append(get_stream(stream_name, self.user_profile.realm))
do_invite_users(self.user_profile, ["foo@zulip.com"], streams, False)
prereg_users = PreregistrationUser.objects.get(email="foo@zulip.com")
events = self.do_test(
lambda: do_create_user('foo@zulip.com', 'password', self.user_profile.realm,
'full name', 'short name', prereg_user=prereg_users),
state_change_expected=True,
num_events=5,
)
error = schema_checker('events[4]', events[4])
self.assert_on_error(error)
def test_typing_events(self) -> None: def test_typing_events(self) -> None:
schema_checker = self.check_events_dict([ schema_checker = self.check_events_dict([
('type', equals('typing')), ('type', equals('typing')),