Implement a single fetch_info() function

This already existed, so I just had to define the api calls to use.
Unfortunately, groups return a similar group object so I also have to
define what return object needs to be looked up.  Additionally, IMs have
an extra "return_im" object that needs to be passed to get the full
channel info.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-11-22 09:45:12 -05:00
parent 53d9aa4e3d
commit 1e767dbfe9
4 changed files with 16 additions and 37 deletions

View File

@ -3,32 +3,21 @@
# #
from . import api from . import api
from . import auth from . import auth
from . import chat
from . import threads from . import threads
from . import users
class Channel(threads.Thread): class Channel(threads.Thread):
def __init__(self, json): def __init__(self, json):
threads.Thread.__init__(self, json) threads.Thread.__init__(self, json)
self.api_history = "channels.history" self.api_history = "channels.history"
self.api_info = { "call" : "channels.info", "ret" : "channel" }
self.api_mark = "channels.mark" self.api_mark = "channels.mark"
self.__member = json.get("is_member", False) self.__member = json.get("is_member", False)
def do_fetch_info(self):
return info(self.id())
def is_member(self): def is_member(self):
return self.__member 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(): def list():
ret = api.call("channels.list", token = auth.token()) ret = api.call("channels.list", token = auth.token())
if ret["ok"] == False: if ret["ok"] == False:

View File

@ -10,18 +10,9 @@ class Group(threads.Thread):
def __init__(self, json): def __init__(self, json):
threads.Thread.__init__(self, json) threads.Thread.__init__(self, json)
self.api_history = "groups.history" self.api_history = "groups.history"
self.api_info = { "call" : "groups.info", "ret" : "group" }
self.api_mark = "groups.mark" 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(): def list():
ret = api.call("groups.list", token = auth.token()) ret = api.call("groups.list", token = auth.token())

View File

@ -10,6 +10,7 @@ class IM(threads.Thread):
def __init__(self, json): def __init__(self, json):
threads.Thread.__init__(self, json) threads.Thread.__init__(self, json)
self.api_history = "im.history" self.api_history = "im.history"
self.api_info = { "call" : "im.open", "ret" : "channel" }
self.api_mark = "im.mark" self.api_mark = "im.mark"
self.__uid = json.get("user") self.__uid = json.get("user")
self.__user = users.info(self.__uid) self.__user = users.info(self.__uid)
@ -20,17 +21,11 @@ class IM(threads.Thread):
def url(self, base): def url(self, base):
return "%s/messages/@%s/" % (base, self.__user.user()) return "%s/messages/@%s/" % (base, self.__user.user())
def do_fetch_info(self): def call_fetch_info(self):
return info(self.id(), self.__uid) 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(): def list():
ret = api.call("im.list", token = auth.token()) ret = api.call("im.list", token = auth.token())
if ret["ok"] == False: if ret["ok"] == False:

View File

@ -9,7 +9,7 @@ from . import chat
# This class represents a slack "thread", which could be a channel, group, # This class represents a slack "thread", which could be a channel, group,
# or chat. Note that child classes need to define the following variables: # or chat. Note that child classes need to define the following variables:
# #
# - do_fetch_info(): Call <whatever>.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_history: The slack api method to find unread messages
# - self.api_mark: Call <whatever>.mark to set unread cursor. # - self.api_mark: Call <whatever>.mark to set unread cursor.
# #
@ -28,12 +28,16 @@ class Thread:
topic = "\nTopic: %s" % self.__topic topic = "\nTopic: %s" % self.__topic
return "%s: %s%s" % (self.__name, self.__purpose, 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): def fetch_info(self):
json = self.do_fetch_info() json = self.call_fetch_info()
if json != None: if json["ok"] == True:
self.__unread = json.get("unread_count", None) self.__unread = json[self.api_info["ret"]].get("unread_count", None)
self.__last_ts = json.get("last_read", None) self.__last_ts = json[self.api_info["ret"]].get("last_read", None)
self.__id = json.get("id", 0) self.__id = json[self.api_info["ret"]].get("id", 0)
def fetch_messages(self): def fetch_messages(self):
json = api.call(self.api_history, token = auth.token(), json = api.call(self.api_history, token = auth.token(),