diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index bca954d7eb..81f289b482 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -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) diff --git a/zerver/management/commands/realm_filters.py b/zerver/management/commands/realm_filters.py index 93eedda898..ed8746fe01 100644 --- a/zerver/management/commands/realm_filters.py +++ b/zerver/management/commands/realm_filters.py @@ -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") diff --git a/zerver/tests.py b/zerver/tests.py index d9482a9c99..809b981617 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -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")) diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 781f455a65..6d952d8258 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -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,