React to something

This commit is contained in:
simonwt 2026-04-02 23:51:29 +01:00
parent 7bca418530
commit 60351f7cdb
14 changed files with 159 additions and 7 deletions

View file

@ -0,0 +1,2 @@
from .react import create
from .listbyparent import listbyparent

View file

@ -0,0 +1,10 @@
def getbyparent(API, parent_sk: str) -> dict:
"""
Fetches the users reactions for a given parent from the API.
Args:
parent_sk (str): sk of the parent to fetch reactions for.
Returns:
dict: The list of reactions.
"""
reaction_list = API.make_request("GET", "content", f"getreactionbysk/{parent_sk}", authenticate=True)
return reaction_list

View file

@ -0,0 +1,11 @@
def listbyparent(API, parent_sk: str) -> dict:
"""
Fetches the list of reactions for a given parent from the API.
Args:
parent_sk (str): sk of the parent to fetch reactions for.
Returns:
dict: The list of reactions.
"""
reaction_list = API.make_request("GET", "content", f"listreactionsbysk/{parent_sk}", authenticate=True)
return reaction_list

View file

@ -0,0 +1,12 @@
def reacttosomething(API, payload: dict) -> dict:
"""
Creates a new reaction in the API.
Args:
payload (dict): The data for the new reaction.
Returns:
dict: The reaction data.
"""
reaction_data = API.make_request("POST", "content", "reacttosomething", data=payload, authenticate=True)
return reaction_data

View file

@ -1,2 +1,4 @@
from .get_parent_pksk_from_path import get_parent_pksk_from_path
from .get_post_pksk import get_post_pksk
from .get_post_pksk import get_post_pksk
from .make_comment_sk import make_comment_sk
from .make_post_sk import make_post_sk

View file

@ -1,9 +1,7 @@
from .make_post_sk import make_post_sk
def get_post_pksk(parent_pksk, post_url):
post_slug = post_url.strip('/post/')
return {
"pk": parent_pksk,
"sk": f"post#{post_slug}"
"sk": make_post_sk(post_url)
}

View file

@ -0,0 +1,10 @@
def make_comment_sk(comment_url):
"""
Generates the sk for a comment given the comment url.
Args:
comment_url (str): The url of the comment.
Returns:
str: The sk for the comment.
"""
comment_slug = comment_url.strip('/comment/')
return f"comment#{comment_slug}"

View file

@ -0,0 +1,11 @@
def make_post_sk(post_url):
"""
Generates the sk for a post given the post url.
Args:
post_url (str): The url of the post.
Returns:
str: The sk for the post.
"""
post_slug = post_url.strip('/post/')
return f"post#{post_slug}"

View file

@ -0,0 +1,47 @@
from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk
def react(reaction_type: str, parent_path: str, item_path: str):
"""
React to something. ie a post or a comment.
This is one endpoint for creating, updating and deleting reactions.
The logic is handled in the backend.
Args:
Returns:
dict: A dictionary containing the job name and payload for creating the post
that will be processed by the API client wrapper function.
"""
'''
If it's a post we want to create a pk/sk like:
'{entity}#{parent_slug}' / 'post#{post_slug}'
If it's a comment we want to create a pk/sk like:
'post#{post_slug}' / 'comment#{comment_slug}'
'''
if item_path.startswith('/post'):
# It's a reaction on a post
top_level_parent_pk = get_parent_pksk_from_path(parent_path)
item_pksk = get_post_pksk(top_level_parent_pk, item_path)
else:
# It's a reaction on a comment
item_pksk = {
'pk': make_post_sk(parent_path),
'sk': make_comment_sk(item_path)
}
return {
"job_function": "reaction.react",
"payload": {
"reaction": reaction_type,
"parent": {
"pk": item_pksk.get('pk', None),
"sk": item_pksk.get('sk', None),
}
}
}