diff --git a/slack/channels.py b/slack/channels.py index c0f16b8..9f4a4f7 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -16,41 +16,25 @@ class Channel(threads.Thread): def do_fetch_info(self): return info(self.id()) + def do_fetch_messages(self, ts): + return history(self.id(), ts) + + def do_mark_messages(self, ts): + mark(self.id(), ts) + def is_member(self): return self.__member def post(self, text): chat.postMessage(self.__id, text) - def read(self): - if self.__last_ts == None: - self.fetch_info() - return history(self.__id, self.__last_ts) - def history(channel, timestamp): ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp) if ret["ok"] == False: return None - - # Read original message list - o_list = [] - for message in ret["messages"]: - o_list += [ chat.Message(message) ] - o_list.sort() - - # Merge together messages from the same user - m_list = [ o_list[0] ] - for msg in o_list[1:]: - if msg.user() == m_list[-1].user(): - m_list[-1].merge(msg) - else: - m_list += [ msg ] - - if len(m_list) > 0: - mark(channel, m_list[-1].ts()) - return m_list + return ret["messages"] def info(channel): ret = api.call("channels.info", token = auth.token(), channel = channel) diff --git a/slack/chat.py b/slack/chat.py index bbf8f60..a7319a6 100644 --- a/slack/chat.py +++ b/slack/chat.py @@ -10,11 +10,11 @@ import re -def __text_parse_uid(text): +def _text_parse_uid(text): res = text for uid in re.findall("<@(.*?)>", text): uid = uid.split("|")[0] - user = slack.users.info(uid) + user = users.info(uid) res = re.sub("<@%s(.*?)>" % uid, "@%s" % user.user(), res) return res @@ -33,7 +33,7 @@ class Message: def __str__(self): lines = [] for line in self.__text: - line = __text_parse_uid(line) + line = _text_parse_uid(line) lines += [ "" ] + textwrap.wrap(line) text = "\n ".join(lines) time = self.__time.strftime("%I:%M:%S %p") diff --git a/slack/groups.py b/slack/groups.py index 8de0ae5..663d521 100644 --- a/slack/groups.py +++ b/slack/groups.py @@ -13,6 +13,19 @@ class Group(threads.Thread): def do_fetch_info(self): return info(self.id()) + def do_fetch_messages(self, ts): + return history(self.id(), ts) + + def do_mark_messages(self, ts): + mark(self.id(), ts) + + + +def history(group, timestamp): + ret = api.call("groups.history", token = auth.token(), channel = group, oldest = timestamp) + if ret["ok"] == False: + return None + return ret["messages"] def info(group): @@ -30,3 +43,6 @@ def list(): for group in ret["groups"]: gr_list += [ Group(group) ] return gr_list + +def mark(group, timestamp): + api.call("groups.mark", token = auth.token(), channel = group, ts = timestamp) diff --git a/slack/threads.py b/slack/threads.py index 271d3d2..606224b 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -2,12 +2,15 @@ # Copyright 2015 (c) Anna Schumaker. # +from . import chat # # This class represents a slack "thread", which could be a channel, group, # or chat. Note that child classes need to implement the following methods: # -# - do_fetch_info(): Call .info to find unread count and timestamp. +# - do_fetch_info(): Call .info to find unread count and timestamp. +# - do_fetch_messages(): Call .history to find unread messages. +# - do_mark_messages(): Call .mark to set unread cursor. # class Thread: def __init__(self, json): @@ -39,6 +42,28 @@ class Thread: def name(self): return self.__name + def read(self): + if self.__last_ts == None: + self.fetch_info() + + # Read original message list + o_list = [] + for message in self.do_fetch_messages(self.__last_ts): + o_list += [ chat.Message(message) ] + o_list.sort() + + # Merge together messages from the same user + m_list = [ o_list[0] ] + for msg in o_list[1:]: + if msg.user() == m_list[-1].user(): + m_list[-1].merge(msg) + else: + m_list += [ msg ] + + self.do_mark_messages(m_list[-1].ts()) + return m_list + + def unread_count(self): if self.__unread == None: self.fetch_info()