8 """This module is only imported in other diaspy modules and
9 MUST NOT import anything.
14 """This class represents an aspect.
16 def __init__(self
, connection
, id=-1):
17 self
._connection
= connection
22 """Returns list of users who are listed in this aspect.
26 def addUser(self
, user_id
):
27 """Add user to current aspect.
29 :param user_id: user to add to aspect
32 data
= {'authenticity_token': self
._connection
.get_token(),
36 request
= self
._connection
.post('aspect_memberships.json', data
=data
)
38 if request
.status_code
!= 201:
39 raise Exception('wrong status code: {0}'.format(request
.status_code
))
42 def removeUser(self
, user_id
):
43 """Remove user from current aspect.
45 :param user_id: user to remove from aspect
48 data
= {'authenticity_token': self
._connection
.get_token(),
52 request
= self
.connection
.delete('aspect_memberships/{0}.json'.format(self
.id), data
=data
)
54 if request
.status_code
!= 200:
55 raise Exception('wrong status code: {0}'.format(request
.status_code
))
60 """This class represents single notification.
62 _who_regexp
= re
.compile(r
'/people/[0-9a-z]+" class=\'hovercardable
')
63 _when_regexp = re.compile(r'[0-9]{4,4}(-[0-9]{2,2}){2,2} [0-9]{2,2}(:[0-9]{2,2}){2,2} UTC
')
65 def __init__(self, connection, data):
66 self._connection = connection
67 self.type = list(data.keys())[0]
68 self.data = data[self.type]
69 self.id = self.data['id']
70 self.unread = self.data['unread
']
72 def __getitem__(self, key):
73 """Returns a key from notification data.
78 """Returns notification note.
80 string = re.sub('</?
[a
-z
]+( *[a
-z_
-]+=["\'][\w():.,!?#/\- ]*["\'])* */?
>', '', self.data['note_html
'])
81 string = string.strip().split('\n')[0]
82 while ' ' in string: string = string.replace(' ', ' ')
86 """Returns notification note with more details.
88 return '{0}
: {1}
'.format(self.when(), str(self))
91 """Returns list of guids of the users who caused you to get the notification.
93 return [who[8:24] for who in self._who_regexp.findall(self.data['note_html
'])]
96 """Returns UTC time as found in note_html.
98 return self._when_regexp.search(self.data['note_html
']).group(0)
100 def mark(self, unread=False):
101 """Marks notification to read/unread.
102 Marks notification to read if `unread` is False.
103 Marks notification to unread if `unread` is True.
105 :param unread: which state set for notification
108 headers = {'x
-csrf
-token
': self._connection.get_token()}
109 params = {'set_unread
': json.dumps(unread)}
110 self._connection.put('notifications
/{0}
'.format(self['id']), params=params, headers=headers)
111 self.data['unread
'] = unread
115 """This class represents a post.
118 Remember that you need to have access to the post.
120 def __init__(self, post_id, connection):
122 :param post_id: id or guid of the post
124 :param connection: connection object used to authenticate
125 :type connection: connection.Connection
127 self._connection = connection
128 self.post_id = post_id
131 """Returns string containing more information then str().
133 data = self.get_data()
134 return '{0}
({1}
): {2}
'.format(data['author
']['name
'], data['author
']['diaspora_id
'], data['text
'])
137 """Returns text of a post.
139 return self.get_data()['text
']
142 """This function retrieves data of the post.
144 r = self._connection.get('posts
/{0}
.json
'.format(self.post_id))
145 if r.status_code != 200:
146 raise Exception('wrong status code
: {0}
'.format(r.status_code))
150 """This function likes a post.
151 It abstracts the 'Like
' functionality.
153 :returns: dict -- json formatted like object.
155 data = {'authenticity_token
': self._connection.get_token()}
157 r = self._connection.post('posts
/{0}
/likes
'.format(self.post_id),
159 headers={'accept
': 'application
/json
'})
161 if r.status_code != 201:
162 raise Exception('{0}
: Post could
not be liked
.'
163 .format(r.status_code))
167 def delete_like(self):
168 """This function removes a like from a post
170 data = {'authenticity_token
': self._connection.get_token()}
172 post_data = self.get_data()
174 r = self._connection.delete('posts
/{0}
/likes
/{1}
'
175 .format(self.post_id,
176 post_data['interactions
']
180 if r.status_code != 204:
181 raise Exception('{0}
: Like could
not be removed
.'
182 .format(r.status_code))
185 """This function reshares a post
188 post_data = self.get_data()
190 data = {'root_guid
': post_data['guid
'],
191 'authenticity_token
': self._connection.get_token()}
193 r = self._connection.post('reshares
',
195 headers={'accept
': 'application
/json
'})
197 if r.status_code != 201:
198 raise Exception('{0}
: Post could
not be reshared
.'
199 .format(r.status_code))
203 def comment(self, text):
204 """This function comments on a post
206 :param text: text to comment.
209 data = {'text
': text,
210 'authenticity_token
': self._connection.get_token()}
212 r = self._connection.post('posts
/{0}
/comments
'.format(self.post_id),
214 headers={'accept
': 'application
/json
'})
216 if r.status_code != 201:
217 raise Exception('{0}
: Comment could
not be posted
.'
218 .format(r.status_code))
222 def delete_comment(self, comment_id):
223 """This function removes a comment from a post
225 :param comment_id: id of the comment to remove.
226 :type comment_id: str
228 data = {'authenticity_token
': self._connection.get_token()}
230 r = self._connection.delete('posts
/{0}
/comments
/{1}
'
231 .format(self.post_id,
234 headers={'accept
': 'application
/json
'})
236 if r.status_code != 204:
237 raise Exception('{0}
: Comment could
not be deleted
.'
238 .format(r.status_code))
241 """ This function deletes this post
243 data = {'authenticity_token
': self._connection.get_token()}
244 r = self._connection.delete('posts
/{0}
'.format(self.post_id),
246 headers={'accept
': 'application
/json
'})
247 if r.status_code != 204:
248 raise Exception('{0}
: Post could
not be deleted
'.format(r.status_code))