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
):
68 """Refreshes data of the notification.
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.
76 :param unread: which state set for notification
79 headers
= {'x-csrf-token': self
._connection
.get_token()}
80 params
= {'set_unread': json
.dumps(unread
)}
81 self
._connection
.put('notifications/{0}'.format(self
['id']), params
=params
, headers
=headers
)
82 self
.data
['unread'] = unread
86 """This class represents a post.
89 Remember that you need to have access to the post.
91 def __init__(self
, post_id
, connection
):
93 :param post_id: id or guid of the post
95 :param connection: connection object used to authenticate
96 :type connection: connection.Connection
98 self
._connection
= connection
99 self
.post_id
= post_id
102 """Returns string containing more information then str().
104 data
= self
.get_data()
105 return '{0} ({1}): {2}'.format(data
['author']['name'], data
['author']['diaspora_id'], data
['text'])
108 """Returns text of a post.
110 return self
.get_data()['text']
113 """This function retrieves data of the post.
115 r
= self
._connection
.get('posts/{0}.json'.format(self
.post_id
))
116 if r
.status_code
!= 200:
117 raise Exception('wrong status code: {0}'.format(r
.status_code
))
121 """This function likes a post.
122 It abstracts the 'Like' functionality.
124 :returns: dict -- json formatted like object.
126 data
= {'authenticity_token': self
._connection
.get_token()}
128 r
= self
._connection
.post('posts/{0}/likes'.format(self
.post_id
),
130 headers
={'accept': 'application/json'})
132 if r
.status_code
!= 201:
133 raise Exception('{0}: Post could not be liked.'
134 .format(r
.status_code
))
138 def delete_like(self
):
139 """This function removes a like from a post
141 data
= {'authenticity_token': self
._connection
.get_token()}
143 post_data
= self
.get_data()
145 r
= self
._connection
.delete('posts/{0}/likes/{1}'
146 .format(self
.post_id
,
147 post_data
['interactions']
151 if r
.status_code
!= 204:
152 raise Exception('{0}: Like could not be removed.'
153 .format(r
.status_code
))
156 """This function reshares a post
159 post_data
= self
.get_data()
161 data
= {'root_guid': post_data
['guid'],
162 'authenticity_token': self
._connection
.get_token()}
164 r
= self
._connection
.post('reshares',
166 headers
={'accept': 'application/json'})
168 if r
.status_code
!= 201:
169 raise Exception('{0}: Post could not be reshared.'
170 .format(r
.status_code
))
174 def comment(self
, text
):
175 """This function comments on a post
177 :param text: text to comment.
180 data
= {'text': text
,
181 'authenticity_token': self
._connection
.get_token()}
183 r
= self
._connection
.post('posts/{0}/comments'.format(self
.post_id
),
185 headers
={'accept': 'application/json'})
187 if r
.status_code
!= 201:
188 raise Exception('{0}: Comment could not be posted.'
189 .format(r
.status_code
))
193 def delete_comment(self
, comment_id
):
194 """This function removes a comment from a post
196 :param comment_id: id of the comment to remove.
197 :type comment_id: str
199 data
= {'authenticity_token': self
._connection
.get_token()}
201 r
= self
._connection
.delete('posts/{0}/comments/{1}'
202 .format(self
.post_id
,
205 headers
={'accept': 'application/json'})
207 if r
.status_code
!= 204:
208 raise Exception('{0}: Comment could not be deleted.'
209 .format(r
.status_code
))
212 """ This function deletes this post
214 data
= {'authenticity_token': self
._connection
.get_token()}
215 r
= self
._connection
.delete('posts/{0}'.format(self
.post_id
),
217 headers
={'accept': 'application/json'})
218 if r
.status_code
!= 204:
219 raise Exception('{0}: Post could not be deleted'.format(r
.status_code
))