From a2d385bbdb7d6f2c1d7c8c9bdf9548832d8965a3 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Mon, 19 Feb 2024 16:24:51 +0100 Subject: Add command `/slack mute` --- slack/commands.py | 32 +++++++++++++++++++++++++++++++- slack/slack_api.py | 8 ++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/slack/commands.py b/slack/commands.py index dbbf024..e066fa4 100644 --- a/slack/commands.py +++ b/slack/commands.py @@ -31,7 +31,7 @@ from slack.slack_conversation import SlackConversation from slack.slack_thread import SlackThread from slack.slack_user import SlackUser, get_user_nick, name_from_user_info from slack.slack_workspace import SlackWorkspace -from slack.task import Future, run_async, sleep +from slack.task import Future, gather, run_async, sleep from slack.util import get_callback_name, with_color from slack.weechat_config import WeeChatOption, WeeChatOptionTypes @@ -434,6 +434,36 @@ async def command_slack_presence(buffer: str, args: List[str], options: Options) await slack_buffer.workspace.api.set_presence(new_presence) +@weechat_command("list") +async def command_slack_mute(buffer: str, args: List[str], options: Options): + slack_buffer = shared.buffers.get(buffer) + if not isinstance(slack_buffer, SlackConversation): + return + + if args[0] == "list": + conversations = await gather( + *[ + slack_buffer.workspace.conversations[conversation_id] + for conversation_id in slack_buffer.workspace.muted_channels + ] + ) + conversation_names = sorted( + conversation.name_with_prefix("short_name_without_padding") + for conversation in conversations + ) + weechat.prnt("", f"Muted conversations: {', '.join(conversation_names)}") + return + + muted_channels = set(slack_buffer.workspace.muted_channels) + muted_channels ^= {slack_buffer.id} + await slack_buffer.workspace.api.set_muted_channels(muted_channels) + muted_str = "Muted" if slack_buffer.id in muted_channels else "Unmuted" + weechat.prnt( + "", + f"{muted_str} channel {slack_buffer.name_with_prefix('short_name_without_padding')}", + ) + + def print_uncaught_error(error: UncaughtError, detailed: bool, options: Options): weechat.prnt("", f" {error.id} ({error.time}): {error.exception}") if detailed: diff --git a/slack/slack_api.py b/slack/slack_api.py index f70e2f4..5c6a230 100644 --- a/slack/slack_api.py +++ b/slack/slack_api.py @@ -479,6 +479,14 @@ class SlackApi(SlackApiCommon): raise SlackApiError(self.workspace, method, response, params) return response + async def set_muted_channels(self, channel_ids: Iterable[str]): + method = "users.prefs.set" + params: Params = {"name": "muted_channels", "value": ",".join(channel_ids)} + response: SlackGenericResponse = await self._fetch(method, params) + if response["ok"] is False: + raise SlackApiError(self.workspace, method, response, params) + return response + async def _set_user_profile(self, profile: SlackSetProfile): method = "users.profile.set" body = {"profile": profile} -- cgit