guild unread and fix dm notification state persistence

This commit is contained in:
WanderingPenwing 2024-07-14 09:49:55 +02:00
parent 32924b608b
commit 59a94d337f
5 changed files with 84 additions and 25 deletions

View file

@ -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":[]}]} {"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":[]}]}

View file

@ -12,11 +12,11 @@ impl Jiji {
while let Ok(packet) = self.receiver.try_recv() { while let Ok(packet) = self.receiver.try_recv() {
match packet { match packet {
postman::Packet::Guild(guild) => { postman::Packet::Guild(guild) => {
println!("gui : guild received : '{}'", guild.name); println!("app : guild received : '{}'", guild.name);
self.guilds.push(guild); self.guilds.push(guild);
} }
postman::Packet::Channel(channel) => { postman::Packet::Channel(channel) => {
println!("gui : channel received : '{}'", channel.name); println!("app : channel received : '{}'", channel.name);
for i in 0..self.guilds.len() { for i in 0..self.guilds.len() {
if self.guilds[i].id != channel.guild_id { if self.guilds[i].id != channel.guild_id {
continue continue
@ -35,7 +35,7 @@ impl Jiji {
} }
} }
postman::Packet::Message(message) => { postman::Packet::Message(message) => {
println!("gui : message received : '{}'", message.content); println!("app : message received : '{}'", message.content);
let mut guild: Option<usize> = None; let mut guild: Option<usize> = None;
@ -48,30 +48,35 @@ impl Jiji {
if let Some(guild_index) = guild { if let Some(guild_index) = guild {
let mut unkown_channel = true; let mut channel: Option<usize> = None;
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
} }
self.guilds[guild_index].channels[i].insert(message.clone()); channel = Some(i);
unkown_channel = false;
println!("gui : message put in : '{}'", self.guilds[guild_index].channels[i].name);
} }
if unkown_channel { let channel_index = if let Some(index) = channel {
println!("gui : unkown 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())); 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.len() - 1
self.guilds[guild_index].channels[last].insert(message.clone()); };
}
} else {
println!("gui : message guild issue : '{}'", message.guild_id);
println!("gui : guilds {:?}", self.guilds.clone().into_iter().map(|guild| { guild.id.clone()}).collect::<Vec<String>>()); 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::<Vec<String>>());
} }
} }
postman::Packet::ChannelEnd(guild_id, channel_id) => { 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<usize> = None; let mut guild: Option<usize> = None;
@ -93,13 +98,13 @@ impl Jiji {
} }
postman::Packet::Error(reason) => { postman::Packet::Error(reason) => {
println!("gui : error received {}", reason); println!("app : error received {}", reason);
} }
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);
} }
_ => { _ => {
println!("gui : unhandled packet"); println!("app : unhandled packet");
} }
} }
} }

View file

@ -5,6 +5,7 @@ pub struct Guild {
pub name: String, pub name: String,
pub id: String, pub id: String,
pub channels: Vec<Channel>, pub channels: Vec<Channel>,
pub unread: bool,
} }
impl Guild { impl Guild {
@ -13,6 +14,7 @@ impl Guild {
name, name,
id, id,
channels: vec![], channels: vec![],
unread: false,
} }
} }
@ -30,6 +32,25 @@ impl Guild {
self.channels.insert(0, channel.clone()) 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)] #[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 != "" { if message.new != "" {
self.unread = true; self.unread = true;
} }
@ -69,6 +90,7 @@ impl Channel {
} }
} }
self.unread
} }
pub fn get_index_from_timestamp(&self, message_timestamp: &str) -> Result<usize, ParseError> { pub fn get_index_from_timestamp(&self, message_timestamp: &str) -> Result<usize, ParseError> {

View file

@ -60,12 +60,19 @@ struct Jiji {
impl Jiji { impl Jiji {
fn new(sender: mpsc::Sender<postman::Packet>, receiver: mpsc::Receiver<postman::Packet>) -> Self { fn new(sender: mpsc::Sender<postman::Packet>, receiver: mpsc::Receiver<postman::Packet>) -> 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()); let mut dms = discord_structure::Guild::create("dm".to_string(), "dm".to_string());
for (id, name) in &app_state.dm_channels { 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 { Self {

View file

@ -11,7 +11,7 @@ impl Jiji {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Where do you want to look ? "); ui.label("Where do you want to look ? ");
let selected_guild_text = if let Some(selected_guild_index) = &self.selected_guild { 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 { } else {
"None".to_string() "None".to_string()
}; };
@ -22,11 +22,27 @@ impl Jiji {
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.set_min_width(60.0); ui.set_min_width(60.0);
if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() { 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_guild = None;
self.selected_channel = None; self.selected_channel = None;
} }
for i in 0..self.guilds.len() { 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_guild = Some(i);
self.selected_channel = None; self.selected_channel = None;
@ -56,11 +72,21 @@ impl Jiji {
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.set_min_width(60.0); ui.set_min_width(60.0);
if ui.add(egui::SelectableLabel::new(self.selected_channel == None, "None")).clicked() { 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; self.selected_channel = None;
} }
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].display())).clicked() { 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); self.selected_channel = Some(i);
if self.guilds[*selected_guild_index].channels[i].messages.len() == 1 { 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()));
@ -122,7 +148,6 @@ impl Jiji {
.show(ui, |ui| { .show(ui, |ui| {
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 {
self.guilds[*selected_guild_index].channels[*selected_channel_index].unread = false;
let mut last_author = "".to_string(); let mut last_author = "".to_string();