~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/store.py

  • Committer: Curtis Hovey
  • Date: 2012-03-17 22:45:15 UTC
  • Revision ID: curtis.hovey@canonical.com-20120317224515-r2n23tqc8cx7cul4
Only store the unique information needed by grackle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__metaclass__ = type
1
2
__all__ = [
 
3
    'make_json_message',
2
4
    'MemoryStore',
 
5
    'SUPPORTED_DISPLAY_TYPES',
 
6
    'SUPPORTED_ORDERS',
3
7
    ]
4
8
 
 
9
import email
5
10
import simplejson
6
11
from urlparse import parse_qs
7
12
 
8
13
from grackle.error import (
 
14
    ArchiveIdExists,
 
15
    ArchiveIdNotFound,
9
16
    MessageIdNotFound,
10
17
    UnparsableDateRange,
11
18
    UnsupportedDisplayType,
36
43
    return threads.values()
37
44
 
38
45
 
 
46
def get_body_text(message):
 
47
    """Return the first plain/text messaage part."""
 
48
    if not message.is_multipart():
 
49
        return message.get_payload()
 
50
    for part in email.iterators.typed_subpart_iterator(message, 'multipart'):
 
51
        subparts = part.get_payload()
 
52
        for subpart in subparts:
 
53
            if subpart.get_content_type() == 'text/plain':
 
54
                return subpart.get_payload().strip()
 
55
    return ''
 
56
 
 
57
 
 
58
def make_json_message(message_id, raw_message, hidden=False):
 
59
    message = email.message_from_string(raw_message)
 
60
    headers = dict(message.items())
 
61
    message = {
 
62
        'message_id': message_id,
 
63
        'headers': headers,
 
64
        # This is broken because the in-reply-to must be encoded.
 
65
        # X-Message-ID-Hash is calculated from the Base 32.
 
66
        'thread_id': headers.get('in-reply-to', message_id),
 
67
        'date': headers.get('date'),
 
68
        'subject': headers.get('subject'),
 
69
        'author': headers.get('from'),
 
70
        'hidden': hidden,
 
71
        'attachments': [],
 
72
        'replies': headers.get('in-reply-to'),
 
73
        'body': get_body_text(message),
 
74
        }
 
75
    return message
 
76
 
 
77
 
39
78
class MemoryStore:
40
79
    """A memory-backed message store."""
41
80
 
42
 
    def __init__(self, messages):
 
81
    def __init__(self, message_archives):
43
82
        """Constructor."""
44
 
        self.messages = messages
 
83
        self.message_archives = message_archives
45
84
 
46
85
    @staticmethod
47
86
    def is_multipart(message):
48
87
        return isinstance(message['body'], list)
49
88
 
 
89
    def put_archive(self, archive_id, raw_archive=None):
 
90
        # XXX sinzui 2012-02-29: this needs to raise an error
 
91
        # if the th archive_id is invalid, or the raw archive is not mbox.
 
92
        if archive_id in self.message_archives:
 
93
            raise ArchiveIdExists()
 
94
        self.message_archives[archive_id] = []
 
95
 
 
96
    def put_message(self, archive_id, message_id, raw_message):
 
97
        # XXX sinzui 2012-02-29: this needs to raise an error
 
98
        # if the th archive_id is invalid, message_id is not base32
 
99
        # or the raw message is not an email.
 
100
        if archive_id not in self.message_archives:
 
101
            raise ArchiveIdNotFound()
 
102
        if not raw_message:
 
103
            raise ValueError('raw_message is not a message.')
 
104
        json_message = make_json_message(message_id, raw_message)
 
105
        messages = self.message_archives[archive_id]
 
106
        messages.append(json_message)
 
107
 
50
108
    def get_messages(self, archive_id, query_string):
51
109
        """Return matching messages.
52
110
 
57
115
        query = parse_qs(query_string)
58
116
        parameters = simplejson.loads(query['parameters'][0])
59
117
        order = parameters.get('order')
60
 
        messages = self.messages[archive_id]
 
118
        messages = self.message_archives[archive_id]
61
119
        if order is not None:
62
120
            if order not in SUPPORTED_ORDERS:
63
121
                raise UnsupportedOrder
142
200
            }
143
201
        return response
144
202
 
145
 
    def hide_message(self, archive_id, query_string):
146
 
        """Return matching messages.
 
203
    def hide_message(self, archive_id, message_id, query_string):
 
204
        """Change the visbility of a message in an archive.
147
205
 
148
206
        :param archive_id: The archive to retrieve from.
149
207
        :param query_string: Contains 'parameters', which is a JSON-format
151
209
        """
152
210
        query = parse_qs(query_string)
153
211
        parameters = simplejson.loads(query['parameters'][0])
154
 
        message_id = parameters['message_id']
155
212
        hidden = parameters['hidden']
156
 
        messages = self.messages[archive_id]
 
213
        messages = self.message_archives[archive_id]
157
214
        for message in messages:
158
215
            if message['message_id'] == message_id:
159
216
                message['hidden'] = hidden