diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2020-02-19 22:06:06 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2020-02-20 22:58:56 +0100 |
commit | 5b40e22b1a6d8cd44f72195d3a15338e72fe2ec7 (patch) | |
tree | 2dd8c111a76054eefde2078694fcc11ee2ab0358 | |
parent | cc6459415bbf117353564cc709da0343434dcf23 (diff) | |
download | wee-slack-5b40e22b1a6d8cd44f72195d3a15338e72fe2ec7.tar.gz |
Only use fallback for refs if ref is not found
Fallbacks in messages are not changed when channels/users/groups are
renamed, so they shouldn't be used unless the ref can't be found. Links
are not refs we look up, so they are still treated the same way.
-rw-r--r-- | _pytest/test_unfurl.py | 34 | ||||
-rw-r--r-- | wee_slack.py | 58 |
2 files changed, 53 insertions, 39 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py index 6178bbb..fe1fc3d 100644 --- a/_pytest/test_unfurl.py +++ b/_pytest/test_unfurl.py @@ -28,23 +28,38 @@ import pytest { 'input': "<@U407ABLLW|@othernick>: foo", 'output': "@alice: foo", - 'ignore_alt_text': True, + }, + { + 'input': "<@UNKNOWN|@othernick>: foo", + 'output': "@othernick: foo", }, { 'input': "foo <#C407ABS94|otherchannel> foo", + 'output': "foo #general foo", + }, + { + 'input': "foo <#UNKNOWN|otherchannel> foo", 'output': "foo #otherchannel foo", }, { - 'input': "foo <#C407ABS94> foo", - 'output': "foo #general foo", + 'input': "url: <https://example.com|fallback> suffix", + 'output': "url: https://example.com suffix", + 'ignore_alt_text': True, }, { 'input': "url: <https://example.com|example> suffix", 'output': "url: https://example.com (example) suffix", + 'auto_link_display': 'both', }, { 'input': "url: <https://example.com|example with spaces> suffix", 'output': "url: https://example.com (example with spaces) suffix", + 'auto_link_display': 'both', + }, + { + 'input': "url: <https://example.com|example.com> suffix", + 'output': "url: https://example.com (example.com) suffix", + 'auto_link_display': 'both', }, { 'input': "url: <https://example.com|example.com> suffix", @@ -67,8 +82,9 @@ import pytest 'auto_link_display': 'url', }, { - 'input': "<@U407ABLLW|@othernick> multiple unfurl <https://example.com|example with spaces>", - 'output': "@othernick multiple unfurl https://example.com (example with spaces)", + 'input': "<@U407ABLLW> multiple unfurl <https://example.com|example with spaces>", + 'output': "@alice multiple unfurl https://example.com (example with spaces)", + 'auto_link_display': 'both', }, { 'input': "try the #general channel", @@ -79,8 +95,12 @@ import pytest 'output': "@alice I think 3 > 2", }, { - 'input': "<!subteam^U407ABLLW|@dev> This is announcement for the dev team", - 'output': "@dev This is announcement for the dev team" + 'input': "<!subteam^TGX0ALBK3|@othersubteam> This is announcement for the dev team", + 'output': "@test This is announcement for the dev team" + }, + { + 'input': "<!subteam^UNKNOWN|@othersubteam> This is announcement for the dev team", + 'output': "@othersubteam This is announcement for the dev team" } )) def test_unfurl_refs(case, realish_eventrouter): diff --git a/wee_slack.py b/wee_slack.py index b081a03..0e08ef5 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -3434,38 +3434,32 @@ def unfurl_refs(text): # Test patterns lives in ./_pytest/test_unfurl.py def unfurl_ref(match): - ref = match.group(1) - id = ref.split('|')[0] - display_text = ref - if ref.find('|') > -1: - if config.unfurl_ignore_alt_text: - display_text = resolve_ref(id) + ref, fallback = match.groups() + + resolved_ref = resolve_ref(ref) + if resolved_ref != ref: + return resolved_ref + + if fallback and not config.unfurl_ignore_alt_text: + if ref.startswith("#"): + return "#{}".format(fallback) + elif ref.startswith("@"): + return fallback + elif ref.startswith("!subteam"): + prefix = "@" if not fallback.startswith("@") else "" + return prefix + fallback else: - if id.startswith("#"): - display_text = "#{}".format(ref.split('|')[1]) - elif id.startswith("@"): - display_text = ref.split('|')[1] - elif id.startswith("!subteam"): - if ref.split('|')[1].startswith('@'): - handle = ref.split('|')[1][1:] - else: - handle = ref.split('|')[1] - display_text = '@{}'.format(handle) + match_url = r"^\w+:(//)?{}$".format(re.escape(fallback)) + url_matches_desc = re.match(match_url, ref) + if url_matches_desc and config.unfurl_auto_link_display == "text": + return fallback + elif url_matches_desc and config.unfurl_auto_link_display == "url": + return ref else: - url, desc = ref.split('|', 1) - match_url = r"^\w+:(//)?{}$".format(re.escape(desc)) - url_matches_desc = re.match(match_url, url) - if url_matches_desc and config.unfurl_auto_link_display == "text": - display_text = desc - elif url_matches_desc and config.unfurl_auto_link_display == "url": - display_text = url - else: - display_text = "{} ({})".format(url, desc) - else: - display_text = resolve_ref(ref) - return display_text + return "{} ({})".format(ref, fallback) + return ref - return re.sub(r"<([@#!]?[^>]*)>", unfurl_ref, text) + return re.sub(r"<([^|>]*)(?:\|([^>]*))?>", unfurl_ref, text) def unhtmlescape(text): @@ -3549,10 +3543,10 @@ def unwrap_files(message_json, text_before): def resolve_ref(ref): + if ref in ['!channel', '!everyone', '!group', '!here']: + return ref.replace('!', '@') for team in EVENTROUTER.teams.values(): - if ref in ['!channel', '!everyone', '!group', '!here']: - return ref.replace('!', '@') - elif ref.startswith('@'): + if ref.startswith('@'): user = team.users.get(ref[1:]) if user: suffix = config.external_user_suffix if user.is_external else '' |