Consolidate active-scan notifications and scrub private data

Active scanners (ARP, SNMP) now accumulate every change across a whole
scan and emit one notification per type via events::notify: a single
device produces the usual single-device notification (carrying its MAC),
while two or more produce one consolidated summary with an empty
mac_address. Device events are still recorded per device.

Notification bodies no longer include MAC or IP addresses; the title
MAC fallback is masked to the last two octets. Summaries list up to
three devices then "…and N more devices".

Split sighting handling so record_sighting persists + records the event
and returns Vec<DeviceChange>; passive listeners (mDNS, SSDP, DHCP) use
record_and_notify since they see one device per event.

Also fixes NotificationType::from_str never mapping "DeviceChanged",
which made those notifications round-trip from the DB as Other.

Frontend: the card already hides the device link when mac_address is
null; added widget tests for the present/absent link cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-06 11:31:13 -04:00
co-authored by Claude Opus 4.8
parent 6eaaff457d
commit 42310a36e9
10 changed files with 468 additions and 104 deletions
+19 -12
View File
@@ -1,21 +1,23 @@
use log::{debug, error};
use crate::db;
use crate::events;
use crate::events::{self, DeviceChange};
use crate::model::device_events::DeviceEventScanner;
use crate::model::devices::Device;
/// Persist a device sighting and emit the matching event/notification, feeding every scanner
/// (ARP, mDNS, SSDP, DHCP, SNMP) through one code path.
/// Persist a device sighting and record its device event, then return any notification-worthy
/// changes it produced (without sending). This feeds every scanner (ARP, mDNS, SSDP, DHCP, SNMP)
/// through one code path. Active scanners accumulate the changes across a whole scan and pass them
/// to `events::notify` once, consolidating per type; passive listeners use `record_and_notify`.
///
/// When the device is already known, the sighting is reconciled with the stored record:
/// - a previously stored hostname is kept rather than overwritten by this sighting's name;
/// - a known IP address is kept when this sighting carries none (an empty string), so a DHCP
/// DISCOVER (which has no assigned IP) never clobbers a good address.
///
/// Errors are logged and swallowed: a scan/listen loop must never stop because a single sighting
/// failed to persist or a notification could not be delivered.
pub fn record_sighting(mut device: Device, scanner: DeviceEventScanner) {
/// Errors are logged and swallowed (an empty list is returned): a scan/listen loop must never stop
/// because a single sighting failed to persist.
pub fn record_sighting(mut device: Device, scanner: DeviceEventScanner) -> Vec<DeviceChange> {
match db::devices::read(device.mac_address.clone()) {
Some(recorded) => {
debug!("Sighting of known device {}; updating", device.mac_address);
@@ -33,23 +35,28 @@ pub fn record_sighting(mut device: Device, scanner: DeviceEventScanner) {
device.name.clone(),
) {
error!("Failed to update device {}: {err}", device.mac_address);
return;
return Vec::new();
}
// Ignoring errors: do not stop the loop if notification delivery fails.
events::trigger_existing_device(recorded, device, scanner).ok();
events::classify_existing_device(recorded, device, scanner)
}
None => {
debug!("New device {} discovered; inserting", device.mac_address);
if let Err(err) = db::devices::insert(device.clone()) {
error!("Failed to insert device {}: {err}", device.mac_address);
return;
return Vec::new();
}
// Ignoring errors: do not stop the loop if notification delivery fails.
events::trigger_new_device(device, scanner).ok();
vec![events::classify_new_device(device, scanner)]
}
}
}
/// Record a single sighting and immediately send any resulting notification. Used by the passive
/// listeners (mDNS, SSDP, DHCP), which process one device per event and so have nothing to
/// consolidate across a scan.
pub fn record_and_notify(device: Device, scanner: DeviceEventScanner) {
events::notify(record_sighting(device, scanner));
}
#[cfg(test)]
mod tests {
use super::*;