From be807fd54ab82e41bd9dbda35d4e15a8dcf41ca7 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 11 Jul 2024 16:43:16 +0200 Subject: [PATCH] better ending of channels --- src/bot.rs | 5 ++--- src/discord_structure.rs | 18 ++++++++++----- src/main.rs | 47 +++++++++++++++++++++++++++++----------- src/postman.rs | 1 + 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 8754273..35a182d 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -167,9 +167,8 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?; } - if messages.len() == 25 { - let discord_fetch_message = discord_structure::Message::new("".to_string(), channel_id_str.clone(), guild_id_str.clone(), "+".to_string(), "".to_string(), "".to_string()); - sender.send(postman::Packet::Message(discord_fetch_message)).map_err(|e| e.to_string())?; + if messages.len() < 25 { + sender.send(postman::Packet::ChannelEnd(guild_id_str.clone(), channel_id_str.clone())).map_err(|e| e.to_string())?; } sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?; diff --git a/src/discord_structure.rs b/src/discord_structure.rs index 775f7b8..de2f4fc 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -27,14 +27,22 @@ impl Channel { pub fn new(name: String, id: String, guild_id: String) -> Self { Self { name, - id, - guild_id, - messages: vec![], + id: id.clone(), + guild_id : guild_id.clone(), + messages: vec![Message::new("0".into(), id, guild_id, "+".into(), "".into(), "".into())], } } - pub fn greetings(&mut self) { - self.messages.push(Message::new("0".into(), self.id.clone(), self.guild_id.clone(), "-".into(), "start of the conversation".into(), "".into())); + pub fn insert(&mut self, message: Message) { + self.messages.insert(1, message); + println!("discord_structure : need to compare timestamp"); + } + + pub fn end(&mut self) { + if self.messages[0].author_name != "+" { + return + } + self.messages.remove(0); } } diff --git a/src/main.rs b/src/main.rs index 9353005..c415c90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,18 +103,42 @@ impl eframe::App for Jiji { } if let Some(guild_index) = guild { + let mut unkown_channel = true; for i in 0..self.guilds[guild_index].channels.len() { if self.guilds[guild_index].channels[i].id != message.channel_id { continue } - if self.guilds[guild_index].channels[i].messages[0].author_name == "+" { - self.guilds[guild_index].channels[i].messages[0] = message.clone(); - continue - } - self.guilds[guild_index].channels[i].messages.insert(0, message.clone()); + self.guilds[guild_index].channels[i].insert(message.clone()); + unkown_channel = false; + } + + if unkown_channel { + println!("gui : unkown channel"); } } } + postman::Packet::ChannelEnd(guild_id, channel_id) => { + println!("gui : end of channel : '{}'", channel_id); + + let mut guild: Option = None; + + for i in 0..self.guilds.len() { + if self.guilds[i].id != guild_id { + continue + } + guild = Some(i); + } + + if let Some(guild_index) = guild { + for i in 0..self.guilds[guild_index].channels.len() { + if self.guilds[guild_index].channels[i].id != channel_id { + continue + } + self.guilds[guild_index].channels[i].end(); + } + } + + } postman::Packet::FinishedRequest => { self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0); } @@ -199,12 +223,10 @@ impl Jiji { 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() { self.selected_channel = Some(i); - if self.guilds[*selected_guild_index].channels[i].messages.len() == 0 { + 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())); self.pending_bot_requests += 1; - - self.guilds[*selected_guild_index].channels[i].greetings(); } } } @@ -257,11 +279,10 @@ impl Jiji { if let Some(selected_guild_index) = &self.selected_guild { if let Some(selected_channel_index) = &self.selected_channel { let mut last_author = "".to_string(); - for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages { - if message.author_name == "-" { - continue - } - + 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() { if let Some(selected_guild_index) = &self.selected_guild { diff --git a/src/postman.rs b/src/postman.rs index c6ae87a..aa41e35 100644 --- a/src/postman.rs +++ b/src/postman.rs @@ -7,6 +7,7 @@ pub enum Packet { Guild(discord_structure::Guild), Channel(discord_structure::Channel), Message(discord_structure::Message), + ChannelEnd(String, String), FetchChannels(String), FetchMessages(String, String, String), FinishedRequest,