From 7adaa0ef0f3407774ba6a6a23ea92e1e117ecc46 Mon Sep 17 00:00:00 2001 From: shorouqtv4 Date: Thu, 29 Jun 2023 17:38:42 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Add=20list=20functions=20for=20?= =?UTF-8?q?Mentions=20and=20Private=20Messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pythorhead/auth.py | 3 +++ pythorhead/lemmy.py | 2 ++ pythorhead/mention.py | 34 ++++++++++++++++++++++++++++++++++ pythorhead/private_message.py | 22 ++++++++++++++++++++++ pythorhead/requestor.py | 3 +++ 5 files changed, 64 insertions(+) create mode 100644 pythorhead/mention.py diff --git a/pythorhead/auth.py b/pythorhead/auth.py index 270a6ad..e9cb29b 100644 --- a/pythorhead/auth.py +++ b/pythorhead/auth.py @@ -8,6 +8,9 @@ class Authentication: def set_token(self, token: str) -> None: self.token = token + def get_token(self) -> Optional[str]: + return self.token + def set_api_base_url(self, base_url: str) -> None: self.api_url = f"{base_url}/api/v3" self.image_url = f"{base_url}/pictrs/image" diff --git a/pythorhead/lemmy.py b/pythorhead/lemmy.py index ba268c4..f18920d 100644 --- a/pythorhead/lemmy.py +++ b/pythorhead/lemmy.py @@ -9,6 +9,7 @@ from pythorhead.requestor import Requestor from pythorhead.site import Site from pythorhead.user import User +from pythorhead.mention import Mention class Lemmy: @@ -25,6 +26,7 @@ def __init__(self, api_base_url: str) -> None: self.user = User(self._requestor) self.private_message = PrivateMessage(self._requestor) self.image = Image(self._requestor) + self.mention = Mention(self._requestor) @property def nodeinfo(self): diff --git a/pythorhead/mention.py b/pythorhead/mention.py new file mode 100644 index 0000000..b591a5b --- /dev/null +++ b/pythorhead/mention.py @@ -0,0 +1,34 @@ +from typing import Any, Optional +from pythorhead.requestor import Request, Requestor +from pythorhead.types import SortType + + +class Mention: + def __init__(self, _requestor: Requestor): + self._requestor = _requestor + + def list( + self, + unread_only: bool, + sort: Optional[SortType] = None, + page: Optional[int] = None, + limit: Optional[int] = None, + ) -> Optional[dict]: + """ + List all user mentions + + Args: + unread_only (bool). + sort? (SortType) + page? (int). + limit? (int) + + Returns: + dict? mentions response + """ + + params = {"auth": self._requestor.get_auth_token()} + json: dict[str, Any] = {key: value for key, value in locals( + ).items() if value is not None and key != "self"} + + return self._requestor.api(Request.GET, "/user/mention", params=params, json=json) diff --git a/pythorhead/private_message.py b/pythorhead/private_message.py index 788c72d..a45c117 100644 --- a/pythorhead/private_message.py +++ b/pythorhead/private_message.py @@ -25,3 +25,25 @@ def create( return self._requestor.api(Request.POST, "/private_message", json=params) __call__ = create + + def list( + self, + unread_only: bool, + page: int, + limit: int + ) -> Optional[dict]: + """ + List private messages + + Args: + unread_only (bool). + page (int). + limit (int). + + Returns: + dict? private message response + """ + json: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"} + params = {"auth": self._requestor.get_auth_token()} + + return self._requestor.api(Request.GET, "/private_message/list", params=params, json=json) diff --git a/pythorhead/requestor.py b/pythorhead/requestor.py index 4d40503..608c035 100644 --- a/pythorhead/requestor.py +++ b/pythorhead/requestor.py @@ -97,6 +97,9 @@ def log_in(self, username_or_email: str, password: str) -> bool: if data := self.api(Request.POST, "/user/login", json=payload): self._auth.set_token(data["jwt"]) return self._auth.token is not None + + def get_auth_token(self) -> Optional[str]: + return self._auth.get_token() def log_out(self) -> None: self._auth.token = None From 1e000fa65b61d46a93024b903685494448fa8f55 Mon Sep 17 00:00:00 2001 From: shorouqtv4 Date: Thu, 29 Jun 2023 23:50:57 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=8F=AA=EF=B8=8F=20revert=20auth=20token?= =?UTF-8?q?=20helper=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pythorhead/auth.py | 3 --- pythorhead/requestor.py | 3 --- 2 files changed, 6 deletions(-) diff --git a/pythorhead/auth.py b/pythorhead/auth.py index e9cb29b..0e74992 100644 --- a/pythorhead/auth.py +++ b/pythorhead/auth.py @@ -7,9 +7,6 @@ class Authentication: def set_token(self, token: str) -> None: self.token = token - - def get_token(self) -> Optional[str]: - return self.token def set_api_base_url(self, base_url: str) -> None: self.api_url = f"{base_url}/api/v3" diff --git a/pythorhead/requestor.py b/pythorhead/requestor.py index 608c035..4d40503 100644 --- a/pythorhead/requestor.py +++ b/pythorhead/requestor.py @@ -97,9 +97,6 @@ def log_in(self, username_or_email: str, password: str) -> bool: if data := self.api(Request.POST, "/user/login", json=payload): self._auth.set_token(data["jwt"]) return self._auth.token is not None - - def get_auth_token(self) -> Optional[str]: - return self._auth.get_token() def log_out(self) -> None: self._auth.token = None From 0cb0af3c2280a9c0b92324df746f6bacf5203d16 Mon Sep 17 00:00:00 2001 From: shorouqtv4 Date: Thu, 29 Jun 2023 23:54:34 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20fix=20auth=20with=20private?= =?UTF-8?q?=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pythorhead/mention.py | 9 ++++----- pythorhead/private_message.py | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pythorhead/mention.py b/pythorhead/mention.py index b591a5b..0f42d77 100644 --- a/pythorhead/mention.py +++ b/pythorhead/mention.py @@ -26,9 +26,8 @@ def list( Returns: dict? mentions response """ - - params = {"auth": self._requestor.get_auth_token()} - json: dict[str, Any] = {key: value for key, value in locals( + unread_only = 'true' if unread_only else 'false' + + params: dict[str, Any] = {key: value for key, value in locals( ).items() if value is not None and key != "self"} - - return self._requestor.api(Request.GET, "/user/mention", params=params, json=json) + return self._requestor.api(Request.GET, "/user/mention", params=params) diff --git a/pythorhead/private_message.py b/pythorhead/private_message.py index a45c117..ed79edf 100644 --- a/pythorhead/private_message.py +++ b/pythorhead/private_message.py @@ -30,7 +30,7 @@ def list( self, unread_only: bool, page: int, - limit: int + limit: int = 20 ) -> Optional[dict]: """ List private messages @@ -38,12 +38,12 @@ def list( Args: unread_only (bool). page (int). - limit (int). + limit (int) with a max of 50. Returns: dict? private message response """ - json: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"} - params = {"auth": self._requestor.get_auth_token()} - - return self._requestor.api(Request.GET, "/private_message/list", params=params, json=json) + limit = 50 if limit > 50 else limit + unread_only = 'true' if unread_only else 'false' + params: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"} + return self._requestor.api(Request.GET, "/private_message/list", params=params) From 813da013e8ab336c818be210b895fb7db1100481 Mon Sep 17 00:00:00 2001 From: Shorouq <94386553+i-be-snek@users.noreply.github.com> Date: Fri, 30 Jun 2023 00:02:08 +0200 Subject: [PATCH 4/5] Fix auth token bug + remove deprecated code (#2) --- pythorhead/auth.py | 3 --- pythorhead/mention.py | 9 ++++----- pythorhead/private_message.py | 12 ++++++------ pythorhead/requestor.py | 3 --- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/pythorhead/auth.py b/pythorhead/auth.py index e9cb29b..0e74992 100644 --- a/pythorhead/auth.py +++ b/pythorhead/auth.py @@ -7,9 +7,6 @@ class Authentication: def set_token(self, token: str) -> None: self.token = token - - def get_token(self) -> Optional[str]: - return self.token def set_api_base_url(self, base_url: str) -> None: self.api_url = f"{base_url}/api/v3" diff --git a/pythorhead/mention.py b/pythorhead/mention.py index b591a5b..0f42d77 100644 --- a/pythorhead/mention.py +++ b/pythorhead/mention.py @@ -26,9 +26,8 @@ def list( Returns: dict? mentions response """ - - params = {"auth": self._requestor.get_auth_token()} - json: dict[str, Any] = {key: value for key, value in locals( + unread_only = 'true' if unread_only else 'false' + + params: dict[str, Any] = {key: value for key, value in locals( ).items() if value is not None and key != "self"} - - return self._requestor.api(Request.GET, "/user/mention", params=params, json=json) + return self._requestor.api(Request.GET, "/user/mention", params=params) diff --git a/pythorhead/private_message.py b/pythorhead/private_message.py index a45c117..ed79edf 100644 --- a/pythorhead/private_message.py +++ b/pythorhead/private_message.py @@ -30,7 +30,7 @@ def list( self, unread_only: bool, page: int, - limit: int + limit: int = 20 ) -> Optional[dict]: """ List private messages @@ -38,12 +38,12 @@ def list( Args: unread_only (bool). page (int). - limit (int). + limit (int) with a max of 50. Returns: dict? private message response """ - json: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"} - params = {"auth": self._requestor.get_auth_token()} - - return self._requestor.api(Request.GET, "/private_message/list", params=params, json=json) + limit = 50 if limit > 50 else limit + unread_only = 'true' if unread_only else 'false' + params: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"} + return self._requestor.api(Request.GET, "/private_message/list", params=params) diff --git a/pythorhead/requestor.py b/pythorhead/requestor.py index 608c035..4d40503 100644 --- a/pythorhead/requestor.py +++ b/pythorhead/requestor.py @@ -97,9 +97,6 @@ def log_in(self, username_or_email: str, password: str) -> bool: if data := self.api(Request.POST, "/user/login", json=payload): self._auth.set_token(data["jwt"]) return self._auth.token is not None - - def get_auth_token(self) -> Optional[str]: - return self._auth.get_token() def log_out(self) -> None: self._auth.token = None From 1febafdc5658be5de775f59c224aebef8b08664b Mon Sep 17 00:00:00 2001 From: shorouqtv4 Date: Fri, 30 Jun 2023 00:06:32 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=9A=A8=20fix=20lint=20+=20fix=20docst?= =?UTF-8?q?ring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pythorhead/auth.py | 2 +- pythorhead/private_message.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythorhead/auth.py b/pythorhead/auth.py index 0e74992..270a6ad 100644 --- a/pythorhead/auth.py +++ b/pythorhead/auth.py @@ -7,7 +7,7 @@ class Authentication: def set_token(self, token: str) -> None: self.token = token - + def set_api_base_url(self, base_url: str) -> None: self.api_url = f"{base_url}/api/v3" self.image_url = f"{base_url}/pictrs/image" diff --git a/pythorhead/private_message.py b/pythorhead/private_message.py index ed79edf..4fb691d 100644 --- a/pythorhead/private_message.py +++ b/pythorhead/private_message.py @@ -38,7 +38,7 @@ def list( Args: unread_only (bool). page (int). - limit (int) with a max of 50. + limit (int) with a max of 50, defaults to 20. Returns: dict? private message response