From b1344585cf8b2ea23f65597363595f801c5b26c7 Mon Sep 17 00:00:00 2001 From: Volker Date: Fri, 27 Dec 2024 20:13:01 -0600 Subject: [PATCH] Add permalink metadata to message object --- slackviewer/message.py | 13 +++++++++- slackviewer/reader.py | 24 +++++++++++++++++-- .../example_template_single_export.html | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/slackviewer/message.py b/slackviewer/message.py index 1aa6f15..5428c8f 100644 --- a/slackviewer/message.py +++ b/slackviewer/message.py @@ -9,13 +9,17 @@ class Message(object): _DEFAULT_USER_ICON_SIZE = 72 - def __init__(self, formatter, message): + def __init__(self, formatter, message, channel_id, slack_name): self._formatter = formatter self._message = message # default is False, we update it later if its a thread message self.is_thread_msg = False # used only with --since flag. Default to True, will update in the function self.is_recent_msg = True + # Channel id is not part of self._message - at least not with slackdump + self.channel_id = channel_id + # slack name that is in the url https://.slack.com + self.slack_name = slack_name def __repr__(self): message = self._message.get("text") @@ -127,6 +131,13 @@ def id(self): def subtype(self): return self._message.get("subtype") + @property + def permalink(self): + permalink = f"https://{self.slack_name}.slack.com/archives/{self.channel_id}/p{self._message['ts'].replace('.','')}" + if "thread_ts" in self._message: + permalink += f"?thread_ts={self._message['thread_ts']}&cid={self.channel_id}" + return permalink + class LinkAttachment(): """ diff --git a/slackviewer/reader.py b/slackviewer/reader.py index 5bce84d..c26ec58 100644 --- a/slackviewer/reader.py +++ b/slackviewer/reader.py @@ -5,6 +5,7 @@ import json import os import datetime +import pathlib from slackviewer.formatter import SlackFormatter from slackviewer.message import Message @@ -20,6 +21,8 @@ def __init__(self, PATH, debug, since): self._PATH = PATH self._debug = debug self._since = since + # slack name that is in the url https://.slack.com + self._slack_name = self._get_slack_name() # TODO: Make sure this works with io.open(os.path.join(self._PATH, "users.json"), encoding="utf8") as f: self.__USER_DATA = {u["id"]: User(u) for u in json.load(f)} @@ -164,6 +167,10 @@ def _create_messages(self, names, data, isDms=False): empty_dms = [] formatter = SlackFormatter(self.__USER_DATA, data) + # Channel name to channel id mapping. Needed to create a messages + # permalink when using slackdump + channel_name_to_id = {c["name"]: c["id"] for c in data.values()} + for name in names: # gets path to dm directory that holds the json archive @@ -186,7 +193,8 @@ def _create_messages(self, names, data, isDms=False): # sorts the messages in the json file day_messages.sort(key=Reader._extract_time) - messages.extend([Message(formatter, d) for d in day_messages]) + c_id = channel_name_to_id[name] + messages.extend([Message(formatter, d, c_id, self._slack_name) for d in day_messages]) chats[name] = messages chats = self._build_threads(chats) @@ -254,7 +262,7 @@ def _build_threads(self, channel_data): location = grouping[0] + 1 for reply in grouping[1]: msgtext = reply._message.get("text") - if not msgtext or not msgtext.startswith("**Thread Reply:**"): + if not msgtext or not reply.is_thread_msg: reply._message["text"] = "**Thread Reply:** {}".format(msgtext) reply.is_thread_msg = True @@ -347,3 +355,15 @@ def _message_in_timeframe(self, msg): ts_obj = datetime.datetime.fromtimestamp(float(ts)) return self._since < ts_obj + + def _get_slack_name(self): + """ + Returns the slack name that should be https://.slack.com + + Since slackdump doesn't contain the name, the function assumed that the + name of the zip file or directory is the slack name. This is a weak + assumption. + + It's name ise used for the permalink generation. + """ + return pathlib.Path(self._PATH).stem diff --git a/slackviewer/templates/example_template_single_export.html b/slackviewer/templates/example_template_single_export.html index 71db639..8233842 100644 --- a/slackviewer/templates/example_template_single_export.html +++ b/slackviewer/templates/example_template_single_export.html @@ -31,6 +31,7 @@ {%if message.user.email%} ({{message.user.email}}){%endif%}
{{ message.time }}
+
{{ message.msg|safe }} {% for attachment in message.attachments -%}