actions: Split out zerver.actions.realm_linkifiers.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-04-14 14:32:56 -07:00
parent e887abcf41
commit 975f5a3c2d
6 changed files with 72 additions and 63 deletions

View File

@ -0,0 +1,59 @@
from typing import Dict, Optional
from zerver.models import (
Realm,
RealmFilter,
active_user_ids,
linkifiers_for_realm,
realm_filters_for_realm,
)
from zerver.tornado.django_api import send_event
def notify_linkifiers(realm: Realm) -> None:
realm_linkifiers = linkifiers_for_realm(realm.id)
event: Dict[str, object] = dict(type="realm_linkifiers", realm_linkifiers=realm_linkifiers)
send_event(realm, event, active_user_ids(realm.id))
# Below is code for backwards compatibility. The now deprecated
# "realm_filters" event-type is used by older clients, and uses
# tuples.
realm_filters = realm_filters_for_realm(realm.id)
event = dict(type="realm_filters", realm_filters=realm_filters)
send_event(realm, event, active_user_ids(realm.id))
# NOTE: Regexes must be simple enough that they can be easily translated to JavaScript
# RegExp syntax. In addition to JS-compatible syntax, the following features are available:
# * Named groups will be converted to numbered groups automatically
# * Inline-regex flags will be stripped, and where possible translated to RegExp-wide flags
def do_add_linkifier(realm: Realm, pattern: str, url_format_string: str) -> int:
pattern = pattern.strip()
url_format_string = url_format_string.strip()
linkifier = RealmFilter(realm=realm, pattern=pattern, url_format_string=url_format_string)
linkifier.full_clean()
linkifier.save()
notify_linkifiers(realm)
return linkifier.id
def do_remove_linkifier(
realm: Realm, pattern: Optional[str] = None, id: Optional[int] = None
) -> None:
if pattern is not None:
RealmFilter.objects.get(realm=realm, pattern=pattern).delete()
else:
RealmFilter.objects.get(realm=realm, id=id).delete()
notify_linkifiers(realm)
def do_update_linkifier(realm: Realm, id: int, pattern: str, url_format_string: str) -> None:
pattern = pattern.strip()
url_format_string = url_format_string.strip()
linkifier = RealmFilter.objects.get(realm=realm, id=id)
linkifier.pattern = pattern
linkifier.url_format_string = url_format_string
linkifier.full_clean()
linkifier.save(update_fields=["pattern", "url_format_string"])
notify_linkifiers(realm)

View File

