feat: move item to the top if copied again

This commit is contained in:
PandaDEV 2024-12-18 14:15:34 +10:00
parent 4ab938a3de
commit 460888a1e7
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C

View file

@ -59,33 +59,42 @@ pub async fn add_history_item(
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 last_content: Option<String> = sqlx::query_scalar( let existing = sqlx::query("SELECT id FROM history WHERE content = ? AND content_type = ?")
"SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1", .bind(&content)
) .bind(&content_type)
.bind(content_type.clone()) .fetch_optional(&*pool)
.fetch_one(&*pool) .await
.await .map_err(|e| e.to_string())?;
.unwrap_or(None);
if last_content.as_deref() == Some(&content) { match existing {
return Ok(()); Some(_) => {
sqlx::query(
"UPDATE history SET timestamp = strftime('%Y-%m-%dT%H:%M:%f+00:00', 'now') WHERE content = ? AND content_type = ?"
)
.bind(&content)
.bind(&content_type)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
}
None => {
sqlx::query(
"INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp, language) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
)
.bind(id)
.bind(source)
.bind(source_icon)
.bind(content_type)
.bind(content)
.bind(favicon)
.bind(timestamp)
.bind(language)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
}
} }
sqlx::query(
"INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp, language) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
)
.bind(id)
.bind(source)
.bind(source_icon)
.bind(content_type)
.bind(content)
.bind(favicon)
.bind(timestamp)
.bind(language)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }