Vote casting

This commit is contained in:
simonwt 2026-04-03 00:50:38 +01:00
parent 8704ad0e70
commit 80786c4965
12 changed files with 152 additions and 42 deletions

View file

@ -0,0 +1 @@
from .votecast import votecast

View file

@ -0,0 +1,12 @@
def votecast(API, payload: dict) -> dict:
"""
Casts a vote in the API.
Args:
payload (dict): The data for the vote.
Returns:
dict: The vote data.
"""
vote_data = API.make_request("POST", "content", "votecast", data=payload, authenticate=True)
return vote_data

View file

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

View file

@ -0,0 +1,34 @@
from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk
def get_child_spksk_from_paths(parent_path: str, item_path: str):
'''
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}'
'''
slug = item_path.split('/')[-1]
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)
entity = 'post'
else:
# It's a reaction on a comment
item_pksk = {
'pk': make_post_sk(parent_path),
'sk': make_comment_sk(item_path)
}
entity = 'comment'
return {
"pk": item_pksk.get('pk', None),
"sk": item_pksk.get('sk', None),
"entity": entity,
"slug": slug
}

View file

@ -1,5 +1,5 @@
from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk
from trustcafeapiwrapper.utils import get_child_spksk_from_paths
def react(reaction_type: str, parent_path: str, item_path: str):
"""
React to something. ie a post or a comment.
@ -12,40 +12,11 @@ def react(reaction_type: str, parent_path: str, item_path: str):
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}'
'''
slug = item_path.split('/')[-1]
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)
entity = 'post'
else:
# It's a reaction on a comment
item_pksk = {
'pk': make_post_sk(parent_path),
'sk': make_comment_sk(item_path)
}
entity = 'comment'
parent = get_child_spksk_from_paths(parent_path, item_path)
return {
"job_function": "reaction.reacttosomething",
"payload": {
"reaction": reaction_type,
"parent": {
"pk": item_pksk.get('pk', None),
"sk": item_pksk.get('sk', None),
"entity": entity,
"slug": slug
}
"parent": parent
}
}

View file

@ -0,0 +1 @@
from .votecast import votecast

View file

@ -0,0 +1,21 @@
from trustcafeapiwrapper.utils import get_child_spksk_from_paths
def votecast(vote: str, parent_path: str, item_path: str):
"""
Creates a new vote in the API.
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.
"""
parent = get_child_spksk_from_paths(parent_path, item_path)
return {
"job_function": "vote.votecast",
"payload": {
"vote": vote,
"parent": parent
}
}