feat: cancel a download from the TUI, the CLI, and the daemon
Closes #54. A download can now be cancelled whether it is queued, in progress, or paused. Cancelled is a terminal status: the store refuses to overwrite it, the status channel relays nothing after it, the partial file is deleted, and the transfer thread releases its socket at its next one-second poll instead of the 30s read deadline. A queued download that is cancelled declines the peer's later offer with TransferResponse "Cancelled", and an inbound file connection for it is refused. A fresh download of the same file evicts the dead row so it is not shadowed, and a cancelled transfer is not re-queued after a restart. Surfaces: Client::cancel_download, download.cancel over RPC, `transfer cancel <user> <path>` on the CLI (which also stops an upload by that name), and x in the transfers pane, where d dismisses the row.
This commit is contained in:
@@ -6,7 +6,6 @@ use super::state::{PersistedDownload, PersistedMessage};
|
||||
use crate::models::{
|
||||
AppState, ChatMessage, MessageDirection, SearchEntry, SearchStatus,
|
||||
};
|
||||
use soulseek_rs::DownloadStatus;
|
||||
|
||||
/// How much private-message history survives a restart.
|
||||
/// a flat cap on the whole log rather than per conversation; make
|
||||
@@ -34,16 +33,7 @@ impl Snapshot {
|
||||
let downloads = state
|
||||
.downloads
|
||||
.iter()
|
||||
.map(|entry| PersistedDownload {
|
||||
username: entry.download.username.clone(),
|
||||
filename: entry.download.filename.clone(),
|
||||
size: entry.download.size,
|
||||
download_directory: entry.download.download_directory.clone(),
|
||||
completed: matches!(
|
||||
entry.download.status,
|
||||
DownloadStatus::Completed
|
||||
),
|
||||
})
|
||||
.filter_map(|entry| PersistedDownload::capture(&entry.download))
|
||||
.collect();
|
||||
|
||||
let mut queries: Vec<String> = Vec::new();
|
||||
@@ -121,6 +111,7 @@ pub fn restore_searches(state: &mut AppState, queries: &[String]) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::models::DownloadEntry;
|
||||
use soulseek_rs::DownloadStatus;
|
||||
use soulseek_rs::types::{Download, DownloadMetadata};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, mpsc};
|
||||
@@ -188,6 +179,28 @@ mod tests {
|
||||
assert_eq!(snapshot.downloads[0].download_directory, "/music");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cancelled_download_is_not_captured() {
|
||||
let mut state = AppState::new();
|
||||
state
|
||||
.downloads
|
||||
.push(download("gone.mp3", DownloadStatus::Cancelled));
|
||||
state
|
||||
.downloads
|
||||
.push(download("queued.mp3", DownloadStatus::Queued));
|
||||
|
||||
let snapshot = Snapshot::capture(&state);
|
||||
assert_eq!(
|
||||
snapshot
|
||||
.downloads
|
||||
.iter()
|
||||
.map(|d| d.filename.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
vec!["queued.mp3"],
|
||||
"a cancel must not come back as a retry after a restart"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capture_takes_queries_in_order_without_duplicates() {
|
||||
let mut state = AppState::new();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
use color_eyre::Result;
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use serde_json::Value;
|
||||
use soulseek_rs::{DownloadStatus, types::Download};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The shape every file is written in today. Bump it — and convert the older
|
||||
@@ -24,6 +25,22 @@ pub struct PersistedDownload {
|
||||
pub completed: bool,
|
||||
}
|
||||
|
||||
impl PersistedDownload {
|
||||
#[must_use]
|
||||
pub fn capture(download: &Download) -> Option<Self> {
|
||||
if matches!(download.status, DownloadStatus::Cancelled) {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
username: download.username.clone(),
|
||||
filename: download.filename.clone(),
|
||||
size: download.size,
|
||||
download_directory: download.download_directory.clone(),
|
||||
completed: matches!(download.status, DownloadStatus::Completed),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// One line of private-message history.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, serde::Deserialize)]
|
||||
pub struct PersistedMessage {
|
||||
|
||||
Reference in New Issue
Block a user