Notifications can now be marked as `read` or `unread`
[diaspy.git] / diaspy / models.py
1 #!/usr/bin/env python3
2
3
4 import json
5
6
7 """This module is only imported in other diaspy modules and
8 MUST NOT import anything.
9 """
10
11
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
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
67 def mark(self, unread=False):
68 """Marks notification to read/unread.
69 Marks notification to read if `unread` is False.
70 Marks notification to unread if `unread` is True.
71
72 :param unread: which state set for notification
73 :type unread: bool
74 """
75 headers = {'x-csrf-token': self._connection.get_token()}
76 params = {'set_unread': json.dumps(unread)}
77 print(json.dumps(False))
78 self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers)
79 self.data['unread'] = unread
80
81
82 class Post():
83 """This class represents a post.
84
85 .. note::
86 Remember that you need to have access to the post.
87 """
88 def __init__(self, post_id, connection):
89 """
90 :param post_id: id or guid of the post
91 :type post_id: str
92 :param connection: connection object used to authenticate
93 :type connection: connection.Connection
94 """
95 self._connection = connection
96 self.post_id = post_id
97
98 def __repr__(self):
99 """Returns string containing more information then str().
100 """
101 data = self.get_data()
102 return '{0} ({1}): {2}'.format(data['author']['name'], data['author']['diaspora_id'], data['text'])
103
104 def __str__(self):
105 """Returns text of a post.
106 """
107 return self.get_data()['text']
108
109 def get_data(self):
110 """This function retrieves data of the post.
111 """
112 r = self._connection.get('posts/{0}.json'.format(self.post_id))
113 if r.status_code != 200:
114 raise Exception('wrong status code: {0}'.format(r.status_code))
115 return r.json()
116
117 def like(self):
118 """This function likes a post.
119 It abstracts the 'Like' functionality.
120
121 :returns: dict -- json formatted like object.
122 """
123 data = {'authenticity_token': self._connection.get_token()}
124
125 r = self._connection.post('posts/{0}/likes'.format(self.post_id),
126 data=data,
127 headers={'accept': 'application/json'})
128
129 if r.status_code != 201:
130 raise Exception('{0}: Post could not be liked.'
131 .format(r.status_code))
132
133 return r.json()
134
135 def delete_like(self):
136 """This function removes a like from a post
137 """
138 data = {'authenticity_token': self._connection.get_token()}
139
140 post_data = self.get_data()
141
142 r = self._connection.delete('posts/{0}/likes/{1}'
143 .format(self.post_id,
144 post_data['interactions']
145 ['likes'][0]['id']),
146 data=data)
147
148 if r.status_code != 204:
149 raise Exception('{0}: Like could not be removed.'
150 .format(r.status_code))
151
152 def reshare(self):
153 """This function reshares a post
154
155 """
156 post_data = self.get_data()
157
158 data = {'root_guid': post_data['guid'],
159 'authenticity_token': self._connection.get_token()}
160
161 r = self._connection.post('reshares',
162 data=data,
163 headers={'accept': 'application/json'})
164
165 if r.status_code != 201:
166 raise Exception('{0}: Post could not be reshared.'
167 .format(r.status_code))
168
169 return r.json()
170
171 def comment(self, text):
172 """This function comments on a post
173
174 :param text: text to comment.
175 :type text: str
176 """
177 data = {'text': text,
178 'authenticity_token': self._connection.get_token()}
179
180 r = self._connection.post('posts/{0}/comments'.format(self.post_id),
181 data=data,
182 headers={'accept': 'application/json'})
183
184 if r.status_code != 201:
185 raise Exception('{0}: Comment could not be posted.'
186 .format(r.status_code))
187
188 return r.json()
189
190 def delete_comment(self, comment_id):
191 """This function removes a comment from a post
192
193 :param comment_id: id of the comment to remove.
194 :type comment_id: str
195 """
196 data = {'authenticity_token': self._connection.get_token()}
197
198 r = self._connection.delete('posts/{0}/comments/{1}'
199 .format(self.post_id,
200 comment_id),
201 data=data,
202 headers={'accept': 'application/json'})
203
204 if r.status_code != 204:
205 raise Exception('{0}: Comment could not be deleted.'
206 .format(r.status_code))
207
208 def delete(self):
209 """ This function deletes this post
210 """
211 data = {'authenticity_token': self._connection.get_token()}
212 r = self._connection.delete('posts/{0}'.format(self.post_id),
213 data=data,
214 headers={'accept': 'application/json'})
215 if r.status_code != 204:
216 raise Exception('{0}: Post could not be deleted'.format(r.status_code))