From f39954d5bc38dda740fe6e1049f1ff89487884ca Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sat, 13 Jul 2024 15:38:22 +0200 Subject: [PATCH] starting to work on unread/notify --- jiji.project | 2 +- src/bot.rs | 12 ++++++------ src/discord_structure.rs | 37 +++++++++++++++++++++++++++++++++---- src/main.rs | 17 ++++++++++++++--- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/jiji.project b/jiji.project index a1f3c69..0e2d144 100644 --- a/jiji.project +++ b/jiji.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"get incoming messages","description":"read","id":2},{"name":"ability to write messages","description":"// Hello there","id":5},{"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":"in progress","content":[{"name":"fetch previous messages","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":"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":"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":"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 diff --git a/src/bot.rs b/src/bot.rs index a80dc85..2c6923f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -45,11 +45,11 @@ impl EventHandler for Handler { let guild_id = if let Some(id) = msg.guild_id { id.to_string() } else { - let private_channel = discord_structure::Channel::new(author_name.clone(), msg.channel_id.to_string(), "dm".to_string()); + let private_channel = discord_structure::Channel::create(author_name.clone(), msg.channel_id.to_string(), "dm".to_string()); sender.send(postman::Packet::Channel(private_channel)).expect("failed to send packet"); "dm".to_string() }; - let discord_message = discord_structure::Message::new(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822()); + let discord_message = discord_structure::Message::create(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822()).new(); sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet"); } else { println!("bot : failed to retrieve sender"); @@ -177,7 +177,7 @@ async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), Str if channel.kind != ChannelType::Text { continue } - let discord_channel = discord_structure::Channel::new(channel.name, format!("{}",channel_id), guild_id_str.to_string()); + let discord_channel = discord_structure::Channel::create(channel.name, format!("{}",channel_id), guild_id_str.to_string()); sender.send(postman::Packet::Channel(discord_channel)).map_err(|e| e.to_string())?; } sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?; @@ -209,7 +209,7 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S for message in &messages { let author_name = message.author.name.clone(); - let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_rfc2822()); + let discord_message = discord_structure::Message::create(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_rfc2822()); sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?; } @@ -229,13 +229,13 @@ async fn get_guilds(context: &Context) { let guilds = context.cache.guilds().await; if let Some(sender) = context.data.read().await.get::() { - let personal_messages = discord_structure::Guild::new("dm".to_string(), "dm".to_string()); + let personal_messages = discord_structure::Guild::create("dm".to_string(), "dm".to_string()); sender.send(postman::Packet::Guild(personal_messages)).expect("Failed to send packet"); for guild_id in guilds { if let Some(guild) = context.cache.guild(guild_id).await { println!("bot : found guild '{}'", guild.name.clone()); - let discord_guild = discord_structure::Guild::new(guild.name.clone(), guild_id.to_string()); + let discord_guild = discord_structure::Guild::create(guild.name.clone(), guild_id.to_string()); sender.send(postman::Packet::Guild(discord_guild)).expect("Failed to send packet"); } else { diff --git a/src/discord_structure.rs b/src/discord_structure.rs index 6224222..fb9a72d 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -8,7 +8,7 @@ pub struct Guild { } impl Guild { - pub fn new(name: String, id: String) -> Self { + pub fn create(name: String, id: String) -> Self { Self { name, id, @@ -38,19 +38,27 @@ pub struct Channel { pub id: String, pub guild_id: String, pub messages: Vec, + pub notify: bool, + pub unread: bool, } impl Channel { - pub fn new(name: String, id: String, guild_id: String) -> Self { + pub fn create(name: String, id: String, guild_id: String) -> Self { Self { name, id: id.clone(), guild_id : guild_id.clone(), - messages: vec![Message::new("0".into(), id, guild_id, "+".into(), "".into(), "".into())], + messages: vec![Message::create("0".into(), id, guild_id, "+".into(), "".into(), "".into())], + notify: false, + unread: false, } } pub fn insert(&mut self, message: Message) { + if message.new != "" { + self.unread = true; + } + match self.get_index_from_timestamp(&message.timestamp) { Ok(index) => { self.messages.insert(index, message); @@ -88,6 +96,20 @@ impl Channel { } self.messages.remove(0); } + + pub fn display(&self) -> String { + let notify = if self.notify { + " !" + } else { + "" + }; + let unread = if self.unread { + "~ " + } else { + "" + }; + format!("{}{}{}", unread, self.name, notify) + } } #[derive(PartialEq, Clone)] @@ -98,10 +120,11 @@ pub struct Message { pub author_name: String, pub content: String, pub timestamp: String, + pub new: String, } impl Message { - pub fn new(id: String, channel_id: String, guild_id: String, author_name: String, content: String, timestamp: String) -> Self { + pub fn create(id: String, channel_id: String, guild_id: String, author_name: String, content: String, timestamp: String) -> Self { Self { id, channel_id, @@ -109,6 +132,12 @@ impl Message { author_name, content, timestamp, + new: "".to_string(), } } + pub fn new(&mut self) -> Self { + let mut updated = self.clone(); + updated.new = "yes".to_string(); + updated + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a4fa8ee..49f7606 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ struct Jiji { time_watch: f32, pending_bot_requests: usize, current_message: String, + channels_to_notify: Vec, } impl Jiji { @@ -63,6 +64,7 @@ impl Jiji { time_watch: 0.0, pending_bot_requests: 0, current_message: "".into(), + channels_to_notify: vec![], } } } @@ -115,7 +117,9 @@ impl eframe::App for Jiji { if unkown_channel { println!("gui : unkown channel"); - self.guilds[guild_index].channels.push(discord_structure::Channel::new(message.channel_id.clone(), message.channel_id, message.guild_id)); + 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); @@ -218,7 +222,7 @@ impl Jiji { if let Some(selected_guild_index) = &self.selected_guild { if self.guilds[*selected_guild_index].channels.len() != 0 { let selected_channel_text = if let Some(selected_channel_index) = &self.selected_channel { - self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone() + self.guilds[*selected_guild_index].channels[*selected_channel_index].display() } else { "None".to_string() }; @@ -232,7 +236,7 @@ impl Jiji { 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].name.clone())).clicked() { + if ui.add(egui::SelectableLabel::new(self.selected_channel == Some(i), self.guilds[*selected_guild_index].channels[i].display())).clicked() { 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())); @@ -243,6 +247,9 @@ impl Jiji { } }); + if let Some(selected_channel_index) = &self.selected_channel { + ui.checkbox(&mut self.guilds[*selected_guild_index].channels[*selected_channel_index].notify, "notify"); + } } } }); @@ -292,10 +299,14 @@ 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(); + if self.guilds[*selected_guild_index].channels[*selected_channel_index].messages.len() < 2 { return } + for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages { if message.author_name == "+" { if ui.button("+").clicked() {