@ -239,7 +239,6 @@ from zerver.models import (
RealmAuditLog, RealmAuditLog,
RealmDomain, RealmDomain,
RealmEmoji, RealmEmoji,
RealmFilter,
RealmUserDefault, RealmUserDefault,
Recipient, Recipient,
ScheduledEmail, ScheduledEmail,
@ -279,9 +278,7 @@ from zerver.models import (
get_user_by_delivery_email, get_user_by_delivery_email,
get_user_profile_by_id, get_user_profile_by_id,
is_cross_realm_bot_email, is_cross_realm_bot_email,
linkifiers_for_realm,
query_for_ids, query_for_ids,
realm_filters_for_realm,
validate_attachment_request, validate_attachment_request,
) )
from zerver.tornado.django_api import send_event from zerver.tornado.django_api import send_event
@ -7724,55 +7721,6 @@ def do_mark_hotspot_as_read(user: UserProfile, hotspot: str) -> None:
send_event(user.realm, event, [user.id]) send_event(user.realm, event, [user.id])
def notify_linkifiers(realm: Realm) -> None:
realm_linkifiers = linkifiers_for_realm(realm.id)
event: Dict[str, object] = dict(type="realm_linkifiers", realm_linkifiers=realm_linkifiers)
send_event(realm, event, active_user_ids(realm.id))
# Below is code for backwards compatibility. The now deprecated
# "realm_filters" event-type is used by older clients, and uses
# tuples.
realm_filters = realm_filters_for_realm(realm.id)
event = dict(type="realm_filters", realm_filters=realm_filters)
send_event(realm, event, active_user_ids(realm.id))
# NOTE: Regexes must be simple enough that they can be easily translated to JavaScript
# RegExp syntax. In addition to JS-compatible syntax, the following features are available:
# * Named groups will be converted to numbered groups automatically
# * Inline-regex flags will be stripped, and where possible translated to RegExp-wide flags
def do_add_linkifier(realm: Realm, pattern: str, url_format_string: str) -> int:
pattern = pattern.strip()
url_format_string = url_format_string.strip()
linkifier = RealmFilter(realm=realm, pattern=pattern, url_format_string=url_format_string)
linkifier.full_clean()
linkifier.save()
notify_linkifiers(realm)
return linkifier.id
def do_remove_linkifier(
realm: Realm, pattern: Optional[str] = None, id: Optional[int] = None
) -> None:
if pattern is not None:
RealmFilter.objects.get(realm=realm, pattern=pattern).delete()
else:
RealmFilter.objects.get(realm=realm, id=id).delete()
notify_linkifiers(realm)
def do_update_linkifier(realm: Realm, id: int, pattern: str, url_format_string: str) -> None:
pattern = pattern.strip()
url_format_string = url_format_string.strip()
linkifier = RealmFilter.objects.get(realm=realm, id=id)
linkifier.pattern = pattern
linkifier.url_format_string = url_format_string
linkifier.full_clean()
linkifier.save(update_fields=["pattern", "url_format_string"])
notify_linkifiers(realm)
@transaction.atomic(durable=True) @transaction.atomic(durable=True)
def do_add_realm_domain( def do_add_realm_domain(
realm: Realm, domain: str, allow_subdomains: bool, *, acting_user: Optional[UserProfile] realm: Realm, domain: str, allow_subdomains: bool, *, acting_user: Optional[UserProfile]

View File

@ -4,7 +4,7 @@ from typing import Any
from django.core.management.base import CommandError from django.core.management.base import CommandError
from zerver.lib.actions import do_add_linkifier, do_remove_linkifier from zerver.actions.realm_linkifiers import do_add_linkifier, do_remove_linkifier
from zerver.lib.management import ZulipBaseCommand from zerver.lib.management import ZulipBaseCommand
from zerver.models import linkifiers_for_realm from zerver.models import linkifiers_for_realm

View File

@ -10,13 +10,9 @@ from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.actions.realm_linkifiers import do_add_linkifier
from zerver.actions.realm_playgrounds import do_add_realm_playground from zerver.actions.realm_playgrounds import do_add_realm_playground
from zerver.lib.actions import ( from zerver.lib.actions import do_add_reaction, do_create_user, update_user_presence
do_add_linkifier,
do_add_reaction,
do_create_user,
update_user_presence,
)
from zerver.lib.events import do_events_register from zerver.lib.events import do_events_register
from zerver.lib.initial_password import initial_password from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase

View File

@ -13,6 +13,11 @@ from unittest import mock
import orjson import orjson
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.actions.realm_linkifiers import (
do_add_linkifier,
do_remove_linkifier,
do_update_linkifier,
)
from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
from zerver.actions.submessage import do_add_submessage from zerver.actions.submessage import do_add_submessage
from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notification from zerver.actions.typing import check_send_typing_notification, do_send_stream_typing_notification
@ -31,7 +36,6 @@ from zerver.lib.actions import (
check_add_realm_emoji, check_add_realm_emoji,
do_add_alert_words, do_add_alert_words,
do_add_default_stream, do_add_default_stream,
do_add_linkifier,
do_add_reaction, do_add_reaction,
do_add_realm_domain, do_add_realm_domain,
do_add_streams_to_default_stream_group, do_add_streams_to_default_stream_group,
@ -72,7 +76,6 @@ from zerver.lib.actions import (
do_remove_alert_words, do_remove_alert_words,
do_remove_default_stream, do_remove_default_stream,
do_remove_default_stream_group, do_remove_default_stream_group,
do_remove_linkifier,
do_remove_reaction, do_remove_reaction,
do_remove_realm_custom_profile_field, do_remove_realm_custom_profile_field,
do_remove_realm_domain, do_remove_realm_domain,
@ -90,7 +93,6 @@ from zerver.lib.actions import (
do_unmute_topic, do_unmute_topic,
do_unmute_user, do_unmute_user,
do_update_embedded_data, do_update_embedded_data,
do_update_linkifier,
do_update_message, do_update_message,
do_update_message_flags, do_update_message_flags,
do_update_outgoing_webhook_service, do_update_outgoing_webhook_service,

View File

@ -2,8 +2,12 @@ from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from zerver.actions.realm_linkifiers import (
do_add_linkifier,
do_remove_linkifier,
do_update_linkifier,
)
from zerver.decorator import require_realm_admin from zerver.decorator import require_realm_admin
from zerver.lib.actions import do_add_linkifier, do_remove_linkifier, do_update_linkifier
from zerver.lib.exceptions import JsonableError, ValidationFailureError from zerver.lib.exceptions import JsonableError, ValidationFailureError
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success