simple-web-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit abfa460d29ccec0f7a752585084ecb54d4cb8586
parent b147b8dbc7d2e0a13d00492e29580ee1281ba30c
Author: Silas Brack <silasbrack@gmail.com>
Date:   Sat, 25 Apr 2026 18:58:40 +0200

fix: resolve FTS5 content table and vote auth issues

- Add U102 migration to fix FTS5 table referencing old news_item table
  (was renamed to story but FTS still pointed to news_item)
- Extract user_id from JWT token for vote creation
- TrailBase v0.23.0 requires explicit user_id in vote records

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Diffstat:
Msrc/trailbase.rs | 27+++++++++++++++++++--------
Atraildepot/migrations/main/U102__fix_fts_content.sql | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/src/trailbase.rs b/src/trailbase.rs @@ -400,14 +400,25 @@ pub async fn calculate_comment_path( // ============================================================================ /// Create a vote -#[derive(Debug, Serialize)] -struct CreateVote { - target_type: i64, - target_id: i64, -} - pub async fn vote(client: &Client, target_type: i64, target_id: i64) -> Result<(), ClientError> { - let vote_data = CreateVote { + // Check if client has tokens + let has_tokens = client.tokens().is_some(); + eprintln!("Client has tokens: {}", has_tokens); + + let tokens = client.tokens().ok_or_else(|| ClientError::Auth("Not authenticated".to_string()))?; + let user_id = extract_user_id_from_token(&tokens.auth_token) + .ok_or_else(|| ClientError::Auth("Invalid token".to_string()))?; + + // Include user_id in vote + #[derive(Debug, serde::Serialize)] + struct CreateVoteWithUser { + user_id: String, + target_type: i64, + target_id: i64, + } + + let vote_data = CreateVoteWithUser { + user_id, target_type, target_id, }; @@ -420,7 +431,7 @@ pub async fn vote(client: &Client, target_type: i64, target_id: i64) -> Result<( Ok(()) } Err(e) => { - eprintln!("Failed to create vote: {:?}", e); + eprintln!("Failed to create vote: {:#?}", e); Err(e.into()) } } diff --git a/traildepot/migrations/main/U102__fix_fts_content.sql b/traildepot/migrations/main/U102__fix_fts_content.sql @@ -0,0 +1,41 @@ +-- Fix FTS5 content table reference +-- The news_item_fts table was created with content='news_item' but we renamed to 'story' + +-- Drop the old FTS table and related tables +DROP TABLE IF EXISTS news_item_fts; +DROP TABLE IF EXISTS news_item_fts_data; +DROP TABLE IF EXISTS news_item_fts_idx; +DROP TABLE IF EXISTS news_item_fts_docsize; +DROP TABLE IF EXISTS news_item_fts_config; + +-- Drop the triggers that reference the old FTS table +DROP TRIGGER IF EXISTS story_ai; +DROP TRIGGER IF EXISTS story_au; +DROP TRIGGER IF EXISTS story_ad; + +-- Create new FTS table with correct content reference +CREATE VIRTUAL TABLE story_fts USING fts5( + title, + text, + content='story', + content_rowid='id' +); + +-- Populate the FTS table with existing data +INSERT INTO story_fts(rowid, title, text) +SELECT id, title, text FROM story; + +-- Create triggers for the new FTS table +CREATE TRIGGER story_ai AFTER INSERT ON story BEGIN + INSERT INTO story_fts(rowid, title, text) + VALUES (new.id, new.title, new.text); +END; + +CREATE TRIGGER story_au AFTER UPDATE ON story BEGIN + UPDATE story_fts SET title=new.title, text=new.text + WHERE rowid=old.id; +END; + +CREATE TRIGGER story_ad AFTER DELETE ON story BEGIN + DELETE FROM story_fts WHERE rowid=old.id; +END;