aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-02-19 21:53:00 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-20 13:21:38 +0100
commit23d41cfb9b70f2596436e27f69a8865efe32da8a (patch)
tree5546bbd48a4ece39be044221386d6190af0dc478
parentb9e67bba4d4021f3400275c033b42fa8ffd1a476 (diff)
downloadwee-slack-23d41cfb9b70f2596436e27f69a8865efe32da8a.tar.gz
Add command `/slack linkarchive`
-rw-r--r--slack/commands.py59
-rw-r--r--slack/slack_workspace.py2
2 files changed, 61 insertions, 0 deletions
diff --git a/slack/commands.py b/slack/commands.py
index 52a3e03..7a336a4 100644
--- a/slack/commands.py
+++ b/slack/commands.py
@@ -27,6 +27,7 @@ from slack.python_compatibility import format_exception, removeprefix
from slack.shared import shared
from slack.slack_buffer import SlackBuffer
from slack.slack_conversation import SlackConversation
+from slack.slack_message import SlackTs
from slack.slack_thread import SlackThread
from slack.slack_user import SlackUser
from slack.slack_workspace import SlackWorkspace
@@ -46,6 +47,16 @@ if TYPE_CHECKING:
T = TypeVar("T")
+def print_message_not_found_error(msg_id: str):
+ if msg_id:
+ print_error(
+ "Invalid id given, must be an existing id or a number greater "
+ + "than 0 and less than the number of messages in the channel"
+ )
+ else:
+ print_error("No messages found in channel")
+
+
# def parse_help_docstring(cmd):
# doc = textwrap.dedent(cmd.__doc__).strip().split("\n", 1)
# cmd_line = doc[0].split(None, 1)
@@ -535,6 +546,54 @@ async def command_slack_status(buffer: str, args: List[str], options: Options):
print_error("Run the command in a slack buffer")
+def _get_conversation_from_buffer(
+ slack_buffer: SlackBuffer,
+) -> Optional[SlackConversation]:
+ if isinstance(slack_buffer, SlackConversation):
+ return slack_buffer
+ elif isinstance(slack_buffer, SlackThread):
+ return slack_buffer.parent.conversation
+ return None
+
+
+def _get_linkarchive_url(
+ slack_buffer: SlackBuffer, message_ts: Optional[SlackTs]
+) -> str:
+ url = f"https://{slack_buffer.workspace.domain}.slack.com/"
+ conversation = _get_conversation_from_buffer(slack_buffer)
+ if conversation is not None:
+ url += f"archives/{conversation.id}/"
+ if message_ts is not None:
+ message = conversation.messages[message_ts]
+ url += f"p{message.ts.major}{message.ts.minor:0>6}"
+ if message.thread_ts is not None:
+ url += f"?thread_ts={message.thread_ts}&cid={conversation.id}"
+ return url
+
+
+@weechat_command("%(threads)")
+def command_slack_linkarchive(buffer: str, args: List[str], options: Options):
+ """
+ /slack linkarchive [message_id]
+ Place a link to the conversation or message in the input bar.
+ Use cursor or mouse mode to get the id.
+ """
+ slack_buffer = shared.buffers.get(buffer)
+ if slack_buffer is None:
+ return
+
+ if args[0]:
+ ts = slack_buffer.ts_from_hash_or_index(args[0])
+ if ts is None:
+ print_message_not_found_error(args[0])
+ return
+ else:
+ ts = None
+
+ url = _get_linkarchive_url(slack_buffer, ts)
+ weechat.command(buffer, f"/input insert {url}")
+
+
def find_command(start_cmd: str, args: str) -> Optional[Tuple[Command, str]]:
args_parts = re.finditer("[^ ]+", args)
cmd = start_cmd
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
index 41e4a21..faa0d73 100644
--- a/slack/slack_workspace.py
+++ b/slack/slack_workspace.py
@@ -261,6 +261,7 @@ class SlackWorkspace:
async def _connect_oauth(self) -> List[SlackConversation]:
rtm_connect = await self.api.fetch_rtm_connect()
self.id = rtm_connect["team"]["id"]
+ self.domain = rtm_connect["team"]["domain"]
self.my_user = await self.users[rtm_connect["self"]["id"]]
await self._connect_ws(rtm_connect["url"])
@@ -298,6 +299,7 @@ class SlackWorkspace:
if "enterprise_id" in team_info["team"]
else None
)
+ self.domain = team_info["team"]["domain"]
my_user_id = user_boot["self"]["id"]
# self.users.initialize_items(my_user_id, {my_user_id: user_boot["self"]})
self.my_user = await self.users[my_user_id]