Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Core: fetch photos quietly, for a backup

Bee3dbaebc3a86c
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rs
index c47add4..3d00ad5 100644
--- a/core/nectarlink-core/src/node.rs
+++ b/core/nectarlink-core/src/node.rs
@@ -2926,6 +2926,19 @@ impl Node {
crate::photos::fetch_many(&self.shared, &session, ids).await
}
+ /// Like [`fetch_photos`](Self::fetch_photos), for a backup: the files are
+ /// saved as usual, but the transfer leaves nothing in the Timeline.
+ pub async fn fetch_photos_quietly(&self, peer: DeviceId, ids: Vec<String>) -> Result<String> {
+ let session = self.connected(&peer)?;
+ crate::photos::fetch_quietly(&self.shared, &session, ids).await
+ }
+
+ /// Keeps a transfer started by [`fetch_photos_quietly`](Self::fetch_photos_quietly)
+ /// quiet after a restart, when it resumes.
+ pub fn keep_transfer_quiet(&self, transfer_id: &str) {
+ crate::transfer::mark_quiet_transfer(&self.shared, transfer_id);
+ }
+
// ---- Remote input (docs/protocol/remote.md) ----
/// Checks whether a paired PC currently accepts remote input from this
diff --git a/core/nectarlink-core/src/photos.rs b/core/nectarlink-core/src/photos.rs
index d27c6fe..6ac45f9 100644
--- a/core/nectarlink-core/src/photos.rs
+++ b/core/nectarlink-core/src/photos.rs
@@ -124,7 +124,21 @@ pub(crate) async fn fetch(shared: &Shared, session: &Session, id: String) -> Res
}
/// Asks a phone for one or more photos or videos in a single transfer.
-pub(crate) async fn fetch_many(shared: &Shared, session: &Session, mut ids: Vec<String>) -> Result<String> {
+pub(crate) async fn fetch_many(shared: &Shared, session: &Session, ids: Vec<String>) -> Result<String> {
+ fetch_inner(shared, session, ids, false).await
+}
+
+/// Like [`fetch_many`], but the transfer leaves nothing in the Timeline.
+pub(crate) async fn fetch_quietly(shared: &Shared, session: &Session, ids: Vec<String>) -> Result<String> {
+ fetch_inner(shared, session, ids, true).await
+}
+
+async fn fetch_inner(
+ shared: &Shared,
+ session: &Session,
+ mut ids: Vec<String>,
+ quiet: bool,
+) -> Result<String> {
allowed(shared, &session.peer)?;
ids.retain(|id| is_valid_photo_id(id));
ids.truncate(photos::MAX_GET_ITEMS);
@@ -135,7 +149,11 @@ pub(crate) async fn fetch_many(shared: &Shared, session: &Session, mut ids: Vec<
let env = Envelope::new(types::PHOTOS_GET, &PhotoGet { id, ids })?;
let reply = session.request(env, crate::session::REQUEST_TIMEOUT).await?;
let PhotoSending { transfer } = reply.expect_body(types::PHOTOS_SENDING)?;
- crate::transfer::mark_photo_transfer(shared, &transfer);
+ if quiet {
+ crate::transfer::mark_quiet_transfer(shared, &transfer);
+ } else {
+ crate::transfer::mark_photo_transfer(shared, &transfer);
+ }
Ok(transfer)
}
diff --git a/core/nectarlink-core/src/transfer.rs b/core/nectarlink-core/src/transfer.rs
index 55ff582..3e10f49 100644
--- a/core/nectarlink-core/src/transfer.rs
+++ b/core/nectarlink-core/src/transfer.rs
@@ -365,6 +365,35 @@ pub(crate) fn mark_photo_transfer(shared: &Shared, transfer_id: &str) {
}
}
+static QUIET_TRANSFERS: std::sync::Mutex<Option<std::collections::HashSet<String>>> =
+ std::sync::Mutex::new(None);
+
+/// Marks an incoming transfer as quiet: it's saved like any other, but leaves
+/// nothing in the Timeline (a photo backup's downloads). An entry already
+/// recorded for it (the transfer finished before this) is removed.
+pub(crate) fn mark_quiet_transfer(shared: &Shared, transfer_id: &str) {
+ QUIET_TRANSFERS
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .get_or_insert_with(std::collections::HashSet::new)
+ .insert(transfer_id.to_owned());
+ if shared.store.delete_timeline_by_ref(transfer_id).unwrap_or(false) {
+ // It had finished already, so nothing will take the mark.
+ take_quiet_transfer(transfer_id);
+ shared.emit(NodeEvent::TimelineChanged);
+ }
+}
+
+/// Whether a transfer that just finished was quiet, forgetting it. A failed
+/// one stays marked: it resumes under the same ID.
+fn take_quiet_transfer(transfer_id: &str) -> bool {
+ QUIET_TRANSFERS
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .as_mut()
+ .is_some_and(|set| set.remove(transfer_id))
+}
+
fn take_photo_transfer(transfer_id: &str) -> bool {
PHOTO_TRANSFERS
.lock()
@@ -1666,19 +1695,21 @@ async fn receive(shared: Arc<Shared>, peer: DeviceId, mut send: SendStream, mut
};
let target =
saved.iter().map(|p| p.to_string_lossy().into_owned()).collect::<Vec<_>>().join("\n");
- let _ = shared.record_timeline(crate::timeline::NewTimelineEntry {
- kind,
- device_id: peer,
- device_name: shared.peer_name(&peer),
- incoming: true,
- timestamp: crate::now_unix(),
- title: transfer_timeline_title(&reporter.transfer.names),
- detail,
- target,
- size_bytes: reporter.transfer.total,
- duration_secs: 0,
- ref_id: Some(offer.id.clone()),
- });
+ if !take_quiet_transfer(&offer.id) {
+ let _ = shared.record_timeline(crate::timeline::NewTimelineEntry {
+ kind,
+ device_id: peer,
+ device_name: shared.peer_name(&peer),
+ incoming: true,
+ timestamp: crate::now_unix(),
+ title: transfer_timeline_title(&reporter.transfer.names),
+ detail,
+ target,
+ size_bytes: reporter.transfer.total,
+ duration_secs: 0,
+ ref_id: Some(offer.id.clone()),
+ });
+ }
TransferState::Done { saved }
}
Err(e) => {
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index bea996e..09a3a87 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -2488,6 +2488,19 @@ async fn gallery_albums_paging_thumbs_download_and_toggles() {
assert_eq!(std::fs::read(&files[0]).unwrap(), bytes4);
assert_eq!(std::fs::read(&files[1]).unwrap(), bytes2);
+ // A backup's download is saved the same way but leaves nothing in the Timeline.
+ let quiet =
+ with_timeout("fetch_photos_quietly", pc.node.fetch_photos_quietly(phone_id, vec!["media:4".into()]))
+ .await
+ .unwrap();
+ let quiet_files = wait_transfer(&mut pc, &quiet, "backup file", saved).await;
+ assert_eq!(std::fs::read(&quiet_files[0]).unwrap(), bytes4);
+ let timeline =
+ pc.node.timeline_page(&nectarlink_core::TimelineQuery { limit: 50, ..Default::default() }).unwrap();
+ let refs: Vec<_> = timeline.entries.iter().filter_map(|e| e.ref_id.as_deref()).collect();
+ assert!(refs.contains(&tid.as_str()), "the gallery download is in the Timeline");
+ assert!(!refs.contains(&quiet.as_str()), "the backup download isn't");
+
// `photos.changed` notification.
phone.node.photos_changed().await;
wait_for(&mut pc, "photos changed", |e| match e {