Skip to content

Commit

Permalink
Merge pull request #44 from db0/purge
Browse files Browse the repository at this point in the history
Purge for Communities, Comments, Posts and Users
  • Loading branch information
NicKoehler authored Jul 4, 2023
2 parents 58f864b + e69761e commit 6165313
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
if not lemmy_password:
raise Exception("You need to provide a lemmy password via env var or arg")

lemmy = Lemmy(f"http://{lemmy_domain}")
lemmy = Lemmy(f"https://{lemmy_domain}")
if not lemmy.log_in(lemmy_username, lemmy_password):
print("Failed to log in")
exit(1)
Expand Down
25 changes: 23 additions & 2 deletions pythorhead/comment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Literal, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import CommentSortType, ListingType, LanguageType
from pythorhead.types import CommentSortType, LanguageType, ListingType


class Comment:
Expand Down Expand Up @@ -129,7 +129,7 @@ def edit(
Returns:
Optional[dict]: edited comment data if successful
"""
edit_comment: dict[Any, Any] = {
edit_comment: dict[str, Any] = {
"comment_id": comment_id,
}
if content is not None:
Expand Down Expand Up @@ -277,4 +277,25 @@ def mark_as_read(self, comment_reply_id: int, read: bool) -> Optional[dict]:
}
return self._requestor.api(Request.POST, "/comment/mark_as_read", json=mark_as_read_comment)

def purge(self, id: int, reason: Optional[str] = None) -> Optional[dict]:
"""
Admin purge / delete a comment from the database
Args:
id (int)
reason (Optional[str]): Defaults to None
Returns:
Optional[dict]: purge result if successful
"""

purge_comment: dict[str, Any] = {
"comment_id": id,
}

if reason is not None:
purge_comment["reason"] = reason

return self._requestor.api(Request.POST, "/admin/purge/comment", json=purge_comment)

__call__ = create
33 changes: 27 additions & 6 deletions pythorhead/community.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List, Optional, Union

from typing import Any, List, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import ListingType, SortType, LanguageType
from pythorhead.types import LanguageType, ListingType, SortType


class Community:
Expand Down Expand Up @@ -34,7 +33,7 @@ def create(
Returns:
Optional[dict]: post data if successful
"""
new_community: dict[Any, Any] = {
new_community: dict[str, Any] = {
"name": name,
"title": title,
}
Expand All @@ -47,8 +46,9 @@ def create(
if [posting_restricted_to_mods] is not None:
new_community["[posting_restricted_to_mods]"] = [posting_restricted_to_mods]
if discussion_languages is not None:
new_community["discussion_languages"] = [l.value for l in discussion_languages
if isinstance(l, LanguageType)]
new_community["discussion_languages"] = [
language.value for language in discussion_languages if isinstance(language, LanguageType)
]

return self._requestor.api(Request.POST, "/community", json=new_community)

Expand Down Expand Up @@ -127,3 +127,24 @@ def follow(self, id: int, follow: Optional[bool] = True):
if data := self._requestor.api(Request.POST, "/community/follow", json=follow_community):
return data["community_view"]
return None

def purge(self, id: int, reason: Optional[str] = None) -> Optional[dict]:
"""
Admin purge / delete a community from the database
Args:
id (int)
reason (Optional[str]): Defaults to None
Returns:
Optional[dict]: purge result if successful
"""

purge_community: dict[str, Any] = {
"community_id": id,
}

if reason is not None:
purge_community["reason"] = reason

return self._requestor.api(Request.POST, "/admin/purge/community", json=purge_community)
21 changes: 20 additions & 1 deletion pythorhead/post.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Literal, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import FeatureType, ListingType, SortType, LanguageType
from pythorhead.types import FeatureType, LanguageType, ListingType, SortType


class Post:
Expand Down Expand Up @@ -335,4 +335,23 @@ def site_metadata(self, url: str) -> Optional[dict]:
}
return self._requestor.api(Request.GET, "/post/site_metadata", params=site_metadata_post)

def purge(self, id: int, reason: Optional[str]) -> Optional[dict]:
"""
Admin purge / delete a post from the database
Args:
id (int)
reason (Optional[str]): Defaults to None
Returns:
Optional[dict]: purge result if successful
"""
purge_post: dict[str, Any] = {
"post_id": id,
}
if reason is not None:
purge_post["reason"] = reason

return self._requestor.api(Request.POST, "/admin/purge/post", json=purge_post)

__call__ = create
19 changes: 19 additions & 0 deletions pythorhead/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ def get(
"""
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", params=params)

def purge(self, id: int, reason: Optional[str] = None) -> Optional[dict]:
"""
Admin purge / delete a person from the database
Args:
id (int)
reason (Optional[str]): Defaults to None
Returns:
Optional[dict]: purge result if successful
"""

user_purge: dict[str, Any] = {"person_id": id}

if reason is not None:
user_purge["reason"] = reason

return self._requestor.api(Request.POST, "/admin/purge/person", json=user_purge)

0 comments on commit 6165313

Please sign in to comment.