commit 5d5606a77a6c9bd5f091aeee31578f287ca1fc7b
parent 9caba3ce794ed4ac9b6d219ca7b7509da7eda9ff
Author: Silas Brack <silasbrack@gmail.com>
Date: Sat, 25 Apr 2026 14:33:49 +0200
fix: use TrailBase autofill for vote user_id
- Remove manual user_id from CreateVote struct
- Use autofill_user_id_column config for automatic population
- Simplifies vote creation flow
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat:
3 files changed, 4 insertions(+), 14 deletions(-)
diff --git a/src/handlers/vote.rs b/src/handlers/vote.rs
@@ -20,10 +20,6 @@ pub async fn vote_story(
let tokens = get_tokens_from_cookies(&jar)
.ok_or_else(|| AppError::Unauthorized("Login required to vote".to_string()))?;
- // Extract user ID from token
- let user_id = trailbase::extract_user_id_from_token(&tokens)
- .ok_or_else(|| AppError::Unauthorized("Invalid auth token".to_string()))?;
-
let client = trailbase::client_with_tokens(&state.trailbase_url, &tokens)?;
// Check if already voted
@@ -37,7 +33,7 @@ pub async fn vote_story(
trailbase::unvote(&client, TARGET_TYPE_STORY, story_id).await?;
} else {
// Vote
- trailbase::vote(&client, &user_id, TARGET_TYPE_STORY, story_id).await?;
+ trailbase::vote(&client, TARGET_TYPE_STORY, story_id).await?;
}
// Get updated story score
@@ -64,10 +60,6 @@ pub async fn vote_comment(
let tokens = get_tokens_from_cookies(&jar)
.ok_or_else(|| AppError::Unauthorized("Login required to vote".to_string()))?;
- // Extract user ID from token
- let user_id = trailbase::extract_user_id_from_token(&tokens)
- .ok_or_else(|| AppError::Unauthorized("Invalid auth token".to_string()))?;
-
let client = trailbase::client_with_tokens(&state.trailbase_url, &tokens)?;
// Check if already voted
@@ -81,7 +73,7 @@ pub async fn vote_comment(
trailbase::unvote(&client, TARGET_TYPE_COMMENT, comment_id).await?;
} else {
// Vote
- trailbase::vote(&client, &user_id, TARGET_TYPE_COMMENT, comment_id).await?;
+ trailbase::vote(&client, TARGET_TYPE_COMMENT, comment_id).await?;
}
// Get updated comment score
diff --git a/src/trailbase.rs b/src/trailbase.rs
@@ -291,14 +291,12 @@ pub async fn calculate_comment_path(
/// Create a vote
#[derive(Debug, Serialize)]
struct CreateVote {
- user_id: String,
target_type: i64,
target_id: i64,
}
-pub async fn vote(client: &Client, user_id: &str, target_type: i64, target_id: i64) -> Result<(), ClientError> {
+pub async fn vote(client: &Client, target_type: i64, target_id: i64) -> Result<(), ClientError> {
let vote_data = CreateVote {
- user_id: user_id.to_string(),
target_type,
target_id,
};
diff --git a/traildepot/config.textproto b/traildepot/config.textproto
@@ -40,7 +40,7 @@ record_apis: [{
table_name: "vote"
acl_world: []
acl_authenticated: [READ, CREATE, DELETE]
- create_access_rule: "_REQ_.user_id = _USER_.id"
+ autofill_user_id_column: "user_id"
delete_access_rule: "_ROW_.user_id = _USER_.id"
}, {
name: "user_profile"