summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAurélien Bompard <aurelien@bompard.org>2013-05-18 12:15:03 +0200
committerAurélien Bompard <aurelien@bompard.org>2013-05-18 12:15:03 +0200
commita55527aad008d522b98015cd8941ec3fad5d39d5 (patch)
tree5eec822c66c1ebda52f5a1b2753791224187bb90
parent35dfa25de6642bb9e5c9a8b4d720a25993c65809 (diff)
downloadkittystore-a55527aad008d522b98015cd8941ec3fad5d39d5.tar.gz
kittystore-a55527aad008d522b98015cd8941ec3fad5d39d5.tar.xz
kittystore-a55527aad008d522b98015cd8941ec3fad5d39d5.zip
Fix thread neighbors method to sort by thread activity
Closes: #29
-rw-r--r--kittystore/storm/store.py33
-rw-r--r--kittystore/test/test_storm_store.py53
2 files changed, 68 insertions, 18 deletions
diff --git a/kittystore/storm/store.py b/kittystore/storm/store.py
index 6479099..7276048 100644
--- a/kittystore/storm/store.py
+++ b/kittystore/storm/store.py
@@ -410,8 +410,7 @@ class StormStore(object):
def get_thread_neighbors(self, list_name, thread_id):
""" Return the previous and the next threads of the specified thread,
- in date order. The returned objects are the emails starting the
- threads.
+ in date order.
:param list_name: The name of the mailing list to query.
:param thread_id: The unique identifier of the thread as specified in
@@ -420,26 +419,24 @@ class StormStore(object):
this order.
:rtype: tuple
"""
- current_email = self.get_message_by_hash_from_list(list_name, thread_id)
- next_email = self.db.find(Email, And(
- Email.list_name == unicode(list_name),
- Email.in_reply_to == None,
- Email.date > current_email.date,
- )).order_by(Email.date)
+ thread = self.get_thread(list_name, thread_id)
+ next_thread = self.db.find(Thread, And(
+ Thread.list_name == unicode(list_name),
+ Thread.date_active > thread.date_active,
+ )).order_by(Thread.date_active)
try:
- next_email = next_email[0]
+ next_thread = next_thread[0]
except IndexError:
- next_email = None
- prev_email = self.db.find(Email, And(
- Email.list_name == unicode(list_name),
- Email.in_reply_to == None,
- Email.date < current_email.date,
- )).order_by(Desc(Email.date))
+ next_thread = None
+ prev_thread = self.db.find(Thread, And(
+ Thread.list_name == unicode(list_name),
+ Thread.date_active < thread.date_active,
+ )).order_by(Desc(Thread.date_active))
try:
- prev_email = prev_email[0]
+ prev_thread = prev_thread[0]
except IndexError:
- prev_email = None
- return (prev_email, next_email)
+ prev_thread = None
+ return (prev_thread, next_thread)
def get_list(self, list_name):
""" Return the list object for a mailing list name.
diff --git a/kittystore/test/test_storm_store.py b/kittystore/test/test_storm_store.py
index cea9b60..d4fa464 100644
--- a/kittystore/test/test_storm_store.py
+++ b/kittystore/test/test_storm_store.py
@@ -12,6 +12,7 @@ from mailman.email.message import Message
from kittystore.storm import get_storm_store
from kittystore.storm.model import Email, Attachment, List
+from kittystore.utils import get_message_id_hash
from kittystore.test import get_test_file, FakeList
@@ -93,6 +94,58 @@ class TestStormStore(unittest.TestCase):
self.assertEqual(ml_db.display_name, "name 2")
self.assertEqual(ml_db.description, "desc 2")
+
+ def test_thread_neighbors(self):
+ ml = FakeList("example-list")
+ # Create 3 threads
+ msg_t1_1 = Message()
+ msg_t1_1["From"] = "dummy@example.com"
+ msg_t1_1["Message-ID"] = "<id1_1>"
+ msg_t1_1.set_payload("Dummy message")
+ self.store.add_to_list(ml, msg_t1_1)
+ msg_t2_1 = Message()
+ msg_t2_1["From"] = "dummy@example.com"
+ msg_t2_1["Message-ID"] = "<id2_1>"
+ msg_t2_1.set_payload("Dummy message")
+ self.store.add_to_list(ml, msg_t2_1)
+ msg_t3_1 = Message()
+ msg_t3_1["From"] = "dummy@example.com"
+ msg_t3_1["Message-ID"] = "<id3_1>"
+ msg_t3_1.set_payload("Dummy message")
+ self.store.add_to_list(ml, msg_t3_1)
+ # Check the neighbors
+ def check_neighbors(thread, expected_prev, expected_next):
+ thread_id = get_message_id_hash("<id%s_1>" % thread)
+ prev_th, next_th = self.store.get_thread_neighbors(
+ "example-list", thread_id)
+ # convert to something I can compare
+ prev_th = prev_th and prev_th.thread_id
+ expected_prev = expected_prev and \
+ get_message_id_hash("<id%s_1>" % expected_prev)
+ next_th = next_th and next_th.thread_id
+ expected_next = expected_next and \
+ get_message_id_hash("<id%s_1>" % expected_next)
+ # compare
+ self.assertEqual(prev_th, expected_prev)
+ self.assertEqual(next_th, expected_next)
+ # Order should be: 1, 2, 3
+ check_neighbors(1, None, 2)
+ check_neighbors(2, 1, 3)
+ check_neighbors(3, 2, None)
+ # now add a new message in thread 1, which becomes the most recently
+ # active
+ msg_t1_2 = Message()
+ msg_t1_2["From"] = "dummy@example.com"
+ msg_t1_2["Message-ID"] = "<id1_2>"
+ msg_t1_2["In-Reply-To"] = "<id1_1>"
+ msg_t1_2.set_payload("Dummy message")
+ self.store.add_to_list(ml, msg_t1_2)
+ # Order should be: 2, 3, 1
+ check_neighbors(2, None, 3)
+ check_neighbors(3, 2, 1)
+ check_neighbors(1, 3, None)
+
+
#def test_non_ascii_payload(self):
# """add_to_list must handle non-ascii messages"""
# with open(get_test_file("non-ascii-payload.txt")) as email_file: