See changes in Changelog
[diaspy.git] / diaspy / messages.py
1 #!/usr/bin/env python3
2
3
4 from diaspy import errors, models
5
6
7 class Mailbox():
8 """Object implementing diaspora* mailbox.
9 """
10 def __init__(self, connection, fetch=True):
11 self._connection = connection
12 self._mailbox = []
13 if fetch: self._fetch()
14
15 def __len__(self):
16 return len(self._mailbox)
17
18 def __iter__(self):
19 return iter(self._mailbox)
20
21 def __getitem__(self, n):
22 return self._mailbox[n]
23
24 def _fetch(self):
25 """This method will fetch messages from user's mailbox.
26 """
27 request = self._connection.get('conversations.json')
28
29 if request.status_code != 200:
30 raise errors.DiaspyError('wrong status code: {0}'.format(r.status_code))
31 mailbox = request.json()
32 self._mailbox = [models.Conversation(self._connection, c['conversation']['id']) for c in mailbox]