Fixes for merging messages

I noticed that a merged message reversed all the lines.  This is due to
how slack returns messages in the history() call, so let's parse replies
in two steps to keep everything chronological.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-06-08 16:52:53 -04:00
parent e80b200a31
commit 8d38fffaf4
2 changed files with 12 additions and 9 deletions

View File

@ -49,7 +49,7 @@ class Channel:
def read(self):
if self.__last_ts == None:
self.fetch_info()
return history(self.__id, self.__last_ts);
return history(self.__id, self.__last_ts)
@ -58,18 +58,20 @@ def history(channel, timestamp):
if ret["ok"] == False:
return None
m_list = []
# Read original message list
o_list = []
for message in ret["messages"]:
msg = chat.Message(message)
if len(m_list) == 0:
m_list += [ msg ]
elif m_list[-1].user() == msg.user():
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 ]
m_list.sort()
if len(m_list) > 0:
mark(channel, m_list[-1].ts())
return m_list

View File

@ -12,7 +12,7 @@ class Message:
def __init__(self, json):
self.__ts = json["ts"]
self.__time = datetime.fromtimestamp(float(self.__ts))
self.__user = users.info(json["user"])
self.__user = users.info(json.get("user", None))
self.__text = [ json["text"].encode("utf-8").decode("latin-1") ]
def __lt__(self, other):
@ -34,6 +34,7 @@ class Message:
def merge(self, msg):
self.__text += msg.__text
self.__ts = msg.__ts