From f947d5f08878f99d2787a12d7ebb8675276a59a7 Mon Sep 17 00:00:00 2001 From: dr Date: Tue, 3 Dec 2024 22:25:03 +0100 Subject: [PATCH] Add vote for rants and comments --- README.md | 1 - src/devranta/api.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 285207d..4b36d14 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,5 @@ See [tests](src/devranta/tests.py) for [examples](src/devranta/tests.py) on how ## Todo - - voting comment - edit message diff --git a/src/devranta/api.py b/src/devranta/api.py index 66eac6b..32df954 100644 --- a/src/devranta/api.py +++ b/src/devranta/api.py @@ -1,5 +1,11 @@ +from typing import Literal, Optional import aiohttp +from enum import Enum +class VoteReason(Enum): + NOT_FOR_ME = 0 + REPOST = 1 + OFFENSIVE_SPAM = 2 class Api: @@ -168,6 +174,28 @@ class Api: obj = await response.json() return obj.get("success", False) + async def vote_rant(self, rant_id: int, vote: Literal[-1, 0, 1], reason: Optional[VoteReason] = None): + if not await self.ensure_login(): + return None + async with self as session: + response = await session.post( + url=self.patch_url(f"devrant/rants/{rant_id}/vote"), + data=self.patch_auth({"vote": vote, "reason": reason.value if reason else None}), + ) + obj = await response.json() + return obj.get("success", False) + + async def vote_comment(self, comment_id: int, vote: Literal[-1, 0, 1], reason: Optional[VoteReason] = None): + if not await self.ensure_login(): + return None + async with self as session: + response = await session.post( + url=self.patch_url(f"comments/{comment_id}/vote"), + data=self.patch_auth({"vote": vote, "reason": reason.value if reason else None}), + ) + obj = await response.json() + return obj.get("success", False) + @property async def notifs(self): response = None