better ending of channels

This commit is contained in:
WanderingPenwing 2024-07-11 16:43:16 +02:00
parent 84cd96b2f6
commit be807fd54a
4 changed files with 50 additions and 21 deletions

View file

@ -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())?; sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?;
} }
if messages.len() == 25 { 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::ChannelEnd(guild_id_str.clone(), channel_id_str.clone())).map_err(|e| e.to_string())?;
sender.send(postman::Packet::Message(discord_fetch_message)).map_err(|e| e.to_string())?;
} }
sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?; sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;

View file

@ -27,14 +27,22 @@ impl Channel {
pub fn new(name: String, id: String, guild_id: String) -> Self { pub fn new(name: String, id: String, guild_id: String) -> Self {
Self { Self {
name, name,
id, id: id.clone(),
guild_id, guild_id : guild_id.clone(),
messages: vec![], messages: vec![Message::new("0".into(), id, guild_id, "+".into(), "".into(), "".into())],
} }
} }
pub fn greetings(&mut self) { pub fn insert(&mut self, message: Message) {
self.messages.push(Message::new("0".into(), self.id.clone(), self.guild_id.clone(), "-".into(), "start of the conversation".into(), "".into())); 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);
} }
} }

View file

@ -103,18 +103,42 @@ impl eframe::App for Jiji {
} }
if let Some(guild_index) = guild { if let Some(guild_index) = guild {
let mut unkown_channel = true;
for i in 0..self.guilds[guild_index].channels.len() { for i in 0..self.guilds[guild_index].channels.len() {
if self.guilds[guild_index].channels[i].id != message.channel_id { if self.guilds[guild_index].channels[i].id != message.channel_id {
continue continue
} }
if self.guilds[guild_index].channels[i].messages[0].author_name == "+" { self.guilds[guild_index].channels[i].insert(message.clone());
self.guilds[guild_index].channels[i].messages[0] = message.clone(); unkown_channel = false;
continue }
}
self.guilds[guild_index].channels[i].messages.insert(0, message.clone()); 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<usize> = 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 => { postman::Packet::FinishedRequest => {
self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0); 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() { 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].name.clone())).clicked() {
self.selected_channel = Some(i); 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())); 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.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_guild_index) = &self.selected_guild {
if let Some(selected_channel_index) = &self.selected_channel { if let Some(selected_channel_index) = &self.selected_channel {
let mut last_author = "".to_string(); let mut last_author = "".to_string();
for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages { if self.guilds[*selected_guild_index].channels[*selected_channel_index].messages.len() < 2 {
if message.author_name == "-" { return
continue }
} for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages {
if message.author_name == "+" { if message.author_name == "+" {
if ui.button("+").clicked() { if ui.button("+").clicked() {
if let Some(selected_guild_index) = &self.selected_guild { if let Some(selected_guild_index) = &self.selected_guild {

View file

@ -7,6 +7,7 @@ pub enum Packet {
Guild(discord_structure::Guild), Guild(discord_structure::Guild),
Channel(discord_structure::Channel), Channel(discord_structure::Channel),
Message(discord_structure::Message), Message(discord_structure::Message),
ChannelEnd(String, String),
FetchChannels(String), FetchChannels(String),
FetchMessages(String, String, String), FetchMessages(String, String, String),
FinishedRequest, FinishedRequest,