slackmail/slack/im.py

56 lines
1.3 KiB
Python

#
# Copyright 2016 (c) Anna Schumaker.
#
from . import api
from . import auth
from . import threads
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)
def name(self):
return "Chat With %s" % self.__user.name()
def url(self, base):
return "%s/messages/@%s/" % (base, self.__user.user())
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:
return None
return ret["channel"]
def list():
ret = api.call("im.list", token = auth.token())
if ret["ok"] == False:
return None
im_list = []
for im in ret["ims"]:
im_list += [ IM(im) ]
return im_list
def mark(im, timestamp):
api.call("im.mark", token = auth.token(), channel = im, ts = timestamp)