7 """This module is only imported in other diaspy modules and
8 MUST NOT import anything.
13 """This class represents an aspect.
15 def __init__(self
, connection
, id=-1):
16 self
._connection
= connection
20 def addUser(self
, user_id
):
21 """Add user to current aspect.
23 :param user_id: user to add to aspect
26 data
= {'authenticity_token': self
._connection
.get_token(),
30 request
= self
._connection
.post('aspect_memberships.json', data
=data
)
32 if request
.status_code
!= 201:
33 raise Exception('wrong status code: {0}'.format(request
.status_code
))
36 def removeUser(self
, user_id
):
37 """Remove user from current aspect.
39 :param user_id: user to remove from aspect
42 data
= {'authenticity_token': self
._connection
.get_token(),
46 request
= self
.connection
.delete('aspect_memberships/{0}.json'.format(self
.id), data
=data
)
48 if request
.status_code
!= 200:
49 raise Exception('wrong status code: {0}'.format(request
.status_code
))
54 """This class represents single notification.
56 def __init__(self
, connection
, data
):
57 self
._connection
= connection
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']
64 def __getitem__(self
, key
):
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.
72 :param unread: which state set for notification
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
83 """This class represents a post.
86 Remember that you need to have access to the post.
88 def __init__(self
, post_id
, connection
):
90 :param post_id: id or guid of the post
92 :param connection: connection object used to authenticate
93 :type connection: connection.Connection
95 self
._connection
= connection
96 self
.post_id
= post_id
99 """Returns string containing more information then str().
101 data
= self
.get_data()
102 return '{0} ({1}): {2}'.format(data
['author']['name'], data
['author']['diaspora_id'], data
['text'])
105 """Returns text of a post.
107 return self
.get_data()['text']
110 """This function retrieves data of the post.
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
))
118 """This function likes a post.
119 It abstracts the 'Like' functionality.
121 :returns: dict -- json formatted like object.
123 data
= {'authenticity_token': self
._connection
.get_token()}
125 r
= self
._connection
.post('posts/{0}/likes'.format(self
.post_id
),
127 headers
={'accept': 'application/json'})
129 if r
.status_code
!= 201:
130 raise Exception('{0}: Post could not be liked.'
131 .format(r
.status_code
))
135 def delete_like(self
):
136 """This function removes a like from a post
138 data
= {'authenticity_token': self
._connection
.get_token()}
140 post_data
= self
.get_data()
142 r
= self
._connection
.delete('posts/{0}/likes/{1}'
143 .format(self
.post_id
,
144 post_data
['interactions']
148 if r
.status_code
!= 204:
149 raise Exception('{0}: Like could not be removed.'
150 .format(r
.status_code
))
153 """This function reshares a post
156 post_data
= self
.get_data()
158 data
= {'root_guid': post_data
['guid'],
159 'authenticity_token': self
._connection
.get_token()}
161 r
= self
._connection
.post('reshares',
163 headers
={'accept': 'application/json'})
165 if r
.status_code
!= 201:
166 raise Exception('{0}: Post could not be reshared.'
167 .format(r
.status_code
))
171 def comment(self
, text
):
172 """This function comments on a post
174 :param text: text to comment.
177 data
= {'text': text
,
178 'authenticity_token': self
._connection
.get_token()}
180 r
= self
._connection
.post('posts/{0}/comments'.format(self
.post_id
),
182 headers
={'accept': 'application/json'})
184 if r
.status_code
!= 201:
185 raise Exception('{0}: Comment could not be posted.'
186 .format(r
.status_code
))
190 def delete_comment(self
, comment_id
):
191 """This function removes a comment from a post
193 :param comment_id: id of the comment to remove.
194 :type comment_id: str
196 data
= {'authenticity_token': self
._connection
.get_token()}
198 r
= self
._connection
.delete('posts/{0}/comments/{1}'
199 .format(self
.post_id
,
202 headers
={'accept': 'application/json'})
204 if r
.status_code
!= 204:
205 raise Exception('{0}: Comment could not be deleted.'
206 .format(r
.status_code
))
209 """ This function deletes this post
211 data
= {'authenticity_token': self
._connection
.get_token()}
212 r
= self
._connection
.delete('posts/{0}'.format(self
.post_id
),
214 headers
={'accept': 'application/json'})
215 if r
.status_code
!= 204:
216 raise Exception('{0}: Post could not be deleted'.format(r
.status_code
))