aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-10-25 01:17:39 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2023-10-25 01:17:39 +0200
commit6b388cd9aea6b091e16bdd22d0a28d9f1931b284 (patch)
tree64c73dac45b8307372235731cf73527e5eaaab82
parentcf244bb2b9571b64bdd9b9e9ec3ac4e74a213190 (diff)
downloadwee-slack-6b388cd9aea6b091e16bdd22d0a28d9f1931b284.tar.gz
Correctly show reactions with more than 50 users
If a reaction has more than 50 users, the list of users will be cropped to just 50 users. This made the counts wrong since we used the number of users rather than the count property. Fix it by using the count property instead and adding "and others" to the list of users if show_reaction_nicks is enabled (the same as the Slack client shows when you hover over the reaction).
-rw-r--r--_pytest/test_render_message.py24
-rw-r--r--wee_slack.py13
2 files changed, 33 insertions, 4 deletions
diff --git a/_pytest/test_render_message.py b/_pytest/test_render_message.py
index 12b654d..f336a05 100644
--- a/_pytest/test_render_message.py
+++ b/_pytest/test_render_message.py
@@ -91,6 +91,29 @@ import wee_slack
},
{
"input_message": {
+ "text": "test",
+ "reactions": [{"name": "custom", "users": ["U01E8P3JKM1"], "count": 2}],
+ },
+ "rendered": "test <[color darkgray]>[:custom:2]<[color reset]>",
+ },
+ {
+ "input_message": {
+ "text": "test",
+ "reactions": [{"name": "custom", "users": ["U407ABLLW"], "count": 1}],
+ },
+ "rendered": "test <[color darkgray]>[:custom:(@alice)]<[color reset]>",
+ "show_reaction_nicks": True,
+ },
+ {
+ "input_message": {
+ "text": "test",
+ "reactions": [{"name": "custom", "users": ["U407ABLLW"], "count": 2}],
+ },
+ "rendered": "test <[color darkgray]>[:custom:(@alice, and others)]<[color reset]>",
+ "show_reaction_nicks": True,
+ },
+ {
+ "input_message": {
"blocks": [
{
"type": "rich_text",
@@ -144,6 +167,7 @@ import wee_slack
def test_render_message(case, channel_general):
wee_slack.EMOJI, wee_slack.EMOJI_WITH_SKIN_TONES_REVERSE = wee_slack.load_emoji()
wee_slack.config.render_emoji_as_string = case.get("render_emoji_as_string")
+ wee_slack.config.show_reaction_nicks = case.get("show_reaction_nicks", False)
message_json = {"ts": str(wee_slack.SlackTS()), **case["input_message"]}
message = wee_slack.SlackMessage("normal", message_json, channel_general)
result = message.render()
diff --git a/wee_slack.py b/wee_slack.py
index 88949d9..73723b9 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -3505,17 +3505,19 @@ class SlackMessage(object):
def add_reaction(self, reaction_name, user):
reaction = self.get_reaction(reaction_name)
if reaction:
+ reaction["count"] += 1
if user not in reaction["users"]:
reaction["users"].append(user)
else:
if "reactions" not in self.message_json:
self.message_json["reactions"] = []
self.message_json["reactions"].append(
- {"name": reaction_name, "users": [user]}
+ {"name": reaction_name, "count": 1, "users": [user]}
)
def remove_reaction(self, reaction_name, user):
reaction = self.get_reaction(reaction_name)
+ reaction["count"] -= 1
if user in reaction["users"]:
reaction["users"].remove(user)
@@ -5139,9 +5141,12 @@ def create_user_status_string(profile):
def create_reaction_string(reaction, myidentifier):
if config.show_reaction_nicks:
nicks = [resolve_ref("@{}".format(user)) for user in reaction["users"]]
- users = "({})".format(", ".join(nicks))
+ nicks_extra = (
+ ["and others"] if len(reaction["users"]) < reaction["count"] else []
+ )
+ users = "({})".format(", ".join(nicks + nicks_extra))
else:
- users = len(reaction["users"])
+ users = reaction["count"]
reaction_string = ":{}:{}".format(reaction["name"], users)
if myidentifier in reaction["users"]:
return colorize_string(
@@ -5154,7 +5159,7 @@ def create_reaction_string(reaction, myidentifier):
def create_reactions_string(reactions, myidentifier):
- reactions_with_users = [r for r in reactions if len(r["users"]) > 0]
+ reactions_with_users = [r for r in reactions if r["count"] > 0]
reactions_string = " ".join(
create_reaction_string(r, myidentifier) for r in reactions_with_users
)