Add curations

This commit is contained in:
Daoud Clarke 2022-12-11 18:48:25 +00:00
parent f89e1d6043
commit 31c27daca4

View file

@ -5,7 +5,6 @@ from urllib.parse import urljoin
import requests
from fastapi import APIRouter, Response
from pydantic import BaseModel
from starlette.responses import JSONResponse
from mwmbl.tokenizer import tokenize
@ -31,6 +30,25 @@ class BeginCurate(BaseModel):
original_urls: list[str]
class Curation(BaseModel):
auth: str
curation_id: int
class CurateMove(Curation):
url_old_index: int
url_new_index: int
class CurateDelete(Curation):
url_delete_index: int
class CurateAdd(Curation):
url_insert_index: int
url: str
def create_router() -> APIRouter:
router = APIRouter(prefix="/user", tags=["user"])
@ -59,9 +77,7 @@ def create_router() -> APIRouter:
@router.post("/curation/begin")
def user_begin_curate(begin_curate: BeginCurate):
# TODO: check if there is already a post for this user and query combination.
# If there is, just post the new original urls.
body = json.dumps({'original_urls': begin_curate.original_urls}, indent=2)
body = json.dumps({"original_urls": begin_curate.original_urls}, indent=2)
tokens = tokenize(begin_curate.query)
url = RESULT_URL + "+".join(tokens)
create_post = {
@ -77,6 +93,31 @@ def create_router() -> APIRouter:
request = requests.post(urljoin(LEMMY_URL, "api/v3/post"), json=create_post)
return Response(content=request.content, status_code=request.status_code, media_type="text/json")
@router.post("/curation/move")
def user_move_result(curate_move: CurateMove):
return _create_comment("curate_move", curate_move)
@router.post("/curation/delete")
def user_delete_result(curate_delete: CurateDelete):
return _create_comment("curate_delete", curate_delete)
@router.post("/curation/add")
def user_add_result(curate_add: CurateAdd):
return _create_comment("curate_add", curate_add)
def _create_comment(curation_type: str, curation: Curation):
content = json.dumps({curation_type: curation.dict()}, indent=2)
create_comment = {
"auth": curation.auth,
"content": json.dumps(content, indent=2),
"form_id": None,
"language_id": None,
"parent_id": None,
"post_id": curation.curation_id,
}
request = requests.post(urljoin(LEMMY_URL, "api/v3/comment"), json=create_comment)
return Response(content=request.content, status_code=request.status_code, media_type="text/json")
return router