Skip to main content

#
jellytau_lib/commands/download/
pinning.rs

    1//! Pinning commands - protect an item's cached metadata from cache clearing.
    2//!
    3//! TRACES: UR-044 | DR-056
    4
    5use std::sync::Arc;
    6use tauri::State;
    7
    8use crate::commands::DatabaseWrapper;
    9use crate::storage::db_service::{DatabaseService, Query, QueryParam};
    10
    11/// Pin an item's metadata (protects from cache clear)
    12#[tauri::command]
    13#[specta::specta]
    14pub async fn pin_item(db: State<'_, DatabaseWrapper>, item_id: String) -> Result<(), String> {
    15    let db_service = {
    16        let database = db.0.lock().map_err(|e| e.to_string())?;
    17        Arc::new(database.service())
    18    };
    19
    20    let query = Query::with_params(
    21        "UPDATE items SET is_pinned = 1 WHERE id = ?",
    22        vec![QueryParam::String(item_id)],
    23    );
    24
    25    db_service.execute(query).await.map_err(|e| e.to_string())?;
    26    Ok(())
    27}
    28
    29/// Unpin an item's metadata
    30#[tauri::command]
    31#[specta::specta]
    32pub async fn unpin_item(db: State<'_, DatabaseWrapper>, item_id: String) -> Result<(), String> {
    33    let db_service = {
    34        let database = db.0.lock().map_err(|e| e.to_string())?;
    35        Arc::new(database.service())
    36    };
    37
    38    let query = Query::with_params(
    39        "UPDATE items SET is_pinned = 0 WHERE id = ?",
    40        vec![QueryParam::String(item_id)],
    41    );
    42
    43    db_service.execute(query).await.map_err(|e| e.to_string())?;
    44    Ok(())
    45}
    46
    47/// Check if an item is pinned
    48#[tauri::command]
    49#[specta::specta]
    50pub async fn is_item_pinned(
    51    db: State<'_, DatabaseWrapper>,
    52    item_id: String,
    53) -> Result<bool, String> {
    54    let db_service = {
    55        let database = db.0.lock().map_err(|e| e.to_string())?;
    56        Arc::new(database.service())
    57    };
    58
    59    let query = Query::with_params(
    60        "SELECT COALESCE(is_pinned, 0) FROM items WHERE id = ?",
    61        vec![QueryParam::String(item_id)],
    62    );
    63
    64    let is_pinned: i32 = db_service
    65        .query_optional(query, |row| row.get(0))
    66        .await
    67        .map_err(|e| e.to_string())?
    68        .unwrap_or(0);
    69
    70    Ok(is_pinned == 1)
    71}