# # 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 .info to find unread count and timestamp. # - do_fetch_messages(): Call .history to find unread messages. # - do_mark_messages(): Call .mark to set unread cursor. # class Thread: def __init__(self, json): self.__id = json.get("id", 0) self.__name = json.get("name", "").title() self.__topic = json["topic"].get("value", "").encode("utf-8").decode("latin-1") self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1") self.__unread = json.get("unread_count", None) self.__last_ts = json.get("last_read", None) def __str__(self): topic="" if self.__topic != "": topic = "\nTopic: %s" % self.__topic return "%s: %s%s" % (self.__name, self.__purpose, topic) def fetch_info(self): json = self.do_fetch_info() if json != None: self.__unread = json.get("unread_count", None) self.__last_ts = json.get("last_read", None) def id(self): return self.__id def is_member(self): return True 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() return self.__unread def post(self, text): chat.post_message(self.__id, text)