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())?;
}
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())?;

View file

@ -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);
}
}

View file

@ -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<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 => {
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 {

View file

@ -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,