diff src/tools/common/data_objects.py @ 2464:8788c217a49e

tools (common/data_objects): added classes for messages
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 00:21:21 +0100
parents 8b37a62336c3
children adcc35625e17
line wrap: on
line diff
--- a/src/tools/common/data_objects.py	Wed Jan 03 00:20:21 2018 +0100
+++ b/src/tools/common/data_objects.py	Wed Jan 03 00:21:21 2018 +0100
@@ -133,7 +133,6 @@
 class BlogItems(object):
 
     def __init__(self, mb_data):
-        self.mb_data = mb_data
         self.items = [BlogItem(i) for i in mb_data[0]]
         self.metadata = mb_data[1]
 
@@ -168,6 +167,103 @@
         return self.items.__contains__(item)
 
 
+class Message(object):
+
+    def __init__(self, msg_data):
+        self._uid = msg_data[0]
+        self._timestamp = msg_data[1]
+        self._from_jid = msg_data[2]
+        self._to_jid = msg_data[3]
+        self._message_data = msg_data[4]
+        self._subject_data = msg_data[5]
+        self._type = msg_data[6]
+        self._extra = msg_data[7]
+        self._html = dict(data_format.getSubDict('xhtml', self._extra))
+
+    @property
+    def id(self):
+        return self._uid
+
+    @property
+    def timestamp(self):
+        return self._timestamp
+
+    @property
+    def from_(self):
+        return self._from_jid
+
+    @property
+    def text(self):
+        try:
+            return self._message_data['']
+        except KeyError:
+            return next(self._message_data.itervalues())
+
+    @property
+    def subject(self):
+        try:
+            return self._subject_data['']
+        except KeyError:
+            return next(self._subject_data.itervalues())
+
+    @property
+    def type(self):
+        return self._type
+
+    @property
+    def thread(self):
+        return self._extra.get('thread')
+
+    @property
+    def thread_parent(self):
+        return self._extra.get('thread_parent')
+
+    @property
+    def received(self):
+        return self._extra.get('received_timestamp', self._timestamp)
+
+    @property
+    def delay_sender(self):
+        return self._extra.get('delay_sender')
+
+    @property
+    def info_type(self):
+        return self._extra.get('info_type')
+
+    @property
+    def html(self):
+        if not self._html:
+            return None
+        try:
+            return safe(self._html[''])
+        except KeyError:
+            return safe(next(self._html.itervalues()))
+
+
+class Messages(object):
+
+    def __init__(self, msgs_data):
+        self.messages = [Message(m) for m in msgs_data]
+
+    def __len__(self):
+        return self.messages.__len__()
+
+    def __missing__(self, key):
+        return self.messages.__missing__(key)
+
+    def __getitem__(self, key):
+        return self.messages.__getitem__(key)
+
+    def __iter__(self):
+        return self.messages.__iter__()
+
+    def __reversed__(self):
+        return self.messages.__reversed__()
+
+    def __contains__(self, item):
+        return self.messages.__contains__(item)
+
+
 class Identity(object):
 
     def __init__(self, jid_str, data=None):