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:
parent
e80b200a31
commit
8d38fffaf4
|
@ -49,7 +49,7 @@ class Channel:
|
||||||
def read(self):
|
def read(self):
|
||||||
if self.__last_ts == None:
|
if self.__last_ts == None:
|
||||||
self.fetch_info()
|
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:
|
if ret["ok"] == False:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
m_list = []
|
# Read original message list
|
||||||
|
o_list = []
|
||||||
for message in ret["messages"]:
|
for message in ret["messages"]:
|
||||||
msg = chat.Message(message)
|
o_list += [ chat.Message(message) ]
|
||||||
if len(m_list) == 0:
|
o_list.sort()
|
||||||
m_list += [ msg ]
|
|
||||||
elif m_list[-1].user() == msg.user():
|
# 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)
|
m_list[-1].merge(msg)
|
||||||
else:
|
else:
|
||||||
m_list += [ msg ]
|
m_list += [ msg ]
|
||||||
|
|
||||||
m_list.sort()
|
|
||||||
|
|
||||||
if len(m_list) > 0:
|
if len(m_list) > 0:
|
||||||
mark(channel, m_list[-1].ts())
|
mark(channel, m_list[-1].ts())
|
||||||
return m_list
|
return m_list
|
||||||
|
|
|
@ -12,7 +12,7 @@ class Message:
|
||||||
def __init__(self, json):
|
def __init__(self, json):
|
||||||
self.__ts = json["ts"]
|
self.__ts = json["ts"]
|
||||||
self.__time = datetime.fromtimestamp(float(self.__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") ]
|
self.__text = [ json["text"].encode("utf-8").decode("latin-1") ]
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
@ -34,6 +34,7 @@ class Message:
|
||||||
|
|
||||||
def merge(self, msg):
|
def merge(self, msg):
|
||||||
self.__text += msg.__text
|
self.__text += msg.__text
|
||||||
|
self.__ts = msg.__ts
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue