Commit | Line | Data |
---|---|---|
88ec6cda MM |
1 | #!/usr/bin/env python3 |
2 | ||
70227e79 | 3 | |
7ebc8b0d MM |
4 | from diaspy import errors |
5 | ||
6 | ||
385e7ebe | 7 | class Conversation(): |
70227e79 B |
8 | """This class represents a conversation. |
9 | ||
10 | .. note:: | |
11 | Remember that you need to have access to the conversation. | |
70227e79 | 12 | """ |
7ebc8b0d | 13 | def __init__(self, connection, id, fetch=True): |
70227e79 | 14 | """ |
df912114 MM |
15 | :param conv_id: id of the post and not the guid! |
16 | :type conv_id: str | |
b95ffe83 MM |
17 | :param connection: connection object used to authenticate |
18 | :type connection: connection.Connection | |
70227e79 | 19 | """ |
b95ffe83 | 20 | self._connection = connection |
7ebc8b0d MM |
21 | self.id = id |
22 | self.data = {} | |
23 | if fetch: self._fetch() | |
70227e79 | 24 | |
7ebc8b0d MM |
25 | def _fetch(self): |
26 | """Fetches JSON data representing conversation. | |
70227e79 | 27 | """ |
78cc478a | 28 | request = self._connection.get('conversations/{}.json'.format(self.id)) |
7ebc8b0d MM |
29 | if request.status_code == 200: |
30 | self.data = request.json()['conversation'] | |
70227e79 | 31 | else: |
7ebc8b0d | 32 | raise errors.ConversationError('cannot download conversation data: {0}'.format(request.status_code)) |
70227e79 B |
33 | |
34 | def answer(self, text): | |
7ebc8b0d | 35 | """Answer that conversation |
70227e79 B |
36 | |
37 | :param text: text to answer. | |
38 | :type text: str | |
70227e79 | 39 | """ |
70227e79 B |
40 | data = {'message[text]': text, |
41 | 'utf8': '✓', | |
7ebc8b0d | 42 | 'authenticity_token': repr(self._connection)} |
70227e79 | 43 | |
7ebc8b0d MM |
44 | request = self._connection.post('conversations/{}/messages'.format(self.id), |
45 | data=data, | |
46 | headers={'accept': 'application/json'}) | |
47 | if request.status_code != 200: | |
48 | raise errors.ConversationError('{0}: Answer could not be posted.' | |
49 | .format(request.status_code)) | |
50 | return request.json() | |
70227e79 B |
51 | |
52 | def delete(self): | |
7ebc8b0d MM |
53 | """Delete this conversation. |
54 | Has to be implemented. | |
70227e79 | 55 | """ |
7ebc8b0d | 56 | data = {'authenticity_token': repr(self._connection)} |
70227e79 | 57 | |
7ebc8b0d MM |
58 | request = self._connection.delete('conversations/{0}/visibility/' |
59 | .format(self.id), | |
385e7ebe MM |
60 | data=data, |
61 | headers={'accept': 'application/json'}) | |
70227e79 | 62 | |
7ebc8b0d MM |
63 | if request.status_code != 404: |
64 | raise errors.ConversationError('{0}: Conversation could not be deleted.' | |
65 | .format(request.status_code)) | |
70227e79 B |
66 | |
67 | def get_subject(self): | |
7ebc8b0d | 68 | """Returns the subject of this conversation |
70227e79 | 69 | """ |
7ebc8b0d | 70 | return self.data['subject'] |