Commit | Line | Data |
---|---|---|
88ec6cda | 1 | #!/usr/bin/env python3 |
ae221396 MK |
2 | |
3 | ||
178faa46 MM |
4 | import json |
5 | ||
6 | ||
7 | """This module is only imported in other diaspy modules and | |
8 | MUST NOT import anything. | |
9 | """ | |
10 | ||
11 | ||
7a818fdb MM |
12 | class Aspect(): |
13 | """This class represents an aspect. | |
14 | """ | |
15 | def __init__(self, connection, id=-1): | |
16 | self._connection = connection | |
17 | self.id = id | |
18 | self.name = '' | |
19 | ||
20 | def addUser(self, user_id): | |
21 | """Add user to current aspect. | |
22 | ||
23 | :param user_id: user to add to aspect | |
24 | :type user: int | |
25 | """ | |
26 | data = {'authenticity_token': self._connection.get_token(), | |
27 | 'aspect_id': self.id, | |
28 | 'person_id': user_id} | |
29 | ||
30 | request = self._connection.post('aspect_memberships.json', data=data) | |
31 | ||
32 | if request.status_code != 201: | |
33 | raise Exception('wrong status code: {0}'.format(request.status_code)) | |
34 | return request.json() | |
35 | ||
36 | def removeUser(self, user_id): | |
37 | """Remove user from current aspect. | |
38 | ||
39 | :param user_id: user to remove from aspect | |
40 | :type user: int | |
41 | """ | |
42 | data = {'authenticity_token': self._connection.get_token(), | |
43 | 'aspect_id': self.id, | |
44 | 'person_id': user_id} | |
45 | ||
46 | request = self.connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data) | |
47 | ||
48 | if request.status_code != 200: | |
49 | raise Exception('wrong status code: {0}'.format(request.status_code)) | |
50 | return request.json() | |
51 | ||
52 | ||
178faa46 MM |
53 | class Notification(): |
54 | """This class represents single notification. | |
55 | """ | |
56 | def __init__(self, connection, data): | |
57 | self._connection = connection | |
58 | ||
59 | self.type = list(data.keys())[0] | |
60 | self.data = data[self.type] | |
61 | self.id = self.data['id'] | |
62 | self.unread = self.data['unread'] | |
63 | ||
64 | def __getitem__(self, key): | |
65 | return self.data[key] | |
66 | ||
b0b4c46d MM |
67 | def _refresh(self): |
68 | """Refreshes data of the notification. | |
69 | """ | |
70 | ||
178faa46 MM |
71 | def mark(self, unread=False): |
72 | """Marks notification to read/unread. | |
73 | Marks notification to read if `unread` is False. | |
74 | Marks notification to unread if `unread` is True. | |
75 | ||
76 | :param unread: which state set for notification | |
77 | :type unread: bool | |
78 | """ | |
79 | headers = {'x-csrf-token': self._connection.get_token()} | |
80 | params = {'set_unread': json.dumps(unread)} | |
178faa46 MM |
81 | self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers) |
82 | self.data['unread'] = unread | |
83 | ||
84 | ||
dd0a4d9f | 85 | class Post(): |
ae221396 MK |
86 | """This class represents a post. |
87 | ||
88 | .. note:: | |
89 | Remember that you need to have access to the post. | |
ae221396 | 90 | """ |
b95ffe83 | 91 | def __init__(self, post_id, connection): |
a7661afd MK |
92 | """ |
93 | :param post_id: id or guid of the post | |
94 | :type post_id: str | |
b95ffe83 MM |
95 | :param connection: connection object used to authenticate |
96 | :type connection: connection.Connection | |
a7661afd | 97 | """ |
b95ffe83 | 98 | self._connection = connection |
8095ac8b MK |
99 | self.post_id = post_id |
100 | ||
1467ec15 MM |
101 | def __repr__(self): |
102 | """Returns string containing more information then str(). | |
103 | """ | |
104 | data = self.get_data() | |
105 | return '{0} ({1}): {2}'.format(data['author']['name'], data['author']['diaspora_id'], data['text']) | |
106 | ||
66c3bb76 MM |
107 | def __str__(self): |
108 | """Returns text of a post. | |
109 | """ | |
63cc182d | 110 | return self.get_data()['text'] |
66c3bb76 | 111 | |
7eb90eb7 | 112 | def get_data(self): |
0e858bba MM |
113 | """This function retrieves data of the post. |
114 | """ | |
505fc964 MM |
115 | r = self._connection.get('posts/{0}.json'.format(self.post_id)) |
116 | if r.status_code != 200: | |
0e858bba | 117 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
505fc964 | 118 | return r.json() |
a993a4b6 MK |
119 | |
120 | def like(self): | |
88ec6cda | 121 | """This function likes a post. |
df912114 | 122 | It abstracts the 'Like' functionality. |
a993a4b6 MK |
123 | |
124 | :returns: dict -- json formatted like object. | |
a993a4b6 | 125 | """ |
59ad210c | 126 | data = {'authenticity_token': self._connection.get_token()} |
a993a4b6 | 127 | |
b95ffe83 | 128 | r = self._connection.post('posts/{0}/likes'.format(self.post_id), |
385e7ebe MM |
129 | data=data, |
130 | headers={'accept': 'application/json'}) | |
a7661afd MK |
131 | |
132 | if r.status_code != 201: | |
9088535d MK |
133 | raise Exception('{0}: Post could not be liked.' |
134 | .format(r.status_code)) | |
a7661afd | 135 | |
a993a4b6 MK |
136 | return r.json() |
137 | ||
5e809c8b | 138 | def delete_like(self): |
a993a4b6 | 139 | """This function removes a like from a post |
a993a4b6 | 140 | """ |
59ad210c | 141 | data = {'authenticity_token': self._connection.get_token()} |
a993a4b6 | 142 | |
7eb90eb7 | 143 | post_data = self.get_data() |
8095ac8b | 144 | |
b95ffe83 | 145 | r = self._connection.delete('posts/{0}/likes/{1}' |
385e7ebe MM |
146 | .format(self.post_id, |
147 | post_data['interactions'] | |
148 | ['likes'][0]['id']), | |
149 | data=data) | |
a993a4b6 | 150 | |
a7661afd | 151 | if r.status_code != 204: |
9088535d MK |
152 | raise Exception('{0}: Like could not be removed.' |
153 | .format(r.status_code)) | |
a7661afd | 154 | |
a993a4b6 MK |
155 | def reshare(self): |
156 | """This function reshares a post | |
157 | ||
158 | """ | |
7eb90eb7 | 159 | post_data = self.get_data() |
8095ac8b MK |
160 | |
161 | data = {'root_guid': post_data['guid'], | |
59ad210c | 162 | 'authenticity_token': self._connection.get_token()} |
a993a4b6 | 163 | |
b95ffe83 | 164 | r = self._connection.post('reshares', |
385e7ebe MM |
165 | data=data, |
166 | headers={'accept': 'application/json'}) | |
a7661afd MK |
167 | |
168 | if r.status_code != 201: | |
9088535d MK |
169 | raise Exception('{0}: Post could not be reshared.' |
170 | .format(r.status_code)) | |
a993a4b6 MK |
171 | |
172 | return r.json() | |
173 | ||
174 | def comment(self, text): | |
175 | """This function comments on a post | |
176 | ||
a993a4b6 MK |
177 | :param text: text to comment. |
178 | :type text: str | |
a993a4b6 | 179 | """ |
a993a4b6 | 180 | data = {'text': text, |
59ad210c | 181 | 'authenticity_token': self._connection.get_token()} |
a993a4b6 | 182 | |
b95ffe83 | 183 | r = self._connection.post('posts/{0}/comments'.format(self.post_id), |
385e7ebe MM |
184 | data=data, |
185 | headers={'accept': 'application/json'}) | |
a7661afd MK |
186 | |
187 | if r.status_code != 201: | |
9088535d MK |
188 | raise Exception('{0}: Comment could not be posted.' |
189 | .format(r.status_code)) | |
a993a4b6 MK |
190 | |
191 | return r.json() | |
ae221396 | 192 | |
5e809c8b | 193 | def delete_comment(self, comment_id): |
a993a4b6 MK |
194 | """This function removes a comment from a post |
195 | ||
91d2d5dc B |
196 | :param comment_id: id of the comment to remove. |
197 | :type comment_id: str | |
a993a4b6 | 198 | """ |
59ad210c | 199 | data = {'authenticity_token': self._connection.get_token()} |
a993a4b6 | 200 | |
b95ffe83 | 201 | r = self._connection.delete('posts/{0}/comments/{1}' |
385e7ebe MM |
202 | .format(self.post_id, |
203 | comment_id), | |
204 | data=data, | |
205 | headers={'accept': 'application/json'}) | |
a7661afd MK |
206 | |
207 | if r.status_code != 204: | |
9088535d MK |
208 | raise Exception('{0}: Comment could not be deleted.' |
209 | .format(r.status_code)) | |
5e809c8b MK |
210 | |
211 | def delete(self): | |
212 | """ This function deletes this post | |
5e809c8b | 213 | """ |
59ad210c | 214 | data = {'authenticity_token': self._connection.get_token()} |
b95ffe83 | 215 | r = self._connection.delete('posts/{0}'.format(self.post_id), |
385e7ebe MM |
216 | data=data, |
217 | headers={'accept': 'application/json'}) | |
65b91311 MM |
218 | if r.status_code != 204: |
219 | raise Exception('{0}: Post could not be deleted'.format(r.status_code)) |