~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
 
39
 
def make_json_message(message_id, body='body', headers=None, hidden=False):
40
 
    if headers is None:
41
 
        headers = {}
42
 
    headers['Message-Id'] = message_id
 
41
def make_json_message(message_id, raw_message):
 
42
    message = email.message_from_string(raw_message)
 
43
    headers = dict(message.items())
43
44
    message = {
44
45
        'message_id': message_id,
45
46
        'headers': headers,
46
 
        'thread_id': message_id,
47
 
        'date': headers.get('date', '2005-01-01'),
48
 
        'subject': headers.get('subject', 'subject'),
49
 
        'author': headers.get('author', 'author'),
50
 
        'hidden': hidden,
 
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,
51
54
        'attachments': [],
52
 
        'replies': headers.get('in-reply-to', None),
53
 
        'body': body,
 
55
        'replies': [],
 
56
        'body': raw_message,
54
57
        }
55
58
    return message
56
59
 
66
69
    def is_multipart(message):
67
70
        return isinstance(message['body'], list)
68
71
 
69
 
    def put_message(self, archive_id, message_id, message):
70
 
        # Make a json message.
 
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)
71
81
        messages = self.message_archives[archive_id]
72
 
        json_message = make_json_message(message_id, body=message)
73
82
        messages.append(json_message)
74
83
 
75
84
    def get_messages(self, archive_id, query_string):