2022-05-16 11:55:36 +02:00
|
|
|
from typing import Dict, List, Optional
|
2022-04-14 23:32:56 +02:00
|
|
|
|
2022-03-14 11:50:24 +01:00
|
|
|
import orjson
|
2022-03-14 14:58:19 +01:00
|
|
|
from django.db import transaction
|
2022-03-14 11:50:24 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2022-05-16 11:55:36 +02:00
|
|
|
from zerver.lib.types import LinkifierDict
|
2022-04-14 23:32:56 +02:00
|
|
|
from zerver.models import (
|
|
|
|
Realm,
|
2022-03-14 11:50:24 +01:00
|
|
|
RealmAuditLog,
|
2022-04-14 23:32:56 +02:00
|
|
|
RealmFilter,
|
2022-03-14 11:50:24 +01:00
|
|
|
UserProfile,
|
2022-04-14 23:32:56 +02:00
|
|
|
active_user_ids,
|
|
|
|
linkifiers_for_realm,
|
|
|
|
realm_filters_for_realm,
|
|
|
|
)
|
|
|
|
from zerver.tornado.django_api import send_event
|
|
|
|
|
|
|
|
|
2022-05-16 11:55:36 +02:00
|
|
|
def notify_linkifiers(realm: Realm, realm_linkifiers: List[LinkifierDict]) -> None:
|
2022-04-14 23:32:56 +02:00
|
|
|
event: Dict[str, object] = dict(type="realm_linkifiers", realm_linkifiers=realm_linkifiers)
|
2022-03-14 14:58:19 +01:00
|
|
|
transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
|
2022-04-14 23:32:56 +02:00
|
|
|
|
|
|
|
# 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)
|
2022-03-14 14:58:19 +01:00
|
|
|
legacy_event = dict(type="realm_filters", realm_filters=realm_filters)
|
|
|
|
transaction.on_commit(lambda: send_event(realm, legacy_event, active_user_ids(realm.id)))
|
2022-04-14 23:32:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2022-03-14 14:58:19 +01:00
|
|
|
@transaction.atomic(durable=True)
|
2022-03-14 11:50:24 +01:00
|
|
|
def do_add_linkifier(
|
|
|
|
realm: Realm, pattern: str, url_format_string: str, *, acting_user: Optional[UserProfile]
|
|
|
|
) -> int:
|
2022-04-14 23:32:56 +02:00
|
|
|
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()
|
2022-05-16 11:55:36 +02:00
|
|
|
|
|
|
|
realm_linkifiers = linkifiers_for_realm(realm.id)
|
2022-03-14 11:50:24 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_LINKIFIER_ADDED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_linkifiers": realm_linkifiers,
|
|
|
|
"added_linkifier": LinkifierDict(
|
|
|
|
pattern=pattern,
|
|
|
|
url_format=url_format_string,
|
|
|
|
id=linkifier.id,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
2022-05-16 11:55:36 +02:00
|
|
|
notify_linkifiers(realm, realm_linkifiers)
|
2022-04-14 23:32:56 +02:00
|
|
|
|
|
|
|
return linkifier.id
|
|
|
|
|
|
|
|
|
2022-03-14 14:59:29 +01:00
|
|
|
@transaction.atomic(durable=True)
|
2022-04-14 23:32:56 +02:00
|
|
|
def do_remove_linkifier(
|
2022-03-14 14:50:55 +01:00
|
|
|
realm: Realm,
|
|
|
|
pattern: Optional[str] = None,
|
|
|
|
id: Optional[int] = None,
|
|
|
|
*,
|
|
|
|
acting_user: Optional[UserProfile] = None,
|
2022-04-14 23:32:56 +02:00
|
|
|
) -> None:
|
|
|
|
if pattern is not None:
|
2022-03-14 14:50:55 +01:00
|
|
|
realm_linkifier = RealmFilter.objects.get(realm=realm, pattern=pattern)
|
2022-04-14 23:32:56 +02:00
|
|
|
else:
|
2022-05-29 21:52:25 +02:00
|
|
|
assert id is not None
|
2022-03-14 14:50:55 +01:00
|
|
|
realm_linkifier = RealmFilter.objects.get(realm=realm, id=id)
|
|
|
|
|
|
|
|
pattern = realm_linkifier.pattern
|
|
|
|
url_format = realm_linkifier.url_format_string
|
|
|
|
realm_linkifier.delete()
|
2022-05-16 11:55:36 +02:00
|
|
|
|
|
|
|
realm_linkifiers = linkifiers_for_realm(realm.id)
|
2022-03-14 14:50:55 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_LINKIFIER_REMOVED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_linkifiers": realm_linkifiers,
|
|
|
|
"removed_linkifier": {
|
|
|
|
"pattern": pattern,
|
|
|
|
"url_format": url_format,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
2022-05-16 11:55:36 +02:00
|
|
|
notify_linkifiers(realm, realm_linkifiers)
|
2022-04-14 23:32:56 +02:00
|
|
|
|
|
|
|
|
2022-03-14 14:59:01 +01:00
|
|
|
@transaction.atomic(durable=True)
|
2022-03-14 12:10:25 +01:00
|
|
|
def do_update_linkifier(
|
|
|
|
realm: Realm,
|
|
|
|
id: int,
|
|
|
|
pattern: str,
|
|
|
|
url_format_string: str,
|
|
|
|
*,
|
|
|
|
acting_user: Optional[UserProfile],
|
|
|
|
) -> None:
|
2022-04-14 23:32:56 +02:00
|
|
|
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"])
|
2022-05-16 11:55:36 +02:00
|
|
|
|
|
|
|
realm_linkifiers = linkifiers_for_realm(realm.id)
|
2022-03-14 12:10:25 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_LINKIFIER_CHANGED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_linkifiers": realm_linkifiers,
|
|
|
|
"changed_linkifier": LinkifierDict(
|
|
|
|
pattern=pattern,
|
|
|
|
url_format=url_format_string,
|
|
|
|
id=linkifier.id,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
|
|
|
|
2022-05-16 11:55:36 +02:00
|
|
|
notify_linkifiers(realm, realm_linkifiers)
|