An open source, non-profit web search engine
Improve voting API
Daoud Clarke4e5436aac5c3
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.pyindex e0f06bf..fd945ff 100644--- a/mwmbl/platform/api.py+++ b/mwmbl/platform/api.py@@ -154,7 +154,15 @@ def update_submission_status(request, submission_id: int, update_submission: Updreturn {"status": "ok", "message": "Submission updated."}[email protected]("/search-results/vote", auth=JWTAuth())+ "/search-results/vote",+ auth=JWTAuth(),+ summary="Vote on a search result",+ description="Cast an upvote or downvote on a search result for a specific query. "+ "If the user has already voted on this result for this query, the vote will be updated. "+ "Each user can only have one vote per URL per query.",+ tags=["Search Result Voting"]+)def vote_on_search_result(request, vote_request: VoteRequest):check_email_verified(request)@@ -174,7 +182,16 @@ def vote_on_search_result(request, vote_request: VoteRequest):return {"status": "ok", "message": f"Vote {action} successfully."}[email protected]("/search-results/votes", response=VoteResponse, auth=JWTAuth())+ "/search-results/votes",+ response=VoteResponse,+ auth=JWTAuth(),+ summary="Get vote statistics for search results",+ description="Retrieve vote counts (upvotes and downvotes) for one or more URLs in the context of a specific search query. "+ "Also returns the current user's vote on each result if they have voted. "+ "URLs should be provided as a comma-separated list.",+ tags=["Search Result Voting"]+)def get_vote_counts(request, query: str, urls: str):check_email_verified(request)@@ -207,7 +224,14 @@ def get_vote_counts(request, query: str, urls: str):return VoteResponse(votes=vote_data)[email protected]("/search-results/vote", auth=JWTAuth())+ "/search-results/vote",+ auth=JWTAuth(),+ summary="Remove a vote from a search result",+ description="Remove the current user's vote (upvote or downvote) from a specific search result for a given query. "+ "If the user has not voted on this result for this query, a 404 error will be returned.",+ tags=["Search Result Voting"]+)def remove_vote(request, vote_request: VoteRemoveRequest):check_email_verified(request)@@ -223,7 +247,15 @@ def remove_vote(request, vote_request: VoteRemoveRequest):raise InvalidRequest("No vote found to remove.", status=404)[email protected]("/search-results/my-votes", response=list[UserVoteHistory], auth=JWTAuth())+ "/search-results/my-votes",+ response=list[UserVoteHistory],+ auth=JWTAuth(),+ summary="Get user's voting history",+ description="Retrieve the current user's complete voting history, showing all votes they have cast on search results. "+ "Results are ordered by timestamp (most recent first) and paginated.",+ tags=["Search Result Voting"]+)@paginatedef get_user_vote_history(request) -> list[SearchResultVote]:check_email_verified(request)diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.pyindex 93da556..180cbab 100644--- a/mwmbl/platform/schemas.py+++ b/mwmbl/platform/schemas.py@@ -1,6 +1,6 @@from datetime import datetimefrom typing import Optional, Literal-from ninja import Schema, ModelSchema+from ninja import Schema, ModelSchema, Fieldfrom mwmbl.models import DomainSubmission@@ -30,28 +30,89 @@ class UpdateDomainSubmission(ModelSchema):class VoteRequest(Schema):- url: str- query: str- vote_type: Literal["upvote", "downvote"]+ """Request schema for voting on search results."""++ url: str = Field(+ description="The URL of the search result being voted on",+ example="https://example.com/article"+ )+ query: str = Field(+ description="The search query that returned this result",+ example="python tutorial"+ )+ vote_type: Literal["upvote", "downvote"] = Field(+ description="Type of vote - either 'upvote' for positive feedback or 'downvote' for negative feedback",+ example="upvote"+ )class VoteRemoveRequest(Schema):- url: str- query: str+ """Request schema for removing a vote on a search result."""++ url: str = Field(+ description="The URL of the search result to remove the vote from",+ example="https://example.com/article"+ )+ query: str = Field(+ description="The search query that returned this result",+ example="python tutorial"+ )class VoteStats(Schema):- upvotes: int- downvotes: int- user_vote: Optional[Literal["upvote", "downvote"]] = None+ """Statistics for votes on a specific search result."""++ upvotes: int = Field(+ description="Total number of upvotes for this search result",+ example=15+ )+ downvotes: int = Field(+ description="Total number of downvotes for this search result",+ example=3+ )+ user_vote: Optional[Literal["upvote", "downvote"]] = Field(+ default=None,+ description="The current user's vote on this result, if any",+ example="upvote"+ )class VoteResponse(Schema):- votes: dict[str, VoteStats]+ """Response schema containing vote statistics for multiple URLs."""++ votes: dict[str, VoteStats] = Field(+ description="Dictionary mapping URLs to their vote statistics",+ example={+ "https://example.com/article": {+ "upvotes": 15,+ "downvotes": 3,+ "user_vote": "upvote"+ },+ "https://another-site.com/page": {+ "upvotes": 8,+ "downvotes": 1,+ "user_vote": None+ }+ }+ )class UserVoteHistory(Schema):- url: str- query: str- vote_type: Literal["upvote", "downvote"]- timestamp: datetime+ """Schema representing a user's voting history entry."""++ url: str = Field(+ description="The URL of the search result that was voted on",+ example="https://example.com/article"+ )+ query: str = Field(+ description="The search query that returned this result",+ example="python tutorial"+ )+ vote_type: Literal["upvote", "downvote"] = Field(+ description="The type of vote cast by the user",+ example="upvote"+ )+ timestamp: datetime = Field(+ description="When the vote was cast",+ example="2024-01-15T10:30:00Z"+ )