Add support for reading messages through the Thread class
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
6859f8c3ec
commit
ca89d98e3a
|
@ -16,41 +16,25 @@ class Channel(threads.Thread):
|
||||||
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):
|
||||||
|
mark(self.id(), ts)
|
||||||
|
|
||||||
def is_member(self):
|
def is_member(self):
|
||||||
return self.__member
|
return self.__member
|
||||||
|
|
||||||
def post(self, text):
|
def post(self, text):
|
||||||
chat.postMessage(self.__id, 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):
|
def history(channel, timestamp):
|
||||||
ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp)
|
ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp)
|
||||||
if ret["ok"] == False:
|
if ret["ok"] == False:
|
||||||
return None
|
return None
|
||||||
|
return ret["messages"]
|
||||||
# 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
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -10,11 +10,11 @@ import re
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __text_parse_uid(text):
|
def _text_parse_uid(text):
|
||||||
res = text
|
res = text
|
||||||
for uid in re.findall("<@(.*?)>", text):
|
for uid in re.findall("<@(.*?)>", text):
|
||||||
uid = uid.split("|")[0]
|
uid = uid.split("|")[0]
|
||||||
user = slack.users.info(uid)
|
user = users.info(uid)
|
||||||
res = re.sub("<@%s(.*?)>" % uid, "@%s" % user.user(), res)
|
res = re.sub("<@%s(.*?)>" % uid, "@%s" % user.user(), res)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class Message:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
lines = []
|
lines = []
|
||||||
for line in self.__text:
|
for line in self.__text:
|
||||||
line = __text_parse_uid(line)
|
line = _text_parse_uid(line)
|
||||||
lines += [ "" ] + textwrap.wrap(line)
|
lines += [ "" ] + textwrap.wrap(line)
|
||||||
text = "\n ".join(lines)
|
text = "\n ".join(lines)
|
||||||
time = self.__time.strftime("%I:%M:%S %p")
|
time = self.__time.strftime("%I:%M:%S %p")
|
||||||
|
|
|
@ -13,6 +13,19 @@ class Group(threads.Thread):
|
||||||
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):
|
||||||
|
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):
|
||||||
|
@ -30,3 +43,6 @@ def list():
|
||||||
for group in ret["groups"]:
|
for group in ret["groups"]:
|
||||||
gr_list += [ Group(group) ]
|
gr_list += [ Group(group) ]
|
||||||
return gr_list
|
return gr_list
|
||||||
|
|
||||||
|
def mark(group, timestamp):
|
||||||
|
api.call("groups.mark", token = auth.token(), channel = group, ts = timestamp)
|
||||||
|
|
|
@ -2,12 +2,15 @@
|
||||||
# Copyright 2015 (c) Anna Schumaker.
|
# Copyright 2015 (c) Anna Schumaker.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
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 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:
|
class Thread:
|
||||||
def __init__(self, json):
|
def __init__(self, json):
|
||||||
|
@ -39,6 +42,28 @@ class Thread:
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.__name
|
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):
|
def unread_count(self):
|
||||||
if self.__unread == None:
|
if self.__unread == None:
|
||||||
self.fetch_info()
|
self.fetch_info()
|
||||||
|
|
Loading…
Reference in New Issue