From 59a94d337f0d8a8b020074bf0ae57a10c7343223 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sun, 14 Jul 2024 09:49:55 +0200 Subject: [PATCH] guild unread and fix dm notification state persistence --- jiji.project | 2 +- src/app.rs | 41 ++++++++++++++++++++++------------------ src/discord_structure.rs | 24 ++++++++++++++++++++++- src/main.rs | 11 +++++++++-- src/ui.rs | 31 +++++++++++++++++++++++++++--- 5 files changed, 84 insertions(+), 25 deletions(-) diff --git a/jiji.project b/jiji.project index 08c2bc6..a9983f9 100644 --- a/jiji.project +++ b/jiji.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1},{"name":"better ui error display","description":"handle the error packet for better display","id":1},{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"timestamps","description":"// Hello there","id":1},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3}]},{"name":"in progress","content":[{"name":"notifications !!!","description":"// Hello there","id":4}]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1}]},{"name":"bugs","content":[]},{"name":"+","content":[]}]} \ No newline at end of file +{"categories":[{"name":"to do","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1},{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3}]},{"name":"bugs","content":[]},{"name":"v1.0","content":[{"name":"better ui error display","description":"handle the error packet for better display","id":1},{"name":"notifications !!!","description":"// Hello there","id":4},{"name":"timestamps","description":"// Hello there","id":1},{"name":"bug : does not save notification state for dm","description":"// Hello there","id":1},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1}]},{"name":"+","content":[]}]} \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 2d33bec..0d1c765 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,11 +12,11 @@ impl Jiji { while let Ok(packet) = self.receiver.try_recv() { match packet { postman::Packet::Guild(guild) => { - println!("gui : guild received : '{}'", guild.name); + println!("app : guild received : '{}'", guild.name); self.guilds.push(guild); } postman::Packet::Channel(channel) => { - println!("gui : channel received : '{}'", channel.name); + println!("app : channel received : '{}'", channel.name); for i in 0..self.guilds.len() { if self.guilds[i].id != channel.guild_id { continue @@ -35,7 +35,7 @@ impl Jiji { } } postman::Packet::Message(message) => { - println!("gui : message received : '{}'", message.content); + println!("app : message received : '{}'", message.content); let mut guild: Option = None; @@ -48,30 +48,35 @@ impl Jiji { if let Some(guild_index) = guild { - let mut unkown_channel = true; + let mut channel: Option = None; + for i in 0..self.guilds[guild_index].channels.len() { if self.guilds[guild_index].channels[i].id != message.channel_id { continue } - self.guilds[guild_index].channels[i].insert(message.clone()); - unkown_channel = false; - println!("gui : message put in : '{}'", self.guilds[guild_index].channels[i].name); + channel = Some(i); } - if unkown_channel { - println!("gui : unkown channel"); + let channel_index = if let Some(index) = channel { + index + } else { + println!("app: unknown channel"); self.guilds[guild_index].channels.push(discord_structure::Channel::create(message.channel_id.clone(), message.channel_id.clone(), message.guild_id.clone())); - let last = self.guilds[guild_index].channels.len() - 1; - self.guilds[guild_index].channels[last].insert(message.clone()); - } - } else { - println!("gui : message guild issue : '{}'", message.guild_id); + self.guilds[guild_index].channels.len() - 1 + }; - println!("gui : guilds {:?}", self.guilds.clone().into_iter().map(|guild| { guild.id.clone()}).collect::>()); + if self.guilds[guild_index].channels[channel_index].insert(message.clone()) { + self.guilds[guild_index].unread = true; + } + + } else { + println!("app : message guild issue : '{}'", message.guild_id); + + println!("app : guilds {:?}", self.guilds.clone().into_iter().map(|guild| { guild.id.clone()}).collect::>()); } } postman::Packet::ChannelEnd(guild_id, channel_id) => { - println!("gui : end of channel : '{}'", channel_id); + println!("app : end of channel : '{}'", channel_id); let mut guild: Option = None; @@ -93,13 +98,13 @@ impl Jiji { } postman::Packet::Error(reason) => { - println!("gui : error received {}", reason); + println!("app : error received {}", reason); } postman::Packet::FinishedRequest => { self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0); } _ => { - println!("gui : unhandled packet"); + println!("app : unhandled packet"); } } } diff --git a/src/discord_structure.rs b/src/discord_structure.rs index fb9a72d..7ae699a 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -5,6 +5,7 @@ pub struct Guild { pub name: String, pub id: String, pub channels: Vec, + pub unread: bool, } impl Guild { @@ -13,6 +14,7 @@ impl Guild { name, id, channels: vec![], + unread: false, } } @@ -30,6 +32,25 @@ impl Guild { self.channels.insert(0, channel.clone()) } } + + pub fn check_unread(&mut self) { + self.unread = false; + + for channel in &self.channels { + if channel.unread { + self.unread = true; + } + } + } + + pub fn display(&self) -> String { + let unread = if self.unread { + "~ " + } else { + "" + }; + format!("{}{}", unread, self.name) + } } #[derive(PartialEq, Clone)] @@ -54,7 +75,7 @@ impl Channel { } } - pub fn insert(&mut self, message: Message) { + pub fn insert(&mut self, message: Message) -> bool { if message.new != "" { self.unread = true; } @@ -69,6 +90,7 @@ impl Channel { } } + self.unread } pub fn get_index_from_timestamp(&self, message_timestamp: &str) -> Result { diff --git a/src/main.rs b/src/main.rs index fc11f77..26a9992 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,12 +60,19 @@ struct Jiji { impl Jiji { fn new(sender: mpsc::Sender, receiver: mpsc::Receiver) -> Self { - let app_state = state::load_state(&save_path()); + let mut app_state = state::load_state(&save_path()); let mut dms = discord_structure::Guild::create("dm".to_string(), "dm".to_string()); for (id, name) in &app_state.dm_channels { - dms.add_channel(discord_structure::Channel::create(name.to_string(), id.to_string(), dms.id.clone())); + let mut channel = discord_structure::Channel::create(name.to_string(), id.to_string(), dms.id.clone()); + + if let Some(index) = app_state.channels_to_notify.iter().position(|x| x == &channel.id) { + channel.notify = true; + app_state.channels_to_notify.remove(index); + } + + dms.add_channel(channel); } Self { diff --git a/src/ui.rs b/src/ui.rs index c0cbf86..3d2c6e1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -11,7 +11,7 @@ impl Jiji { ui.horizontal(|ui| { ui.label("Where do you want to look ? "); let selected_guild_text = if let Some(selected_guild_index) = &self.selected_guild { - self.guilds[*selected_guild_index].name.clone() + self.guilds[*selected_guild_index].display() } else { "None".to_string() }; @@ -22,11 +22,27 @@ impl Jiji { ui.style_mut().wrap = Some(false); ui.set_min_width(60.0); if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() { + + if let Some(selected_guild_index) = &self.selected_guild { + if let Some(selected_channel_index) = &self.selected_channel { + self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false; + self.guilds[*selected_guild_index].check_unread(); + } + } + self.selected_guild = None; self.selected_channel = None; } for i in 0..self.guilds.len() { - if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() { + if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].display())).clicked() { + + if let Some(selected_guild_index) = &self.selected_guild { + if let Some(selected_channel_index) = &self.selected_channel { + self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false; + self.guilds[*selected_guild_index].check_unread(); + } + } + self.selected_guild = Some(i); self.selected_channel = None; @@ -56,11 +72,21 @@ impl Jiji { ui.style_mut().wrap = Some(false); ui.set_min_width(60.0); if ui.add(egui::SelectableLabel::new(self.selected_channel == None, "None")).clicked() { + if let Some(selected_channel_index) = &self.selected_channel { + self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false; + } self.selected_channel = None; } for i in 0..self.guilds[*selected_guild_index].channels.len() { if ui.add(egui::SelectableLabel::new(self.selected_channel == Some(i), self.guilds[*selected_guild_index].channels[i].display())).clicked() { + + if let Some(selected_channel_index) = &self.selected_channel { + self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false; + self.guilds[*selected_guild_index].check_unread(); + } + self.selected_channel = Some(i); + if self.guilds[*selected_guild_index].channels[i].messages.len() == 1 { let _ = self.sender.send(postman::Packet::FetchMessages(self.guilds[*selected_guild_index].id.clone(), self.guilds[*selected_guild_index].channels[i].id.clone(), "".into())); @@ -122,7 +148,6 @@ impl Jiji { .show(ui, |ui| { if let Some(selected_guild_index) = &self.selected_guild { if let Some(selected_channel_index) = &self.selected_channel { - self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false; let mut last_author = "".to_string();