Skip to content

Commit 5578f2b

Browse files
authored
feat: Adds endpoint for custom emoji (#107)
1 parent 0697fc3 commit 5578f2b

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

examples/emoji.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
## python examples/emoji.py db0
3+
4+
import os
5+
import argparse
6+
import json
7+
from pythorhead import Lemmy
8+
9+
10+
arg_parser = argparse.ArgumentParser()
11+
arg_parser.add_argument('username', action="store")
12+
arg_parser.add_argument('-d', '--lemmy_domain', action='store', required=False, type=str, help="the domain in which to look for this user")
13+
arg_parser.add_argument('-u', '--lemmy_username', action='store', required=False, type=str, help="Which user to authenticate as")
14+
arg_parser.add_argument('-p', '--lemmy_password', action='store', required=False, type=str, help="Which password to authenticate with")
15+
args = arg_parser.parse_args()
16+
17+
18+
19+
lemmy_domain = args.lemmy_domain
20+
if not lemmy_domain:
21+
lemmy_domain = os.getenv('LEMMY_DOMAIN', "lemmy.dbzer0.com")
22+
if not lemmy_domain:
23+
raise Exception("You need to provide a lemmy domain via env var or arg")
24+
25+
lemmy_username = args.lemmy_username
26+
if not lemmy_username:
27+
lemmy_username = os.getenv("LEMMY_USERNAME")
28+
29+
lemmy_password = args.lemmy_password
30+
if not lemmy_password:
31+
lemmy_password = os.getenv("LEMMY_PASSWORD")
32+
33+
lemmy = Lemmy(f"https://{lemmy_domain}", raise_exceptions=True)
34+
if lemmy_username and lemmy_password:
35+
login = lemmy.log_in(lemmy_username, lemmy_password)
36+
emoji = lemmy.emoji.get()
37+
if emoji:
38+
print(json.dumps(emoji, indent=4))
39+
else:
40+
print("Retrieval of emoji information failed")

examples/site.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
## python examples/user.py db0
2+
## python examples/site.py db0
33

44
import os
55
import argparse
@@ -35,6 +35,6 @@
3535
login = lemmy.log_in(lemmy_username, lemmy_password)
3636
site = lemmy.site.get()
3737
if site:
38-
print(json.dumps(site["admins"], indent=4))
38+
print(json.dumps(site, indent=4))
3939
else:
4040
print("Retrieval of site information failed")

pythorhead/emoji.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from typing import Any, List, Optional, Union
2+
3+
from pythorhead.requestor import Request, Requestor
4+
from pythorhead.types import ListingType, LanguageType
5+
6+
7+
class Emoji:
8+
def __init__(self, _requestor: Requestor):
9+
self._requestor = _requestor
10+
11+
def get(
12+
self,
13+
) -> Optional[dict]:
14+
"""
15+
Get a list of all defined custom emoji
16+
17+
Returns:
18+
dict: emoji view
19+
"""
20+
return self._requestor.api(Request.GET, "/site")["custom_emojis"]
21+
22+
# FIXME: untested
23+
def edit(
24+
self,
25+
emoji_id: int,
26+
category: str,
27+
image_url: str,
28+
alt_text: str,
29+
keywords: str,
30+
) -> dict:
31+
"""
32+
Admin/Mod Edit an existing custom emoji
33+
34+
Args:
35+
emoji_id (int): The ID of the emoji to edit
36+
category (str): The category of the emoji
37+
image_url (str): The URL of the emoji image
38+
alt_text (str): The alt text for the emoji
39+
keywords (str): The keywords associated with the emoji
40+
41+
Returns:
42+
dict: The edited emoji view
43+
"""
44+
emoji_data = {
45+
"id": emoji_id,
46+
"category": category,
47+
"image_url": image_url,
48+
"alt_text": alt_text,
49+
"keywords": keywords,
50+
}
51+
52+
return self._requestor.api(Request.PUT, f"/custom_emoji", json=emoji_data)
53+
54+
# FIXME: untested
55+
def create(
56+
self,
57+
emoji_id: int,
58+
category: str,
59+
image_url: str,
60+
alt_text: str,
61+
keywords: str,
62+
) -> dict:
63+
"""
64+
Admin/Mod Create a new custom emoji
65+
66+
Args:
67+
if (int): The ID of the emoji to edit
68+
category (str): The category of the emoji
69+
image_url (str): The URL of the emoji image
70+
alt_text (str): The alt text for the emoji
71+
keywords (str): The keywords associated with the emoji
72+
73+
Returns:
74+
dict: The edited emoji view
75+
"""
76+
emoji_data = {
77+
"id": emoji_id,
78+
"category": category,
79+
"image_url": image_url,
80+
"alt_text": alt_text,
81+
"keywords": keywords,
82+
}
83+
84+
return self._requestor.api(Request.POST, f"/custom_emoji", json=emoji_data)
85+
86+
# FIXME: untested
87+
def delete(
88+
self,
89+
emoji_id: int,
90+
) -> bool:
91+
"""
92+
Admin/Mod delete an existing custom emoji
93+
94+
Args:
95+
emoji_id (int): The ID of the emoji to edit
96+
97+
Returns:
98+
True if the emoji was deleted successfully. Else false.
99+
"""
100+
emoji_data = {
101+
"id": emoji_id,
102+
}
103+
104+
return self._requestor.api(Request.POST, f"/custom_emoji/delete", json=emoji_data) == 'OK'

pythorhead/lemmy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pythorhead.types import FeatureType, ListingType, SortType, SearchType, SearchOption
1515
from pythorhead.user import User
1616
from pythorhead.admin import Admin
17+
from pythorhead.emoji import Emoji
1718
from pythorhead.classes.user import LemmyUser
1819

1920
logger = logging.getLogger(__name__)
@@ -30,6 +31,7 @@ class Lemmy:
3031
image: Image
3132
mention: Mention
3233
admin: Admin
34+
emoji: Emoji
3335

3436
def __init__(self, api_base_url: str, raise_exceptions = False, request_timeout=3) -> None:
3537
self._requestor = Requestor(raise_exceptions, request_timeout)
@@ -43,6 +45,7 @@ def __init__(self, api_base_url: str, raise_exceptions = False, request_timeout=
4345
self.image = Image(self._requestor)
4446
self.mention = Mention(self._requestor)
4547
self.admin = Admin(self._requestor)
48+
self.emoji = Emoji(self._requestor)
4649

4750
@property
4851
def nodeinfo(self):

0 commit comments

Comments
 (0)