~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/store.py

  • Committer: Curtis Hovey
  • Date: 2012-03-01 01:08:15 UTC
  • Revision ID: curtis.hovey@canonical.com-20120301010815-hz0al69e3j0c9e6n
Raise ArchiveIdNotFound if the client puts a message into an unknown archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    'MemoryStore',
3
3
    ]
4
4
 
 
5
import email
5
6
import simplejson
6
7
from urlparse import parse_qs
7
8
 
8
9
from grackle.error import (
 
10
    ArchiveIdNotFound,
9
11
    MessageIdNotFound,
10
12
    UnparsableDateRange,
11
13
    UnsupportedDisplayType,
36
38
    return threads.values()
37
39
 
38
40
 
 
41
def make_json_message(message_id, raw_message):
 
42
    message = email.message_from_string(raw_message)
 
43
    headers = dict(message.items())
 
44
    message = {
 
45
        'message_id': message_id,
 
46
        'headers': headers,
 
47
        # This is broken because the in-reply-to must be encoded.
 
48
        # X-Message-ID-Hash is calculated from the Base 32.
 
49
        'thread_id': headers.get('in-reply-to', message_id),
 
50
        'date': headers.get('date'),
 
51
        'subject': headers.get('subject'),
 
52
        'author': headers.get('from'),
 
53
        'hidden': False,
 
54
        'attachments': [],
 
55
        'replies': [],
 
56
        'body': raw_message,
 
57
        }
 
58
    return message
 
59
 
 
60
 
39
61
class MemoryStore:
40
62
    """A memory-backed message store."""
41
63
 
47
69
    def is_multipart(message):
48
70
        return isinstance(message['body'], list)
49
71
 
 
72
    def put_message(self, archive_id, message_id, raw_message):
 
73
        # XXX sinzui 2012-02-29: this needs to raise an error
 
74
        # if the th archive_id is invalid, message_id is not base32
 
75
        # or the raw message is not an email.
 
76
        if archive_id not in self.message_archives:
 
77
            raise ArchiveIdNotFound()
 
78
        if not raw_message:
 
79
            raise ValueError('raw_message is not a message.')
 
80
        json_message = make_json_message(message_id, raw_message)
 
81
        messages = self.message_archives[archive_id]
 
82
        messages.append(json_message)
 
83
 
50
84
    def get_messages(self, archive_id, query_string):
51
85
        """Return matching messages.
52
86