Skip to content

Commit 2db7f4a

Browse files
trygveaaflashcode
authored andcommitted
slack.py 2.10.2: Merge with mainline
This is version 2.10.2 of slack.py (currently the most recent), copied over from the wee-slack repo.
1 parent 13aef99 commit 2db7f4a

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

python/slack.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
SCRIPT_NAME = "slack"
7878
SCRIPT_AUTHOR = "Trygve Aaberge <[email protected]>"
79-
SCRIPT_VERSION = "2.10.1"
79+
SCRIPT_VERSION = "2.10.2"
8080
SCRIPT_LICENSE = "MIT"
8181
SCRIPT_DESC = "Extends WeeChat for typing notification/search/etc on slack.com"
8282
REPO_URL = "https://github.com/wee-slack/wee-slack"
@@ -3402,7 +3402,7 @@ def render(self, force=False):
34023402
if blocks_rendered:
34033403
text = blocks_rendered
34043404
else:
3405-
text = self.message_json.get("text", "")
3405+
text = unhtmlescape(unfurl_refs(self.message_json.get("text", "")))
34063406

34073407
if self.message_json.get("mrkdwn", True):
34083408
text = render_formatting(text)
@@ -3412,9 +3412,7 @@ def render(self, force=False):
34123412
"group_join",
34133413
) and self.message_json.get("inviter"):
34143414
inviter_id = self.message_json.get("inviter")
3415-
text += " by invitation from <@{}>".format(inviter_id)
3416-
3417-
text = unfurl_refs(text)
3415+
text += unfurl_refs(" by invitation from <@{}>".format(inviter_id))
34183416

34193417
if self.subtype == "me_message" and not self.message_json["text"].startswith(
34203418
self.sender
@@ -3424,10 +3422,10 @@ def render(self, force=False):
34243422
if "edited" in self.message_json:
34253423
text += " " + colorize_string(config.color_edited_suffix, "(edited)")
34263424

3427-
text += unfurl_refs(unwrap_attachments(self, text))
3428-
text += unfurl_refs(unwrap_files(self, self.message_json, text))
3429-
text += unfurl_refs(unwrap_huddle(self, self.message_json, text))
3430-
text = unhtmlescape(text.lstrip().replace("\t", " "))
3425+
text += unwrap_attachments(self, text)
3426+
text += unhtmlescape(unfurl_refs(unwrap_files(self, self.message_json, text)))
3427+
text += unwrap_huddle(self, self.message_json, text)
3428+
text = text.lstrip().replace("\t", " ")
34313429

34323430
text += create_reactions_string(
34333431
self.message_json.get("reactions", ""), self.team.myidentifier
@@ -3507,17 +3505,19 @@ def get_reaction(self, reaction_name):
35073505
def add_reaction(self, reaction_name, user):
35083506
reaction = self.get_reaction(reaction_name)
35093507
if reaction:
3508+
reaction["count"] += 1
35103509
if user not in reaction["users"]:
35113510
reaction["users"].append(user)
35123511
else:
35133512
if "reactions" not in self.message_json:
35143513
self.message_json["reactions"] = []
35153514
self.message_json["reactions"].append(
3516-
{"name": reaction_name, "users": [user]}
3515+
{"name": reaction_name, "count": 1, "users": [user]}
35173516
)
35183517

35193518
def remove_reaction(self, reaction_name, user):
35203519
reaction = self.get_reaction(reaction_name)
3520+
reaction["count"] -= 1
35213521
if user in reaction["users"]:
35223522
reaction["users"].remove(user)
35233523

@@ -3553,7 +3553,7 @@ def notify_thread(self, message=None):
35533553

35543554
self.last_notify = max(message.ts, SlackTS())
35553555

3556-
if config.auto_open_threads:
3556+
if config.auto_open_threads and self.subscribed:
35573557
self.open_thread()
35583558

35593559
if message.user_identifier != self.team.myidentifier and (
@@ -3584,6 +3584,9 @@ def __init__(self, parent_channel, thread_ts, message_json, *args):
35843584
def parent_message(self):
35853585
return self.parent_channel.messages.get(self.thread_ts)
35863586

3587+
def open_thread(self, switch=False):
3588+
self.parent_message.open_thread(switch)
3589+
35873590

35883591
class Hdata(object):
35893592
def __init__(self, w):
@@ -4729,35 +4732,29 @@ def unfurl_rich_text_section(block):
47294732
colors_remove = []
47304733
characters_apply = []
47314734
characters_remove = []
4732-
if element.get("style", {}).get("bold") != prev_element.get("style", {}).get(
4733-
"bold"
4734-
):
4735-
if element.get("style", {}).get("bold"):
4735+
prev_style = prev_element.get("style", {})
4736+
cur_style = element.get("style", {})
4737+
if cur_style.get("bold", False) != prev_style.get("bold", False):
4738+
if cur_style.get("bold"):
47364739
colors_apply.append(w.color(config.render_bold_as))
47374740
characters_apply.append("*")
47384741
else:
47394742
colors_remove.append(w.color("-" + config.render_bold_as))
47404743
characters_remove.append("*")
4741-
if element.get("style", {}).get("italic") != prev_element.get("style", {}).get(
4742-
"italic"
4743-
):
4744-
if element.get("style", {}).get("italic"):
4744+
if cur_style.get("italic", False) != prev_style.get("italic", False):
4745+
if cur_style.get("italic"):
47454746
colors_apply.append(w.color(config.render_italic_as))
47464747
characters_apply.append("_")
47474748
else:
47484749
colors_remove.append(w.color("-" + config.render_italic_as))
47494750
characters_remove.append("_")
4750-
if element.get("style", {}).get("strike") != prev_element.get("style", {}).get(
4751-
"strike"
4752-
):
4753-
if element.get("style", {}).get("strike"):
4751+
if cur_style.get("strike", False) != prev_style.get("strike", False):
4752+
if cur_style.get("strike"):
47544753
characters_apply.append("~")
47554754
else:
47564755
characters_remove.append("~")
4757-
if element.get("style", {}).get("code") != prev_element.get("style", {}).get(
4758-
"code"
4759-
):
4760-
if element.get("style", {}).get("code"):
4756+
if cur_style.get("code", False) != prev_style.get("code", False):
4757+
if cur_style.get("code"):
47614758
characters_apply.append("`")
47624759
else:
47634760
characters_remove.append("`")
@@ -4779,7 +4776,7 @@ def unfurl_rich_text_section(block):
47794776

47804777
def unfurl_block_rich_text_element(element):
47814778
if element["type"] == "text":
4782-
return htmlescape(element["text"])
4779+
return element["text"]
47834780
elif element["type"] == "link":
47844781
text = element.get("text")
47854782
if text and text != element["url"]:
@@ -4791,6 +4788,10 @@ def unfurl_block_rich_text_element(element):
47914788
return element["url"]
47924789
elif element["type"] == "emoji":
47934790
return replace_string_with_emoji(":{}:".format(element["name"]))
4791+
elif element["type"] == "color":
4792+
rgb_int = int(element["value"].lstrip("#"), 16)
4793+
weechat_color = w.info_get("color_rgb2term", str(rgb_int))
4794+
return "{} {}".format(element["value"], colorize_string(weechat_color, "■"))
47944795
elif element["type"] == "user":
47954796
return resolve_ref("@{}".format(element["user_id"]))
47964797
elif element["type"] == "usergroup":
@@ -4809,9 +4810,9 @@ def unfurl_block_rich_text_element(element):
48094810

48104811
def unfurl_block_element(element):
48114812
if element["type"] == "mrkdwn":
4812-
return render_formatting(element["text"])
4813+
return render_formatting(unhtmlescape(unfurl_refs(element["text"])))
48134814
elif element["type"] == "plain_text":
4814-
return element["text"]
4815+
return unhtmlescape(unfurl_refs(element["text"]))
48154816
elif element["type"] == "image":
48164817
if element.get("alt_text"):
48174818
return "{} ({})".format(element["image_url"], element["alt_text"])
@@ -4881,7 +4882,6 @@ def unhtmlescape(text):
48814882

48824883

48834884
def unwrap_attachments(message, text_before):
4884-
text_before_unescaped = unhtmlescape(text_before)
48854885
attachment_texts = []
48864886
a = message.message_json.get("attachments")
48874887
if a:
@@ -4907,9 +4907,7 @@ def unwrap_attachments(message, text_before):
49074907
link_shown = False
49084908
title = attachment.get("title")
49094909
title_link = attachment.get("title_link", "")
4910-
if title_link and (
4911-
title_link in text_before or title_link in text_before_unescaped
4912-
):
4910+
if title_link and title_link in text_before:
49134911
title_link = ""
49144912
link_shown = True
49154913
if title and title_link:
@@ -4918,7 +4916,7 @@ def unwrap_attachments(message, text_before):
49184916
% (
49194917
prepend_title_text,
49204918
title,
4921-
title_link,
4919+
htmlescape(title_link),
49224920
)
49234921
)
49244922
prepend_title_text = ""
@@ -4932,12 +4930,8 @@ def unwrap_attachments(message, text_before):
49324930
)
49334931
prepend_title_text = ""
49344932
from_url = attachment.get("from_url", "")
4935-
if (
4936-
from_url not in text_before
4937-
and from_url not in text_before_unescaped
4938-
and from_url != title_link
4939-
):
4940-
t.append(from_url)
4933+
if from_url not in text_before and from_url != title_link:
4934+
t.append(htmlescape(from_url))
49414935
elif from_url:
49424936
link_shown = True
49434937

@@ -4947,17 +4941,13 @@ def unwrap_attachments(message, text_before):
49474941
t.append(prepend_title_text + tx)
49484942
prepend_title_text = ""
49494943

4950-
blocks = attachment.get("blocks", [])
4951-
t.extend(unfurl_blocks(blocks))
4952-
49534944
image_url = attachment.get("image_url", "")
49544945
if (
49554946
image_url not in text_before
4956-
and image_url not in text_before_unescaped
49574947
and image_url != from_url
49584948
and image_url != title_link
49594949
):
4960-
t.append(image_url)
4950+
t.append(htmlescape(image_url))
49614951
elif image_url:
49624952
link_shown = True
49634953

@@ -4971,6 +4961,11 @@ def unwrap_attachments(message, text_before):
49714961
if files:
49724962
t.append(files)
49734963

4964+
t = [unhtmlescape(unfurl_refs(x)) for x in t]
4965+
4966+
blocks = attachment.get("blocks", [])
4967+
t.extend(unfurl_blocks(blocks))
4968+
49744969
if attachment.get("is_msg_unfurl"):
49754970
channel_name = resolve_ref("#{}".format(attachment["channel_id"]))
49764971
if attachment.get("is_reply_unfurl"):
@@ -4984,14 +4979,19 @@ def unwrap_attachments(message, text_before):
49844979
ts = attachment.get("ts")
49854980
if ts:
49864981
ts_int = ts if isinstance(ts, int) else SlackTS(ts).major
4982+
if ts_int > 100000000000:
4983+
# The Slack web interface interprets very large timestamps
4984+
# as milliseconds after the epoch instead of regular Unix
4985+
# timestamps. We use the same heuristic here.
4986+
ts_int = ts_int // 1000
49874987
time_string = ""
49884988
if date.today() - date.fromtimestamp(ts_int) <= timedelta(days=1):
49894989
time_string = " at {time}"
49904990
timestamp_formatted = resolve_ref(
49914991
"!date^{}^{{date_short_pretty}}{}".format(ts_int, time_string)
49924992
).capitalize()
49934993
footer += " | {}".format(timestamp_formatted)
4994-
t.append(footer)
4994+
t.append(unhtmlescape(unfurl_refs(footer)))
49954995

49964996
fallback = attachment.get("fallback")
49974997
if t == [] and fallback and not link_shown:
@@ -5144,9 +5144,12 @@ def create_user_status_string(profile):
51445144
def create_reaction_string(reaction, myidentifier):
51455145
if config.show_reaction_nicks:
51465146
nicks = [resolve_ref("@{}".format(user)) for user in reaction["users"]]
5147-
users = "({})".format(", ".join(nicks))
5147+
nicks_extra = (
5148+
["and others"] if len(reaction["users"]) < reaction["count"] else []
5149+
)
5150+
users = "({})".format(", ".join(nicks + nicks_extra))
51485151
else:
5149-
users = len(reaction["users"])
5152+
users = reaction["count"]
51505153
reaction_string = ":{}:{}".format(reaction["name"], users)
51515154
if myidentifier in reaction["users"]:
51525155
return colorize_string(
@@ -5159,7 +5162,7 @@ def create_reaction_string(reaction, myidentifier):
51595162

51605163

51615164
def create_reactions_string(reactions, myidentifier):
5162-
reactions_with_users = [r for r in reactions if len(r["users"]) > 0]
5165+
reactions_with_users = [r for r in reactions if r["count"] > 0]
51635166
reactions_string = " ".join(
51645167
create_reaction_string(r, myidentifier) for r in reactions_with_users
51655168
)
@@ -6622,6 +6625,7 @@ def setup_hooks():
66226625
w.hook_command_run(
66236626
"/input set_unread_current_buffer", "set_unread_current_buffer_cb", ""
66246627
)
6628+
w.hook_command_run("/buffer set unread", "set_unread_current_buffer_cb", "")
66256629
w.hook_command_run("/away", "away_command_cb", "")
66266630
w.hook_command_run("/whois", "whois_command_cb", "")
66276631

0 commit comments

Comments
 (0)