Add support for reading messages through the Thread class

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-06-09 10:22:10 -04:00
parent 6859f8c3ec
commit ca89d98e3a
4 changed files with 52 additions and 27 deletions

View File

@ -16,41 +16,25 @@ class Channel(threads.Thread):
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 is_member(self):
return self.__member
def post(self, text):
chat.postMessage(self.__id, text)
def read(self):
if self.__last_ts == None:
self.fetch_info()
return history(self.__id, self.__last_ts)
def history(channel, timestamp):
ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp)
if ret["ok"] == False:
return None
# Read original message list
o_list = []
for message in ret["messages"]:
o_list += [ chat.Message(message) ]
o_list.sort()
# Merge together messages from the same user
m_list = [ o_list[0] ]
for msg in o_list[1:]:
if msg.user() == m_list[-1].user():
m_list[-1].merge(msg)
else:
m_list += [ msg ]
if len(m_list) > 0:
mark(channel, m_list[-1].ts())
return m_list
return ret["messages"]
def info(channel):
ret = api.call("channels.info", token = auth.token(), channel = channel)

View File

@ -10,11 +10,11 @@ import re
def __text_parse_uid(text):
def _text_parse_uid(text):
res = text
for uid in re.findall("<@(.*?)>", text):
uid = uid.split("|")[0]
user = slack.users.info(uid)
user = users.info(uid)
res = re.sub("<@%s(.*?)>" % uid, "@%s" % user.user(), res)
return res
@ -33,7 +33,7 @@ class Message:
def __str__(self):
lines = []
for line in self.__text:
line = __text_parse_uid(line)
line = _text_parse_uid(line)
lines += [ "" ] + textwrap.wrap(line)
text = "\n ".join(lines)
time = self.__time.strftime("%I:%M:%S %p")

View File

@ -13,6 +13,19 @@ class Group(threads.Thread):
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):
@ -30,3 +43,6 @@ def list():
for group in ret["groups"]:
gr_list += [ Group(group) ]
return gr_list
def mark(group, timestamp):
api.call("groups.mark", token = auth.token(), channel = group, ts = timestamp)

View File

@ -2,12 +2,15 @@
# Copyright 2015 (c) Anna Schumaker.
#
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:
#
# - 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.
# - do_mark_messages(): Call <whatever>.mark to set unread cursor.
#
class Thread:
def __init__(self, json):
@ -39,6 +42,28 @@ class Thread:
def name(self):
return self.__name
def read(self):
if self.__last_ts == None:
self.fetch_info()
# Read original message list
o_list = []
for message in self.do_fetch_messages(self.__last_ts):
o_list += [ chat.Message(message) ]
o_list.sort()
# Merge together messages from the same user
m_list = [ o_list[0] ]
for msg in o_list[1:]:
if msg.user() == m_list[-1].user():
m_list[-1].merge(msg)
else:
m_list += [ msg ]
self.do_mark_messages(m_list[-1].ts())
return m_list
def unread_count(self):
if self.__unread == None:
self.fetch_info()