diff options
-rw-r--r-- | _pytest/conftest.py | 4 | ||||
-rw-r--r-- | _pytest/test_process_message.py | 5 | ||||
-rw-r--r-- | _pytest/test_unfurl.py | 42 | ||||
-rw-r--r-- | wee_slack.py | 79 |
4 files changed, 94 insertions, 36 deletions
diff --git a/_pytest/conftest.py b/_pytest/conftest.py index 0cbceb9..2f90977 100644 --- a/_pytest/conftest.py +++ b/_pytest/conftest.py @@ -1,4 +1,8 @@ import pytest +import sys + +sys.path.append(str(pytest.config.rootdir)) + from wee_slack import SlackServer from wee_slack import Channel from wee_slack import User diff --git a/_pytest/test_process_message.py b/_pytest/test_process_message.py index 6c58336..a8cf9a2 100644 --- a/_pytest/test_process_message.py +++ b/_pytest/test_process_message.py @@ -33,8 +33,3 @@ def test_process_message(slack_debug, monkeypatch, myservers, mychannels, myuser print called # assert called['buffer_prnt'] == 2 # assert called['buffer_prnt_changed'] == 1 - - - - - diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py new file mode 100644 index 0000000..9af7cf2 --- /dev/null +++ b/_pytest/test_unfurl.py @@ -0,0 +1,42 @@ +import wee_slack +import pytest +import json + +slack = wee_slack + +unfurl_map = [ + { "input": "foo", + "output": "foo", + }, + { "input": "<@U2147483697|@othernick>: foo", + "output": "@testuser: foo", + "ignore_alt_text": True + }, + { "input": "foo <#C2147483705|#otherchannel> foo", + "output": "foo #otherchannel foo", + }, + { "input": "foo <#C2147483705> foo", + "output": "foo #testchan foo", + }, + { "input": "url: <https://example.com|example> suffix", + "output": "url: https://example.com (example) suffix", + }, + { "input": "url: <https://example.com|example with spaces> suffix", + "output": "url: https://example.com (example with spaces) suffix", + }, + ] + + +def test_unfurl_refs(myservers, mychannels, myusers): + slack.servers = myservers + slack.channels = mychannels + slack.users = myusers + slack.message_cache = {} + slack.servers[0].users = myusers + print mychannels[0].identifier + + for k in unfurl_map: + if "ignore_alt_text" in k: + assert slack.unfurl_refs(k["input"], ignore_alt_text=k["ignore_alt_text"]) == k["output"] + else: + assert slack.unfurl_refs(k["input"]) == k["output"] diff --git a/wee_slack.py b/wee_slack.py index f84946c..db4b178 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1465,43 +1465,60 @@ def unwrap_attachments(message_json): # attachment_text = attachment_text.encode('ascii', 'ignore') return attachment_text +def resolve_ref(ref): + if ref.startswith('@U'): + if users.find(ref[1:]): + try: + return "@{}".format(users.find(ref[1:]).name) + except: + dbg("NAME: {}".format(ref)) + elif ref.startswith('#C'): + if channels.find(ref[1:]): + try: + return "{}".format(channels.find(ref[1:]).name) + except: + dbg("CHANNEL: {}".format(ref)) + + # Something else, just return as-is + return ref + +def unfurl_ref(ref, ignore_alt_text=False): + id = ref.split('|')[0] + display_text = ref + if ref.find('|') > -1: + if ignore_alt_text: + display_text = resolve_ref(id) + else: + if id.startswith("#C") or id.startswith("@U"): + display_text = ref.split('|')[1] + else: + url, desc = ref.split('|', 1) + display_text = "{} ({})".format(url, desc) + else: + display_text = resolve_ref(ref) + return display_text def unfurl_refs(text, ignore_alt_text=False): """ Worst code ever written. this needs work """ if text and text.find('<') > -1: - newtext = [] - text = text.split(" ") - for item in text: - # dbg(item) - prefix = "" - suffix = "" - start = item.find('<') - end = item.find('>') - if start > -1 and end > -1: - prefix = item[:start] - suffix = item[end+1:] - item = item[start + 1:end] - if item.find('|') > -1: - if ignore_alt_text: - item = item.split('|')[1] - else: - item = item.split('|')[0] - if item.startswith('@U'): - if users.find(item[1:]): - try: - item = "@{}".format(users.find(item[1:]).name) - except: - dbg("NAME: {}".format(item)) - if item.startswith('#C'): - if channels.find(item[1:]): - item = "{}".format(channels.find(item[1:]).name) - newtext.append(prefix + item + suffix) - text = " ".join(newtext) - return text - else: - return text + end = 0 + newtext = "" + while text.find('<') > -1: + # Prepend prefix + newtext += text[:text.find('<')] + text = text[text.find('<'):] + end = text.find('>') + if end == -1: + newtext += text + break + # Format thingabob + newtext += unfurl_ref(text[1:end], ignore_alt_text) + text = text[end+1:] + newtext += text + return newtext + return text def get_user(message_json, server): |