Send an event when a user joins or leaves the realm

(imported from commit fcedba2d9a3e8968e93faa396113cddd5bf36a9d)
This commit is contained in:
Zev Benjamin
2013-03-29 10:35:37 -04:00
parent 130c94a2b1
commit fe45b1c8ad
3 changed files with 45 additions and 13 deletions

View File

@@ -59,7 +59,17 @@ def do_create_user(email, password, realm, full_name, short_name,
'short_name': short_name, 'short_name': short_name,
'user': email, 'user': email,
'domain': realm.domain}) 'domain': realm.domain})
return create_user(email, password, realm, full_name, short_name, active) user_profile = create_user(email, password, realm, full_name, short_name, active)
notice = dict(event=dict(type="realm_user", op="add",
person=dict(email=user_profile.user.email,
full_name=user_profile.full_name)),
users=[up.id for up in
UserProfile.objects.select_related().filter(realm=user_profile.realm,
user__is_active=True)])
tornado_callbacks.send_notification(notice)
return user_profile
def user_sessions(user): def user_sessions(user):
return [s for s in Session.objects.all() if s.get_decoded().get('_auth_user_id') == user.id] return [s for s in Session.objects.all() if s.get_decoded().get('_auth_user_id') == user.id]
@@ -77,6 +87,15 @@ def do_deactivate(user_profile):
'user': user_profile.user.email, 'user': user_profile.user.email,
'domain': user_profile.realm.domain}) 'domain': user_profile.realm.domain})
notice = dict(event=dict(type="realm_user", op="remove",
person=dict(email=user_profile.user.email,
full_name=user_profile.full_name)),
users=[up.id for up in
UserProfile.objects.select_related().filter(realm=user_profile.realm,
user__is_active=True)])
tornado_callbacks.send_notification(notice)
def do_change_user_email(user_profile, new_email): def do_change_user_email(user_profile, new_email):
old_email = user_profile.user.email old_email = user_profile.user.email
user_profile.user.email = new_email user_profile.user.email = new_email
@@ -706,6 +725,12 @@ def do_events_register(user_profile, apply_markdown=True, event_types=None):
ret['max_message_id'] = -1 ret['max_message_id'] = -1
if event_types is None or "pointer" in event_types: if event_types is None or "pointer" in event_types:
ret['pointer'] = user_profile.pointer ret['pointer'] = user_profile.pointer
if event_types is None or "realm_user" in event_types:
ret['realm_users'] = [{'email' : profile.user.email,
'full_name' : profile.full_name}
for profile in
UserProfile.objects.select_related().filter(realm=user_profile.realm,
user__is_active=True)]
# Apply events that came in while we were fetching initial data # Apply events that came in while we were fetching initial data
events = get_user_events(user_profile, queue_id, -1) events = get_user_events(user_profile, queue_id, -1)
@@ -714,6 +739,13 @@ def do_events_register(user_profile, apply_markdown=True, event_types=None):
ret['max_message_id'] = max(ret['max_message_id'], event['message']['id']) ret['max_message_id'] = max(ret['max_message_id'], event['message']['id'])
elif event['type'] == "pointer": elif event['type'] == "pointer":
ret['pointer'] = max(ret['pointer'], event['pointer']) ret['pointer'] = max(ret['pointer'], event['pointer'])
elif event['type'] == "realm_user":
if event['op'] == "add":
ret['realm_users'].append(simplejson.loads(event['person']))
elif event['op'] == "remove":
person = simplejson.loads(event['person'])
ret['realm_users'] = filter(lambda p: p['email'] != person['email'],
ret['realm_users'])
if events: if events:
ret['last_event_id'] = events[-1:]['id'] ret['last_event_id'] = events[-1:]['id']

View File

@@ -259,8 +259,18 @@ def process_new_message(data):
if 'stream_name' in data: if 'stream_name' in data:
stream_receive_message(data['realm_id'], data['stream_name'], message) stream_receive_message(data['realm_id'], data['stream_name'], message)
def process_event(data):
event = data['event']
for user_profile_id in data['users']:
for client in get_client_descriptors_for_user(user_profile_id):
if client.accepts_event_type(event['type']):
client.add_event(event.copy())
def process_notification(data): def process_notification(data):
if data['type'] == 'new_message': if 'type' not in data:
# Generic event that doesn't need special handling
process_event(data)
elif data['type'] == 'new_message':
process_new_message(data) process_new_message(data)
elif data['type'] == 'pointer_update': elif data['type'] == 'pointer_update':
update_pointer(data['user'], data['new_pointer']) update_pointer(data['user'], data['new_pointer'])

View File

@@ -417,16 +417,6 @@ def home(request):
user_profile.pointer = register_ret['max_message_id'] user_profile.pointer = register_ret['max_message_id']
user_profile.last_pointer_updater = request.session.session_key user_profile.last_pointer_updater = request.session.session_key
# Populate personals autocomplete list based on everyone in your
# realm. Later we might want a 2-layer autocomplete, where we
# consider specially some sort of "buddy list" who e.g. you've
# talked to before, but for small organizations, the right list is
# everyone in your realm.
people = [{'email' : profile.user.email,
'full_name' : profile.full_name}
for profile in
UserProfile.objects.select_related().filter(realm=user_profile.realm)]
# Pass parameters to the client-side JavaScript code. # Pass parameters to the client-side JavaScript code.
# These end up in a global JavaScript Object named 'page_params'. # These end up in a global JavaScript Object named 'page_params'.
page_params = simplejson.encoder.JSONEncoderForHTML().encode(dict( page_params = simplejson.encoder.JSONEncoderForHTML().encode(dict(
@@ -434,7 +424,7 @@ def home(request):
poll_timeout = settings.POLL_TIMEOUT, poll_timeout = settings.POLL_TIMEOUT,
have_initial_messages = user_has_messages, have_initial_messages = user_has_messages,
stream_list = gather_subscriptions(user_profile), stream_list = gather_subscriptions(user_profile),
people_list = people, people_list = register_ret['realm_users'],
initial_pointer = register_ret['pointer'], initial_pointer = register_ret['pointer'],
fullname = user_profile.full_name, fullname = user_profile.full_name,
email = user_profile.user.email, email = user_profile.user.email,