diff --git a/slack/channels.py b/slack/channels.py index 1b497c6..cf8a69a 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -3,32 +3,21 @@ # from . import api from . import auth -from . import chat from . import threads -from . import users class Channel(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) self.api_history = "channels.history" + self.api_info = { "call" : "channels.info", "ret" : "channel" } self.api_mark = "channels.mark" self.__member = json.get("is_member", False) - def do_fetch_info(self): - return info(self.id()) - def is_member(self): return self.__member - -def info(channel): - ret = api.call("channels.info", token = auth.token(), channel = channel) - if ret["ok"] == False: - return None - return ret["channel"] - def list(): ret = api.call("channels.list", token = auth.token()) if ret["ok"] == False: diff --git a/slack/groups.py b/slack/groups.py index 949fd03..f557137 100644 --- a/slack/groups.py +++ b/slack/groups.py @@ -10,18 +10,9 @@ class Group(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) self.api_history = "groups.history" + self.api_info = { "call" : "groups.info", "ret" : "group" } self.api_mark = "groups.mark" - def do_fetch_info(self): - return info(self.id()) - - - -def info(group): - ret = api.call("groups.info", token = auth.token(), channel = group) - if ret["ok"] == False: - return None - return ret["group"] def list(): ret = api.call("groups.list", token = auth.token()) diff --git a/slack/im.py b/slack/im.py index 1a72411..271f1a9 100644 --- a/slack/im.py +++ b/slack/im.py @@ -10,6 +10,7 @@ class IM(threads.Thread): def __init__(self, json): threads.Thread.__init__(self, json) self.api_history = "im.history" + self.api_info = { "call" : "im.open", "ret" : "channel" } self.api_mark = "im.mark" self.__uid = json.get("user") self.__user = users.info(self.__uid) @@ -20,17 +21,11 @@ class IM(threads.Thread): def url(self, base): return "%s/messages/@%s/" % (base, self.__user.user()) - def do_fetch_info(self): - return info(self.id(), self.__uid) + def call_fetch_info(self): + return api.call(self.api_info["call"], token = auth.token(), + user = self.__uid, return_im = True) - -def info(im, uid): - ret = api.call("im.open", token = auth.token(), user = uid, return_im = True) - if ret["ok"] == False: - return None - return ret["channel"] - def list(): ret = api.call("im.list", token = auth.token()) if ret["ok"] == False: diff --git a/slack/threads.py b/slack/threads.py index eaa74e5..cfc15ef 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -9,7 +9,7 @@ from . import chat # This class represents a slack "thread", which could be a channel, group, # or chat. Note that child classes need to define the following variables: # -# - do_fetch_info(): Call .info to find unread count and timestamp. +# - self.api_info: The slack api method and return code to find channel info # - self.api_history: The slack api method to find unread messages # - self.api_mark: Call .mark to set unread cursor. # @@ -28,12 +28,16 @@ class Thread: topic = "\nTopic: %s" % self.__topic return "%s: %s%s" % (self.__name, self.__purpose, topic) + def call_fetch_info(self): + return api.call(self.api_info["call"], token = auth.token(), + channel = self.__id) + def fetch_info(self): - json = self.do_fetch_info() - if json != None: - self.__unread = json.get("unread_count", None) - self.__last_ts = json.get("last_read", None) - self.__id = json.get("id", 0) + json = self.call_fetch_info() + if json["ok"] == True: + self.__unread = json[self.api_info["ret"]].get("unread_count", None) + self.__last_ts = json[self.api_info["ret"]].get("last_read", None) + self.__id = json[self.api_info["ret"]].get("id", 0) def fetch_messages(self): json = api.call(self.api_history, token = auth.token(),