Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add list functions for Mentions and Private Messages #34

Merged
merged 8 commits into from
Jun 30, 2023
2 changes: 2 additions & 0 deletions pythorhead/lemmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions pythorhead/mention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
"""
unread_only = 'true' if unread_only else 'false'
NicKoehler marked this conversation as resolved.
Show resolved Hide resolved

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)
22 changes: 22 additions & 0 deletions pythorhead/private_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 20
) -> Optional[dict]:
"""
List private messages

Args:
unread_only (bool).
page (int).
limit (int) with a max of 50, defaults to 20.

Returns:
dict? private message response
"""
limit = 50 if limit > 50 else limit
unread_only = 'true' if unread_only else 'false'
NicKoehler marked this conversation as resolved.
Show resolved Hide resolved
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)