Skip to content

Commit d8cf271

Browse files
committed
add: notifications for received files
Using a Rust-equivalent of our JS 'file extension descriptions' for neat and beautifully rendered notifications!
1 parent 3b666d6 commit d8cf271

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

src-tauri/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,25 @@ async fn handle_event(event: Event, is_new: bool) -> bool {
15911591
size = reported_size;
15921592
}
15931593

1594+
// Send an OS notification for incoming files
1595+
if !is_mine && is_new {
1596+
// Find the name of the sender, if we have it
1597+
let display_name = match STATE.lock().await.get_profile(&contact) {
1598+
Some(profile) => {
1599+
// We have a profile, just check for a name
1600+
match profile.name.is_empty() {
1601+
true => String::from("New Message"),
1602+
false => profile.name.clone(),
1603+
}
1604+
}
1605+
// No profile
1606+
None => String::from("New Message"),
1607+
};
1608+
1609+
// Create a "description" of the attachment file
1610+
show_notification(display_name, "Sent a ".to_string() + &get_file_type_description(extension));
1611+
}
1612+
15941613
// Create an attachment
15951614
let mut attachments = Vec::new();
15961615
let attachment = Attachment {

src-tauri/src/util.rs

+60-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
use once_cell::sync::Lazy;
2+
use std::collections::HashMap;
3+
14
/// Extract all HTTPS URLs from a string
25
pub fn extract_https_urls(text: &str) -> Vec<String> {
36
let mut urls = Vec::new();
47
let mut start_idx = 0;
5-
8+
69
while let Some(https_idx) = text[start_idx..].find("https://") {
710
let abs_start = start_idx + https_idx;
811
let url_text = &text[abs_start..];
9-
12+
1013
// Find the end of the URL (first whitespace or common URL-ending chars)
11-
let mut end_idx = url_text.find(|c: char| {
12-
c.is_whitespace() || c == '"' || c == '<' || c == '>' || c == ')'
13-
|| c == ']' || c == '}' || c == '|'
14-
}).unwrap_or(url_text.len());
15-
14+
let mut end_idx = url_text
15+
.find(|c: char| {
16+
c.is_whitespace()
17+
|| c == '"'
18+
|| c == '<'
19+
|| c == '>'
20+
|| c == ')'
21+
|| c == ']'
22+
|| c == '}'
23+
|| c == '|'
24+
})
25+
.unwrap_or(url_text.len());
26+
1627
// Trim trailing punctuation
1728
while end_idx > 0 {
1829
let last_char = url_text[..end_idx].chars().last().unwrap();
@@ -22,13 +33,51 @@ pub fn extract_https_urls(text: &str) -> Vec<String> {
2233
break;
2334
}
2435
}
25-
36+
2637
if end_idx > "https://".len() {
2738
urls.push(url_text[..end_idx].to_string());
2839
}
29-
40+
3041
start_idx = abs_start + 1;
3142
}
32-
43+
3344
urls
34-
}
45+
}
46+
47+
/// Creates a description of a file type based on its extension.
48+
pub fn get_file_type_description(extension: &str) -> String {
49+
// Define file types with descriptions
50+
static FILE_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
51+
let mut map = HashMap::new();
52+
53+
// Images
54+
map.insert("png", "Picture");
55+
map.insert("jpg", "Picture");
56+
map.insert("jpeg", "Picture");
57+
map.insert("gif", "GIF Animation");
58+
map.insert("webp", "Picture");
59+
60+
// Audio
61+
map.insert("wav", "Voice Message");
62+
map.insert("mp3", "Audio Clip");
63+
64+
// Videos
65+
map.insert("mp4", "Video");
66+
map.insert("webm", "Video");
67+
map.insert("mov", "Video");
68+
map.insert("avi", "Video");
69+
map.insert("mkv", "Video");
70+
71+
map
72+
});
73+
74+
// Normalize the extension to lowercase
75+
let normalized_ext = extension.to_lowercase();
76+
77+
// Return the file type description if found, otherwise return default value
78+
FILE_TYPES
79+
.get(normalized_ext.as_str())
80+
.copied()
81+
.unwrap_or("File")
82+
.to_string()
83+
}

0 commit comments

Comments
 (0)