diff --git a/slack/channels.py b/slack/channels.py index ef5401f..d6f4e87 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -11,14 +11,12 @@ from . import users class Channel(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) - self.__member = json.get("is_member", False) + self.api_history = "channels.history" + self.__member = json.get("is_member", False) 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) @@ -27,12 +25,6 @@ class Channel(threads.Thread): -def history(channel, timestamp): - ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp) - if ret["ok"] == False: - return None - return ret["messages"] - def info(channel): ret = api.call("channels.info", token = auth.token(), channel = channel) if ret["ok"] == False: diff --git a/slack/groups.py b/slack/groups.py index 663d521..94d6fd9 100644 --- a/slack/groups.py +++ b/slack/groups.py @@ -9,25 +9,16 @@ from . import threads class Group(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) + self.api_history = "groups.history" 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): ret = api.call("groups.info", token = auth.token(), channel = group) if ret["ok"] == False: diff --git a/slack/im.py b/slack/im.py index d3458ab..055fc4c 100644 --- a/slack/im.py +++ b/slack/im.py @@ -9,8 +9,9 @@ from . import users class IM(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) - self.__uid = json.get("user") - self.__user = users.info(self.__uid) + self.api_history = "im.history" + self.__uid = json.get("user") + self.__user = users.info(self.__uid) def name(self): return "Chat With %s" % self.__user.name() @@ -21,20 +22,11 @@ class IM(threads.Thread): def do_fetch_info(self): return info(self.id(), self.__uid) - def do_fetch_messages(self, ts): - return history(self.id(), ts) - def do_mark_messages(self, ts): mark(self.id(), ts) -def history(im, timestamp): - ret = api.call("im.history", token = auth.token(), channel = im, oldest = timestamp) - if ret["ok"] == False: - return None - return ret["messages"] - def info(im, uid): ret = api.call("im.open", token = auth.token(), user = uid, return_im = True) if ret["ok"] == False: diff --git a/slack/threads.py b/slack/threads.py index 250b5c1..d9c607f 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -1,15 +1,16 @@ # # Copyright 2015 (c) Anna Schumaker. # - +from . import api +from . import auth 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: +# or chat. Note that child classes need to define the following variables: # # - do_fetch_info(): Call .info to find unread count and timestamp. -# - do_fetch_messages(): Call .history to find unread messages. +# - self.api_history: The slack api method to find unread messages # - do_mark_messages(): Call .mark to set unread cursor. # class Thread: @@ -34,6 +35,13 @@ class Thread: self.__last_ts = json.get("last_read", None) self.__id = json.get("id", 0) + def fetch_messages(self): + json = api.call(self.api_history, token = auth.token(), + channel = self.__id, oldest = self.__last_ts) + if json["ok"] == False: + return None + return json["messages"] + def id(self): return self.__id @@ -51,7 +59,7 @@ class Thread: self.fetch_info() # Read original message list - messages = self.do_fetch_messages(self.__last_ts) + messages = self.fetch_messages() o_list = [ chat.Message(msg) for msg in messages ] o_list.sort() if len(o_list) == 0: