Implement a single fetch_messages() function
Rather than having each thread type define its own fetch history method, let's instead have them tell us what api call to use to avoid duplicating work Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
a88669d5e4
commit
f517f8c391
|
@ -11,14 +11,12 @@ 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.__member = json.get("is_member", False)
|
self.api_history = "channels.history"
|
||||||
|
self.__member = json.get("is_member", False)
|
||||||
|
|
||||||
def do_fetch_info(self):
|
def do_fetch_info(self):
|
||||||
return info(self.id())
|
return info(self.id())
|
||||||
|
|
||||||
def do_fetch_messages(self, ts):
|
|
||||||
return history(self.id(), ts)
|
|
||||||
|
|
||||||
def do_mark_messages(self, ts):
|
def do_mark_messages(self, ts):
|
||||||
mark(self.id(), 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):
|
def info(channel):
|
||||||
ret = api.call("channels.info", token = auth.token(), channel = channel)
|
ret = api.call("channels.info", token = auth.token(), channel = channel)
|
||||||
if ret["ok"] == False:
|
if ret["ok"] == False:
|
||||||
|
|
|
@ -9,25 +9,16 @@ from . import threads
|
||||||
class Group(threads.Thread):
|
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"
|
||||||
|
|
||||||
def do_fetch_info(self):
|
def do_fetch_info(self):
|
||||||
return info(self.id())
|
return info(self.id())
|
||||||
|
|
||||||
def do_fetch_messages(self, ts):
|
|
||||||
return history(self.id(), ts)
|
|
||||||
|
|
||||||
def do_mark_messages(self, ts):
|
def do_mark_messages(self, ts):
|
||||||
mark(self.id(), 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):
|
def info(group):
|
||||||
ret = api.call("groups.info", token = auth.token(), channel = group)
|
ret = api.call("groups.info", token = auth.token(), channel = group)
|
||||||
if ret["ok"] == False:
|
if ret["ok"] == False:
|
||||||
|
|
14
slack/im.py
14
slack/im.py
|
@ -9,8 +9,9 @@ from . import users
|
||||||
class IM(threads.Thread):
|
class IM(threads.Thread):
|
||||||
def __init__(self, json):
|
def __init__(self, json):
|
||||||
threads.Thread.__init__(self, json)
|
threads.Thread.__init__(self, json)
|
||||||
self.__uid = json.get("user")
|
self.api_history = "im.history"
|
||||||
self.__user = users.info(self.__uid)
|
self.__uid = json.get("user")
|
||||||
|
self.__user = users.info(self.__uid)
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return "Chat With %s" % self.__user.name()
|
return "Chat With %s" % self.__user.name()
|
||||||
|
@ -21,20 +22,11 @@ class IM(threads.Thread):
|
||||||
def do_fetch_info(self):
|
def do_fetch_info(self):
|
||||||
return info(self.id(), self.__uid)
|
return info(self.id(), self.__uid)
|
||||||
|
|
||||||
def do_fetch_messages(self, ts):
|
|
||||||
return history(self.id(), ts)
|
|
||||||
|
|
||||||
def do_mark_messages(self, ts):
|
def do_mark_messages(self, ts):
|
||||||
mark(self.id(), 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):
|
def info(im, uid):
|
||||||
ret = api.call("im.open", token = auth.token(), user = uid, return_im = True)
|
ret = api.call("im.open", token = auth.token(), user = uid, return_im = True)
|
||||||
if ret["ok"] == False:
|
if ret["ok"] == False:
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
#
|
#
|
||||||
# Copyright 2015 (c) Anna Schumaker.
|
# Copyright 2015 (c) Anna Schumaker.
|
||||||
#
|
#
|
||||||
|
from . import api
|
||||||
|
from . import auth
|
||||||
from . import chat
|
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 implement the following methods:
|
# or chat. Note that child classes need to define the following variables:
|
||||||
#
|
#
|
||||||
# - do_fetch_info(): Call <whatever>.info to find unread count and timestamp.
|
# - do_fetch_info(): Call <whatever>.info to find unread count and timestamp.
|
||||||
# - do_fetch_messages(): Call <whatever>.history to find unread messages.
|
# - self.api_history: The slack api method to find unread messages
|
||||||
# - do_mark_messages(): Call <whatever>.mark to set unread cursor.
|
# - do_mark_messages(): Call <whatever>.mark to set unread cursor.
|
||||||
#
|
#
|
||||||
class Thread:
|
class Thread:
|
||||||
|
@ -34,6 +35,13 @@ class Thread:
|
||||||
self.__last_ts = json.get("last_read", None)
|
self.__last_ts = json.get("last_read", None)
|
||||||
self.__id = json.get("id", 0)
|
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):
|
def id(self):
|
||||||
return self.__id
|
return self.__id
|
||||||
|
|
||||||
|
@ -51,7 +59,7 @@ class Thread:
|
||||||
self.fetch_info()
|
self.fetch_info()
|
||||||
|
|
||||||
# Read original message list
|
# 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 = [ chat.Message(msg) for msg in messages ]
|
||||||
o_list.sort()
|
o_list.sort()
|
||||||
if len(o_list) == 0:
|
if len(o_list) == 0:
|
||||||
|
|
Loading…
Reference in New Issue