mirror of
https://github.com/zulip/zulip.git
synced 2025-11-07 07:23:22 +00:00
notifications: Extract zerver/lib/url_encoding.py.
Extracting this helper library will help us avoid an import loop between notifications.py and message.py (with bugdown in between). But in addition to that, it's a more natural model, since some of the uses for these functions weren't part of the notifications code anyway.
This commit is contained in:
@@ -35,7 +35,7 @@ from zerver.lib.camo import get_camo_url
|
|||||||
from zerver.lib.emoji import translate_emoticons, emoticon_regex
|
from zerver.lib.emoji import translate_emoticons, emoticon_regex
|
||||||
from zerver.lib.mention import possible_mentions, \
|
from zerver.lib.mention import possible_mentions, \
|
||||||
possible_user_group_mentions, extract_user_group
|
possible_user_group_mentions, extract_user_group
|
||||||
from zerver.lib.notifications import encode_stream
|
from zerver.lib.url_encoding import encode_stream
|
||||||
from zerver.lib.thumbnail import is_thumbor_enabled
|
from zerver.lib.thumbnail import is_thumbor_enabled
|
||||||
from zerver.lib.timeout import timeout, TimeoutExpired
|
from zerver.lib.timeout import timeout, TimeoutExpired
|
||||||
from zerver.lib.cache import cache_with_key, NotFoundInCache
|
from zerver.lib.cache import cache_with_key, NotFoundInCache
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ from django.template import loader
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.timezone import now as timezone_now
|
from django.utils.timezone import now as timezone_now
|
||||||
|
|
||||||
from zerver.lib.notifications import build_message_list, encode_stream, \
|
from zerver.lib.notifications import build_message_list, one_click_unsubscribe_link
|
||||||
one_click_unsubscribe_link
|
|
||||||
from zerver.lib.send_email import send_future_email, FromAddress
|
from zerver.lib.send_email import send_future_email, FromAddress
|
||||||
|
from zerver.lib.url_encoding import encode_stream
|
||||||
from zerver.models import UserProfile, UserMessage, Recipient, Stream, \
|
from zerver.models import UserProfile, UserMessage, Recipient, Stream, \
|
||||||
Subscription, UserActivity, get_active_streams, get_user_profile_by_id, \
|
Subscription, UserActivity, get_active_streams, get_user_profile_by_id, \
|
||||||
Realm
|
Realm
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from django.utils.translation import ugettext as _
|
|||||||
from zerver.decorator import statsd_increment
|
from zerver.decorator import statsd_increment
|
||||||
from zerver.lib.send_email import send_future_email, FromAddress
|
from zerver.lib.send_email import send_future_email, FromAddress
|
||||||
from zerver.lib.queue import queue_json_publish
|
from zerver.lib.queue import queue_json_publish
|
||||||
|
from zerver.lib.url_encoding import pm_narrow_url, stream_narrow_url, topic_narrow_url
|
||||||
from zerver.models import (
|
from zerver.models import (
|
||||||
Recipient,
|
Recipient,
|
||||||
ScheduledEmail,
|
ScheduledEmail,
|
||||||
@@ -31,7 +32,6 @@ import lxml.html
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import ujson
|
import ujson
|
||||||
import urllib
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
@@ -44,33 +44,6 @@ def one_click_unsubscribe_link(user_profile: UserProfile, email_type: str) -> st
|
|||||||
Confirmation.UNSUBSCRIBE,
|
Confirmation.UNSUBSCRIBE,
|
||||||
url_args = {'email_type': email_type})
|
url_args = {'email_type': email_type})
|
||||||
|
|
||||||
def hash_util_encode(string: str) -> str:
|
|
||||||
# Do the same encoding operation as hash_util.encodeHashComponent on the
|
|
||||||
# frontend.
|
|
||||||
# `safe` has a default value of "/", but we want those encoded, too.
|
|
||||||
return urllib.parse.quote(
|
|
||||||
string.encode("utf-8"), safe=b"").replace(".", "%2E").replace("%", ".")
|
|
||||||
|
|
||||||
def encode_stream(stream_id: int, stream_name: str) -> str:
|
|
||||||
# We encode streams for urls as something like 99-Verona.
|
|
||||||
stream_name = stream_name.replace(' ', '-')
|
|
||||||
return str(stream_id) + '-' + hash_util_encode(stream_name)
|
|
||||||
|
|
||||||
def pm_narrow_url(realm: Realm, participants: List[str]) -> str:
|
|
||||||
participants.sort()
|
|
||||||
base_url = "%s/#narrow/pm-with/" % (realm.uri,)
|
|
||||||
return base_url + hash_util_encode(",".join(participants))
|
|
||||||
|
|
||||||
def stream_narrow_url(realm: Realm, stream: Stream) -> str:
|
|
||||||
base_url = "%s/#narrow/stream/" % (realm.uri,)
|
|
||||||
return base_url + encode_stream(stream.id, stream.name)
|
|
||||||
|
|
||||||
def topic_narrow_url(realm: Realm, stream: Stream, topic: str) -> str:
|
|
||||||
base_url = "%s/#narrow/stream/" % (realm.uri,)
|
|
||||||
return "%s%s/topic/%s" % (base_url,
|
|
||||||
encode_stream(stream.id, stream.name),
|
|
||||||
hash_util_encode(topic))
|
|
||||||
|
|
||||||
def relative_to_full_url(base_url: str, content: str) -> str:
|
def relative_to_full_url(base_url: str, content: str) -> str:
|
||||||
# Convert relative URLs to absolute URLs.
|
# Convert relative URLs to absolute URLs.
|
||||||
fragment = lxml.html.fromstring(content)
|
fragment = lxml.html.fromstring(content)
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ from django.utils.translation import ugettext as _
|
|||||||
from zerver.models import Realm, UserProfile, get_user_profile_by_id, get_client, \
|
from zerver.models import Realm, UserProfile, get_user_profile_by_id, get_client, \
|
||||||
GENERIC_INTERFACE, Service, SLACK_INTERFACE, email_to_domain, get_service_profile
|
GENERIC_INTERFACE, Service, SLACK_INTERFACE, email_to_domain, get_service_profile
|
||||||
from zerver.lib.actions import check_send_message
|
from zerver.lib.actions import check_send_message
|
||||||
from zerver.lib.notifications import encode_stream
|
|
||||||
from zerver.lib.queue import retry_event
|
from zerver.lib.queue import retry_event
|
||||||
|
from zerver.lib.url_encoding import encode_stream
|
||||||
from zerver.lib.validator import check_dict, check_string
|
from zerver.lib.validator import check_dict, check_string
|
||||||
from zerver.decorator import JsonableError
|
from zerver.decorator import JsonableError
|
||||||
|
|
||||||
|
|||||||
31
zerver/lib/url_encoding.py
Normal file
31
zerver/lib/url_encoding.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import urllib
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from zerver.models import Realm, Stream
|
||||||
|
|
||||||
|
def hash_util_encode(string: str) -> str:
|
||||||
|
# Do the same encoding operation as hash_util.encodeHashComponent on the
|
||||||
|
# frontend.
|
||||||
|
# `safe` has a default value of "/", but we want those encoded, too.
|
||||||
|
return urllib.parse.quote(
|
||||||
|
string.encode("utf-8"), safe=b"").replace(".", "%2E").replace("%", ".")
|
||||||
|
|
||||||
|
def encode_stream(stream_id: int, stream_name: str) -> str:
|
||||||
|
# We encode streams for urls as something like 99-Verona.
|
||||||
|
stream_name = stream_name.replace(' ', '-')
|
||||||
|
return str(stream_id) + '-' + hash_util_encode(stream_name)
|
||||||
|
|
||||||
|
def pm_narrow_url(realm: Realm, participants: List[str]) -> str:
|
||||||
|
participants.sort()
|
||||||
|
base_url = "%s/#narrow/pm-with/" % (realm.uri,)
|
||||||
|
return base_url + hash_util_encode(",".join(participants))
|
||||||
|
|
||||||
|
def stream_narrow_url(realm: Realm, stream: Stream) -> str:
|
||||||
|
base_url = "%s/#narrow/stream/" % (realm.uri,)
|
||||||
|
return base_url + encode_stream(stream.id, stream.name)
|
||||||
|
|
||||||
|
def topic_narrow_url(realm: Realm, stream: Stream, topic: str) -> str:
|
||||||
|
base_url = "%s/#narrow/stream/" % (realm.uri,)
|
||||||
|
return "%s%s/topic/%s" % (base_url,
|
||||||
|
encode_stream(stream.id, stream.name),
|
||||||
|
hash_util_encode(topic))
|
||||||
Reference in New Issue
Block a user