Skip to content

Commit

Permalink
Made post and comment equivalent
Browse files Browse the repository at this point in the history
Both now have user and community object making the duck typing easier.
  • Loading branch information
isFakeAccount committed Dec 18, 2023
1 parent 207b001 commit 5367a16
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 60 deletions.
2 changes: 1 addition & 1 deletion async_lemmy_py/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
comment_dict: dict[str, Any],
) -> None:
self.request_builder = request_builder
self.post: Post = Post.from_dict(post, request_builder)
self.post: Post = Post.from_dict(post_view={"post": post, "community": community, "creator": user}, request_builder=request_builder)
self.community: Community = Community.from_dict(community)
self.user: User = User.from_dict(user)

Expand Down
98 changes: 39 additions & 59 deletions async_lemmy_py/models/post.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,59 @@
from datetime import datetime
from typing import Optional, Any, Self
from typing import Any, Self

from async_lemmy_py.models.community import Community
from async_lemmy_py.models.user import User
from async_lemmy_py.request_builder import RequestBuilder


class Post:
"""Represents a post."""

def __init__(
self,
request_builder: RequestBuilder,
id: int,
name: str,
creator_id: int,
community_id: int,
removed: bool,
locked: bool,
published: datetime,
deleted: bool,
nsfw: bool,
ap_id: str,
local: bool,
language_id: int,
featured_community: bool,
featured_local: bool,
body: Optional[str] = None,
embed_description: Optional[str] = None,
embed_title: Optional[str] = None,
embed_video_url: Optional[str] = None,
thumbnail_url: Optional[str] = None,
updated: Optional[str] = None,
url: Optional[str] = None,
) -> None:
def __init__(self, request_builder: RequestBuilder, community: dict[str, Any], user: dict[str, Any], post_dict: dict[str, Any]):
self.request_builder = request_builder
self.id = id
self.name = name
self.creator_id = creator_id
self.community_id = community_id
self.removed = removed
self.locked = locked
self.published = published
self.deleted = deleted
self.nsfw = nsfw
self.ap_id = ap_id
self.local = local
self.language_id = language_id
self.featured_community = featured_community
self.featured_local = featured_local
self.body = body
self.embed_description = embed_description
self.embed_title = embed_title
self.embed_video_url = embed_video_url
self.thumbnail_url = thumbnail_url
self.updated = updated
self.url = url
self.community: Community = Community.from_dict(community)
self.user: User = User.from_dict(user)

self.ap_id = post_dict.get("ap_id", "")
self.body = post_dict.get("body")
self.community_id = post_dict.get("community_id", -1)
self.creator_id = post_dict.get("creator_id", -1)
self.deleted = post_dict.get("deleted", False)
self.embed_description = post_dict.get("embed_description")
self.embed_title = post_dict.get("embed_title")
self.embed_video_url = post_dict.get("embed_video_url")
self.featured_community = post_dict.get("featured_community", False)
self.featured_local = post_dict.get("featured_local", False)
self.language_id = post_dict.get("language_id", -1)
self.local = post_dict.get("local", False)
self.locked = post_dict.get("locked", False)
self.name = post_dict.get("name", "")
self.nsfw = post_dict.get("nsfw", False)
self.post_id = post_dict.get("id", -1)
self.published = datetime.fromisoformat(post_dict.get("published", "1970-01-01T00:00:00Z"))
self.removed = post_dict.get("removed", False)
self.thumbnail_url = post_dict.get("thumbnail_url")
self.updated = datetime.fromisoformat(post_dict.get("updated", "1970-01-01T00:00:00Z"))
self.url = post_dict.get("url")

@classmethod
def from_dict(cls, data: dict[str, Any], request_builder: RequestBuilder) -> Self:
def from_dict(cls, *, post_view: dict[str, Any], request_builder: RequestBuilder) -> Self:
"""Create a Post instance from a dictionary.
:param dict data: The dictionary containing post data.
:param dict post_view: The dictionary containing post data.
:param RequestBuilder request_builder: An instance of the RequestBuilder.
:returns: An instance of the Post class.
:rtype: Post
"""
data_copy = data.copy()

published_str = data_copy.get("published")
if published_str:
data_copy["published"] = datetime.fromisoformat(published_str)

data_copy["request_builder"] = request_builder
return cls(**data_copy)
post_dict = post_view["post"]
return cls(
request_builder=request_builder,
community=post_view["community"],
user=post_view["creator"],
post_dict=post_dict,
)

@classmethod
async def from_id(cls, post_id: int, request_builder: RequestBuilder) -> Self:
Expand All @@ -87,7 +67,7 @@ async def from_id(cls, post_id: int, request_builder: RequestBuilder) -> Self:
"""
post_data = await request_builder.get("post", params={"id": post_id})
return cls.from_dict(post_data["post_view"]["post"], request_builder=request_builder)
return cls.from_dict(post_view=post_data["post_view"], request_builder=request_builder)

async def parent(self) -> Self:
"""Exists for duck typing."""
Expand All @@ -96,7 +76,7 @@ async def parent(self) -> Self:
async def reply(self, response: str) -> None:
payload = {
"content": response,
"post_id": self.id,
"post_id": self.post_id,
"language_id": self.language_id,
}
await self.request_builder.post("comment", json=payload)

0 comments on commit 5367a16

Please sign in to comment.