refactor(history): clean up imports and formatting in history module

This commit is contained in:
pandadev 2025-02-14 19:22:05 +01:00
parent 68174334db
commit 7c0e83f86a
No known key found for this signature in database
GPG key ID: C39629DACB8E762F

View file

@ -1,7 +1,7 @@
use crate::utils::types::{ContentType, HistoryItem}; use crate::utils::types::{ContentType, HistoryItem};
use base64::{engine::general_purpose::STANDARD, Engine}; use base64::{engine::general_purpose::STANDARD, Engine};
use rand::{ rng, Rng };
use rand::distr::Alphanumeric; use rand::distr::Alphanumeric;
use rand::{rng, Rng};
use sqlx::{Row, SqlitePool}; use sqlx::{Row, SqlitePool};
use std::fs; use std::fs;
use tauri_plugin_aptabase::EventTracker; use tauri_plugin_aptabase::EventTracker;
@ -55,16 +55,16 @@ pub async fn get_history(pool: tauri::State<'_, SqlitePool>) -> Result<Vec<Histo
pub async fn add_history_item( pub async fn add_history_item(
app_handle: tauri::AppHandle, app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>, pool: tauri::State<'_, SqlitePool>,
item: HistoryItem item: HistoryItem,
) -> Result<(), String> { ) -> Result<(), String> {
let (id, source, source_icon, content_type, content, favicon, timestamp, language) = let (id, source, source_icon, content_type, content, favicon, timestamp, language) =
item.to_row(); item.to_row();
let existing = sqlx let existing = sqlx::query("SELECT id FROM history WHERE content = ? AND content_type = ?")
::query("SELECT id FROM history WHERE content = ? AND content_type = ?")
.bind(&content) .bind(&content)
.bind(&content_type) .bind(&content_type)
.fetch_optional(&*pool).await .fetch_optional(&*pool)
.await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
match existing { match existing {
@ -100,7 +100,7 @@ pub async fn add_history_item(
"history_item_added", "history_item_added",
Some(serde_json::json!({ Some(serde_json::json!({
"content_type": item.content_type.to_string() "content_type": item.content_type.to_string()
})) })),
); );
Ok(()) Ok(())
@ -109,7 +109,7 @@ pub async fn add_history_item(
#[tauri::command] #[tauri::command]
pub async fn search_history( pub async fn search_history(
pool: tauri::State<'_, SqlitePool>, pool: tauri::State<'_, SqlitePool>,
query: String query: String,
) -> Result<Vec<HistoryItem>, String> { ) -> Result<Vec<HistoryItem>, String> {
let query = format!("%{}%", query); let query = format!("%{}%", query);
let rows = sqlx let rows = sqlx
@ -141,7 +141,7 @@ pub async fn search_history(
pub async fn load_history_chunk( pub async fn load_history_chunk(
pool: tauri::State<'_, SqlitePool>, pool: tauri::State<'_, SqlitePool>,
offset: i64, offset: i64,
limit: i64 limit: i64,
) -> Result<Vec<HistoryItem>, String> { ) -> Result<Vec<HistoryItem>, String> {
let rows = sqlx let rows = sqlx
::query( ::query(
@ -173,12 +173,12 @@ pub async fn load_history_chunk(
pub async fn delete_history_item( pub async fn delete_history_item(
app_handle: tauri::AppHandle, app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>, pool: tauri::State<'_, SqlitePool>,
id: String id: String,
) -> Result<(), String> { ) -> Result<(), String> {
sqlx sqlx::query("DELETE FROM history WHERE id = ?")
::query("DELETE FROM history WHERE id = ?")
.bind(id) .bind(id)
.execute(&*pool).await .execute(&*pool)
.await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let _ = app_handle.track_event("history_item_deleted", None); let _ = app_handle.track_event("history_item_deleted", None);
@ -189,11 +189,11 @@ pub async fn delete_history_item(
#[tauri::command] #[tauri::command]
pub async fn clear_history( pub async fn clear_history(
app_handle: tauri::AppHandle, app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool> pool: tauri::State<'_, SqlitePool>,
) -> Result<(), String> { ) -> Result<(), String> {
sqlx sqlx::query("DELETE FROM history")
::query("DELETE FROM history") .execute(&*pool)
.execute(&*pool).await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let _ = app_handle.track_event("history_cleared", None); let _ = app_handle.track_event("history_cleared", None);