Send realm filters to clients

(imported from commit a3ba31701baa4d515ea3bf36c39d7255f1dd20d6)
This commit is contained in:
Leo Franchi 2014-01-06 17:42:02 -05:00
parent 80e6dab78a
commit 76892c52aa
4 changed files with 31 additions and 5 deletions

View File

@ -13,7 +13,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity,
resolve_email_to_domain, email_to_username, display_recipient_cache_key, \
get_stream_cache_key, to_dict_cache_key_id, is_super_user, \
UserActivityInterval, get_active_user_dicts_in_realm, RealmAlias, \
ScheduledJob
ScheduledJob, realm_filters_for_domain, RealmFilter
from zerver.lib.avatar import get_avatar_url
from django.db import transaction, IntegrityError
@ -1753,6 +1753,8 @@ def fetch_initial_state_data(user_profile, event_types, queue_id):
state['alert_words'] = user_alert_words(user_profile)
if event_types is None or "muted_topics" in event_types:
state['muted_topics'] = ujson.loads(user_profile.muted_topics)
if event_types is None or "realm_filters" in event_types:
state['realm_filters'] = realm_filters_for_domain(user_profile.realm.domain)
return state
def apply_events(state, events):
@ -1825,6 +1827,8 @@ def apply_events(state, events):
state['alert_words'] = event['alert_words']
elif event['type'] == "muted_topics":
state['muted_topics'] = event["muted_topics"]
elif event['type'] == "realm_filters":
state['realm_filters'] = event["realm_filters"]
else:
raise ValueError("Unexpected event type %s" % (event['type'],))
@ -2305,6 +2309,22 @@ def do_set_muted_topics(user_profile, muted_topics):
users=[user_profile.id])
tornado_callbacks.send_notification(notice)
def notify_realm_filters(realm):
realm_filters = realm_filters_for_domain(realm.domain)
user_ids = [userdict['id'] for userdict in get_active_user_dicts_in_realm(realm)]
notice = dict(event=dict(type="realm_filters", realm_filters=realm_filters), users=user_ids)
tornado_callbacks.send_notification(notice)
def do_add_realm_filter(realm, pattern, url_format_string):
RealmFilter(realm=realm, pattern=pattern,
url_format_string=url_format_string).save()
notify_realm_filters(realm)
def do_remove_realm_filter(realm, pattern):
RealmFilter.objects.get(realm=realm, pattern=pattern).delete()
notify_realm_filters(realm)
def get_emails_from_user_ids(user_ids):
# We may eventually use memcached to speed this up, but the DB is fast.
return UserProfile.emails_from_ids(user_ids)

View File

@ -3,6 +3,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand
from zerver.models import RealmFilter, all_realm_filters, Realm
from zerver.lib.actions import do_add_realm_filter, do_remove_realm_filter
import sys
class Command(BaseCommand):
@ -44,11 +45,10 @@ Example: python manage.py realm_filters --realm=zulip.com --op=show
if options["op"] == "add":
url_format_string = args[1]
RealmFilter(realm=realm, pattern=pattern,
url_format_string=url_format_string).save()
do_add_realm_filter(realm, pattern, url_format_string)
sys.exit(0)
elif options["op"] == "remove":
RealmFilter.objects.get(realm=realm, pattern=pattern).delete()
do_remove_realm_filter(realm, pattern)
sys.exit(0)
else:
self.print_help("python manage.py", "realm_filters")

View File

@ -2942,7 +2942,7 @@ class GetEventsTest(AuthedTestCase):
from zerver.lib.actions import fetch_initial_state_data, apply_events, do_add_alert_words, \
do_set_muted_topics, do_add_realm_emoji, do_remove_realm_emoji, do_remove_alert_words, \
do_remove_subscription
do_remove_subscription, do_add_realm_filter, do_remove_realm_filter
from zerver.lib.event_queue import allocate_client_descriptor
from collections import OrderedDict
class EventsRegisterTest(AuthedTestCase):
@ -2998,6 +2998,11 @@ class EventsRegisterTest(AuthedTestCase):
"https://realm.com/my_emoji"))
self.do_test(lambda: do_remove_realm_emoji(get_realm("zulip.com"), "my_emoji"))
def test_realm_filter_events(self):
self.do_test(lambda: do_add_realm_filter(get_realm("zulip.com"), "#[123]",
"https://realm.com/my_realm_filter/%(id)s"))
self.do_test(lambda: do_remove_realm_filter(get_realm("zulip.com"), "#[123]"))
def test_subscribe_events(self):
self.do_test(lambda: self.subscribe_to_stream("hamlet@zulip.com", "test_stream"),
matcher=lambda a, b: self.match_with_reorder(a, b, "subscriptions"))

View File

@ -826,6 +826,7 @@ def home(request):
staging = settings.STAGING_DEPLOYED or not settings.DEPLOYED,
alert_words = register_ret['alert_words'],
muted_topics = register_ret['muted_topics'],
realm_filters = register_ret['realm_filters'],
is_admin = user_profile.is_admin(),
notify_for_streams_by_default = notify_for_streams_by_default(user_profile),
name_changes_disabled = settings.NAME_CHANGES_DISABLED,