Skip to content

Commit

Permalink
Add permalink metadata to message object
Browse files Browse the repository at this point in the history
  • Loading branch information
volker-fr committed Dec 28, 2024
1 parent b5dfec0 commit b1f76a6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
13 changes: 12 additions & 1 deletion slackviewer/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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://<slackname>.slack.com
self.slack_name = slack_name

def __repr__(self):
message = self._message.get("text")
Expand Down Expand Up @@ -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():
"""
Expand Down
24 changes: 22 additions & 2 deletions slackviewer/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
import datetime
import pathlib

from slackviewer.formatter import SlackFormatter
from slackviewer.message import Message
Expand All @@ -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://<slackname>.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)}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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://<slackname>.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
9 changes: 1 addition & 8 deletions slackviewer/templates/util.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
<div class="reply">
{% endif %}
{% if not no_external_references and not message.is_thread_msg %}
{% if message.img %}<img src="{{ message.img }}" class="user_icon" />{%else%}<div class="user_icon"></div>{%endif%}
{% elif not no_external_references and message.is_thread_msg %}
{% if message.img %}<img src="{{ message.img }}" class="user_icon_reply" />{%else%}<div class="user_icon_reply"></div>{%endif%}
{% endif %}
<div class="username">{{ message.username }}
{%if message.user.email%} <span class="print-only user-email">({{message.user.email}})</span>{%endif%}
</div>
<a href="#{{ message.id}}"><div class="time">{{ message.time }}</div></a>
<a href="{{ message.permalink }}">permalink</a>
<div class="msg">
{{ message.msg|safe }}
{% for attachment in message.attachments -%}
Expand Down Expand Up @@ -80,11 +78,6 @@
{%endif%}
</div>
{% endfor %}
{% for reaction in message.reactions %}
<div class="message-reaction">
{{ reaction.name }} {{ reaction.usernames|join(', ') }}
</div>
{% endfor %}
</div>
</div>
</div>
Expand Down

0 comments on commit b1f76a6

Please sign in to comment.