summaryrefslogtreecommitdiffstats
path: root/lib/mongo.py
diff options
context:
space:
mode:
authorPierre-Yves Chibon <pingou@pingoured.fr>2012-03-23 18:37:14 +0100
committerPierre-Yves Chibon <pingou@pingoured.fr>2012-03-23 18:37:14 +0100
commit919d535f4077b4b0fc94b5b3e72507c603d1e779 (patch)
treeb1893a13d3281fa2402edb36f0593b658d6cfc6f /lib/mongo.py
parente2ad1c98414750d37ab259befe7b03886880651a (diff)
downloadhyperkitty-919d535f4077b4b0fc94b5b3e72507c603d1e779.tar.gz
hyperkitty-919d535f4077b4b0fc94b5b3e72507c603d1e779.tar.xz
hyperkitty-919d535f4077b4b0fc94b5b3e72507c603d1e779.zip
Add the thread view logic
Diffstat (limited to 'lib/mongo.py')
-rw-r--r--lib/mongo.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/mongo.py b/lib/mongo.py
index 2ea0148..c3f2fab 100644
--- a/lib/mongo.py
+++ b/lib/mongo.py
@@ -7,6 +7,58 @@ from datetime import datetime
connection = pymongo.Connection('localhost', 27017)
+def _build_thread(emails):
+ thread = {}
+ for email in emails:
+ #print email['Date'], email['From'] #, email['Message-ID']
+ email = Bunch(email)
+ ref = []
+ if 'References' in email:
+ ref.extend(email['References'].split()[-1:])
+ elif 'In-Reply-To' in email:
+ ref.append(email['In-Reply-To'])
+ if email['Message-ID'] not in thread:
+ thread[email['Message-ID']] = Bunch(
+ {'email': email, 'child': []})
+ else:
+ thread[email['Message-ID']].email = email
+ for ref in set(ref):
+ if ref in thread:
+ thread[ref].child.append(email['Message-ID'])
+ else:
+ thread[ref] = Bunch(
+ {'email': None, 'child': [email['Message-ID']]})
+ return thread
+
+def _tree_to_list(tree, mailid, level, thread_list):
+ start = tree[mailid]
+ start.level = level
+ thread_list.append(start)
+ for mail in start.child:
+ mail = tree[mail]
+ thread_list = _tree_to_list(tree, mail.email['Message-ID'],
+ level + 1, thread_list)
+ return thread_list
+
+def get_thread_list(table, threadid):
+ db = connection[table]
+ thread = list(db.mails.find({'ThreadID': threadid},
+ sort=[('Date', pymongo.ASCENDING)]))
+
+ tree = _build_thread(thread)
+ thread_list = []
+ if thread:
+ thread = _tree_to_list(tree, thread[0]['Message-ID'], 0, thread_list)
+ return thread
+ else:
+ return []
+
+def get_thread_name(table, threadid):
+ db = connection[table]
+ thread = list(db.mails.find({'ThreadID': threadid},
+ sort=[('Date', pymongo.ASCENDING)]))[0]
+ return thread['Subject']
+
def get_emails_thread(table, start_email, thread):
db = connection[table]
db.mails.create_index('Date')