relfa 0.4.0

A gentle digital gravedigger to lovingly archive your old files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use anyhow::{Context, Result};
use chrono::{DateTime, Duration, Utc};
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

use crate::config::{Config, NotificationType};
use crate::state::NotificationState;

#[derive(Debug, Clone)]
pub struct StaleItem {
    pub path: PathBuf,
    pub name: String,
    pub last_modified: DateTime<Utc>,
    pub is_directory: bool,
    pub age_days: i64,
    pub notification_count: u32,
}

impl StaleItem {
    pub fn display(&self) -> String {
        let item_type = if self.is_directory { "folder" } else { "file" };
        format!(
            "📄 \"{}\" ({}) - last touched {} days ago ({})",
            self.name,
            item_type,
            self.age_days,
            self.last_modified.format("%Y-%m-%d")
        )
    }
}

pub struct Scanner {
    config: Config,
}

impl Scanner {
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    pub fn scan_inbox(&self) -> Result<Vec<StaleItem>> {
        self.scan_inbox_with_state(false)
    }

    pub fn scan_inbox_with_notification_tracking(&self) -> Result<Vec<StaleItem>> {
        self.scan_inbox_with_state(true)
    }

    fn scan_inbox_with_state(&self, track_notifications: bool) -> Result<Vec<StaleItem>> {
        if !self.config.inbox.exists() {
            return Ok(vec![]);
        }

        let mut state = if track_notifications {
            Some(NotificationState::load().unwrap_or_default())
        } else {
            None
        };

        let mut stale_items = Vec::new();
        let mut currently_stale_files = std::collections::HashSet::new();
        let cutoff_date = Utc::now() - Duration::days(self.config.age_threshold_days as i64);

        for entry in fs::read_dir(&self.config.inbox).context("Failed to read inbox directory")? {
            let entry = entry.context("Failed to read directory entry")?;
            let path = entry.path();

            if let Some(last_modified) = self.get_last_modified_time(&path)? {
                if last_modified < cutoff_date {
                    let age_days = (Utc::now() - last_modified).num_days();
                    let name = path
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string();

                    // Track that this file is currently stale
                    currently_stale_files.insert(name.clone());

                    let notification_count = if let Some(ref mut state) = state {
                        // Get current count and increment it
                        let current_count = state.get_notification_count(&name);
                        state.increment_notification_count(&name);
                        current_count + 1 // Return what the count will be after this scan
                    } else {
                        // When not tracking, just load state to get current count
                        NotificationState::load()
                            .unwrap_or_default()
                            .get_notification_count(&name)
                    };

                    stale_items.push(StaleItem {
                        path: path.clone(),
                        name,
                        last_modified,
                        is_directory: path.is_dir(),
                        age_days,
                        notification_count,
                    });
                }
            }
        }

        // Save the updated state if we were tracking
        if let Some(mut state) = state {
            // Clean up entries for files that are no longer stale
            // This includes files that were deleted, modified, or are now younger than threshold
            state.retain_only_files(&currently_stale_files);
            state.save()?;
        }

        Ok(stale_items)
    }

    pub fn scan_auto_archive_eligible(&self) -> Result<Vec<StaleItem>> {
        if !self.config.inbox.exists() {
            return Ok(vec![]);
        }

        let state = NotificationState::load().unwrap_or_default();
        let mut auto_archive_items = Vec::new();
        let cutoff_date =
            Utc::now() - Duration::days(self.config.auto_archive_threshold_days as i64);

        for entry in fs::read_dir(&self.config.inbox).context("Failed to read inbox directory")? {
            let entry = entry.context("Failed to read directory entry")?;
            let path = entry.path();

            if let Some(last_modified) = self.get_last_modified_time(&path)? {
                if last_modified < cutoff_date {
                    let age_days = (Utc::now() - last_modified).num_days();
                    let name = path
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string();

                    let notification_count = state.get_notification_count(&name);

                    // Only eligible if it has been notified enough times
                    if notification_count >= self.config.auto_archive_min_scans {
                        auto_archive_items.push(StaleItem {
                            path: path.clone(),
                            name,
                            last_modified,
                            is_directory: path.is_dir(),
                            age_days,
                            notification_count,
                        });
                    }
                }
            }
        }

        Ok(auto_archive_items)
    }

    // New method to get items that will be eligible after more scans
    pub fn scan_pending_auto_archive(&self) -> Result<Vec<StaleItem>> {
        if !self.config.inbox.exists() {
            return Ok(vec![]);
        }

        let state = NotificationState::load().unwrap_or_default();
        let mut pending_items = Vec::new();
        let cutoff_date =
            Utc::now() - Duration::days(self.config.auto_archive_threshold_days as i64);

        for entry in fs::read_dir(&self.config.inbox).context("Failed to read inbox directory")? {
            let entry = entry.context("Failed to read directory entry")?;
            let path = entry.path();

            if let Some(last_modified) = self.get_last_modified_time(&path)? {
                if last_modified < cutoff_date {
                    let age_days = (Utc::now() - last_modified).num_days();
                    let name = path
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string();

                    let notification_count = state.get_notification_count(&name);

                    // Pending if old enough but not notified enough times yet
                    if notification_count < self.config.auto_archive_min_scans {
                        pending_items.push(StaleItem {
                            path: path.clone(),
                            name,
                            last_modified,
                            is_directory: path.is_dir(),
                            age_days,
                            notification_count,
                        });
                    }
                }
            }
        }

        Ok(pending_items)
    }

    fn get_last_modified_time(&self, path: &Path) -> Result<Option<DateTime<Utc>>> {
        if path.is_file() {
            let metadata = fs::metadata(path).context("Failed to get file metadata")?;
            let modified = metadata
                .modified()
                .context("Failed to get modification time")?;
            Ok(Some(DateTime::from(modified)))
        } else if path.is_dir() {
            // Start with the directory's own modification time
            let metadata = fs::metadata(path).context("Failed to get directory metadata")?;
            let dir_modified = metadata
                .modified()
                .context("Failed to get modification time")?;
            let mut latest_time = DateTime::from(dir_modified);

            // Then check all files and subdirectories recursively
            for entry in WalkDir::new(path)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file() || e.file_type().is_dir())
            {
                // Skip the root directory itself to avoid double-counting
                if entry.path() == path {
                    continue;
                }

                if let Ok(metadata) = entry.metadata() {
                    if let Ok(modified) = metadata.modified() {
                        let modified_dt = DateTime::from(modified);
                        if modified_dt > latest_time {
                            latest_time = modified_dt;
                        }
                    }
                }
            }

            Ok(Some(latest_time))
        } else {
            Ok(None)
        }
    }

    pub fn display_scan_results(&self, stale_items: &[StaleItem]) {
        // Check for auto-archive eligible and pending items
        let auto_archive_items = self.scan_auto_archive_eligible().unwrap_or_default();
        let pending_items = self.scan_pending_auto_archive().unwrap_or_default();

        if stale_items.is_empty() && auto_archive_items.is_empty() && pending_items.is_empty() {
            println!("✨ No dusty items found in your Inbox! All clean and tidy.");
            self.send_notification(
                "Relfa Scan Complete",
                "No dusty items found in your Inbox! All clean and tidy. ✨",
            );
            return;
        }

        // Display stale items (regular threshold)
        if !stale_items.is_empty() {
            let plural = if stale_items.len() == 1 {
                "item"
            } else {
                "items"
            };
            let message = format!(
                "☠️  {} {} in ~/Inbox {} gathering dust:",
                stale_items.len(),
                plural,
                if stale_items.len() == 1 { "is" } else { "are" }
            );

            println!("{message}");

            for item in stale_items {
                let scan_info = if item.notification_count > 0 {
                    format!(" [seen {} times]", item.notification_count)
                } else {
                    String::new()
                };
                println!("   {}{}", item.display(), scan_info);
            }
        }

        // Display pending auto-archive items
        if !pending_items.is_empty() {
            if !stale_items.is_empty() {
                println!();
            }

            println!(
                "{} {} old enough for auto-archiving but {} more scans:",
                pending_items.len(),
                if pending_items.len() == 1 {
                    "item"
                } else {
                    "items"
                },
                if pending_items.len() == 1 {
                    "needs"
                } else {
                    "need"
                }
            );

            for item in &pending_items {
                let scans_needed = self.config.auto_archive_min_scans - item.notification_count;
                println!(
                    "   {} [needs {} more {}]",
                    item.display(),
                    scans_needed,
                    if scans_needed == 1 { "scan" } else { "scans" }
                );
            }
        }

        // Display auto-archive eligible items
        if !auto_archive_items.is_empty() {
            let auto_plural = if auto_archive_items.len() == 1 {
                "item"
            } else {
                "items"
            };

            if !stale_items.is_empty() || !pending_items.is_empty() {
                println!();
            }

            println!(
                "🤖 {} {} {} eligible for auto-archiving NOW:",
                auto_archive_items.len(),
                auto_plural,
                if auto_archive_items.len() == 1 {
                    "is"
                } else {
                    "are"
                }
            );

            for item in &auto_archive_items {
                println!(
                    "   {} [notified {} times]",
                    item.display(),
                    item.notification_count
                );
            }

            println!(
                "   ⚠️  These will be automatically archived if you run 'relfa archive' without arguments!"
            );
        }

        println!("\n💡 Run 'relfa review' to interactively deal with these items");
        if !stale_items.is_empty() && !auto_archive_items.is_empty() {
            println!(
                "   or 'relfa archive' to auto-archive old files (or 'relfa archive --all' for all)."
            );
        } else if !stale_items.is_empty() {
            println!("   or 'relfa archive --all' to archive them all to the Graveyard.");
        } else if !auto_archive_items.is_empty() {
            println!("   or 'relfa archive' to auto-archive old files.");
        }

        // Send desktop notification
        let total_items = stale_items.len() + auto_archive_items.len();
        let notification_text = if !auto_archive_items.is_empty() {
            format!(
                "{} items need attention in Inbox. {} are eligible for auto-archiving!",
                total_items,
                auto_archive_items.len()
            )
        } else {
            format!(
                "{} {} in Inbox {} gathering dust. Consider reviewing them!",
                stale_items.len(),
                if stale_items.len() == 1 {
                    "item"
                } else {
                    "items"
                },
                if stale_items.len() == 1 { "is" } else { "are" }
            )
        };
        self.send_notification("Digital Clutter Detected", &notification_text);
    }

    fn send_notification(&self, title: &str, body: &str) {
        if matches!(self.config.notification, NotificationType::Desktop) {
            #[cfg(not(target_os = "windows"))]
            {
                if let Err(e) = notify_rust::Notification::new()
                    .summary(title)
                    .body(body)
                    .icon("folder")
                    .timeout(notify_rust::Timeout::Milliseconds(5000))
                    .show()
                {
                    eprintln!("Failed to send desktop notification: {e}");
                }
            }

            #[cfg(target_os = "windows")]
            {
                if let Err(e) = notify_rust::Notification::new()
                    .summary(title)
                    .body(body)
                    .timeout(notify_rust::Timeout::Milliseconds(5000))
                    .show()
                {
                    eprintln!("Failed to send desktop notification: {}", e);
                }
            }
        }
    }
}