Implemented methods in Postly client for likes
Browse files
postly/clients/postly_client.py
CHANGED
@@ -214,3 +214,36 @@ class PostlyClient:
|
|
214 |
|
215 |
# retrieve top topics in descending order
|
216 |
return [topic for topic, _ in topics_frequency.most_common()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
# retrieve top topics in descending order
|
216 |
return [topic for topic, _ in topics_frequency.most_common()]
|
217 |
+
|
218 |
+
def like_post(self, user_name: str, post_id: int) -> None:
|
219 |
+
"""
|
220 |
+
Like or unlike a post.
|
221 |
+
|
222 |
+
Args:
|
223 |
+
user_name: The name of the user liking the post.
|
224 |
+
post_id: The ID of the post to like or unlike.
|
225 |
+
Returns:
|
226 |
+
None
|
227 |
+
"""
|
228 |
+
post = self.get_post_by_id(post_id)
|
229 |
+
if user_name in post.liked_by:
|
230 |
+
post.liked_by.remove(user_name)
|
231 |
+
post.likes -= 1
|
232 |
+
else:
|
233 |
+
post.liked_by.add(user_name)
|
234 |
+
post.likes += 1
|
235 |
+
|
236 |
+
def get_post_by_id(self, post_id: int) -> Post:
|
237 |
+
"""
|
238 |
+
Get a post by its ID.
|
239 |
+
|
240 |
+
Args:
|
241 |
+
post_id: The ID of the post to retrieve.
|
242 |
+
Returns:
|
243 |
+
The post with the given ID.
|
244 |
+
"""
|
245 |
+
for user_posts in self.userPosts.values():
|
246 |
+
for post in user_posts:
|
247 |
+
if post.timestamp == post_id:
|
248 |
+
return post
|
249 |
+
raise KeyError("Post not found")
|