Store list of members in channel and fix topic display

This commit is contained in:
Sijmen 2023-11-23 01:41:26 +01:00
parent 9fc8620176
commit 4238210403
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 39 additions and 11 deletions

View file

@ -92,10 +92,11 @@ impl Cri {
let command = tokens.next().unwrap(); let command = tokens.next().unwrap();
match command { match command {
"/join" => self.handle_join_command(&mut tokens), "/join" | "/j" => self.handle_join_command(&mut tokens),
"/part" => self.handle_part_command(&mut tokens), "/part" | "/p" => self.handle_part_command(&mut tokens),
"/query" => self.handle_query_command(tokens), "/query" | "/q" => self.handle_query_command(tokens),
_ => todo!(), "/list" => self.handle_list_command(),
_ => (),
} }
} }
@ -155,6 +156,13 @@ impl Cri {
self.message_log self.message_log
.set_active(Some(tokens.next().unwrap().to_string())); .set_active(Some(tokens.next().unwrap().to_string()));
} }
fn handle_list_command(&self) {
self.input_tx
.borrow()
.send(IrcCommand::LIST(None, None).into())
.unwrap()
}
} }
impl Application for Cri { impl Application for Cri {
@ -266,10 +274,6 @@ impl Application for Cri {
); );
} }
IrcCommand::TOPIC(channel, topic) => {
self.message_log.on_topic(channel, topic.clone());
}
IrcCommand::PRIVMSG(msgtarget, content) IrcCommand::PRIVMSG(msgtarget, content)
| IrcCommand::NOTICE(msgtarget, content) => { | IrcCommand::NOTICE(msgtarget, content) => {
let channel = message.response_target().unwrap_or(msgtarget).to_string(); let channel = message.response_target().unwrap_or(msgtarget).to_string();
@ -282,9 +286,21 @@ impl Application for Cri {
&timestamp, &timestamp,
); );
} }
IrcCommand::Response(Response::RPL_WELCOME, args) => { IrcCommand::Response(Response::RPL_WELCOME, args) => {
self.nickname = args[0].clone() self.nickname = args[0].clone()
} }
IrcCommand::Response(Response::RPL_NAMREPLY, args) => {
let channel = &args[2];
let names: Vec<_> = args[3].split_ascii_whitespace().collect();
self.message_log.on_names_reply(channel, names);
}
IrcCommand::Response(Response::RPL_TOPIC, args) => {
let channel = &args[1];
let topic = &args[2];
self.message_log.on_topic(channel, topic);
}
IrcCommand::BATCH(tag, subcommand, params) => { IrcCommand::BATCH(tag, subcommand, params) => {
self.message_log.on_batch( self.message_log.on_batch(
&self.nickname, &self.nickname,
@ -294,6 +310,7 @@ impl Application for Cri {
&timestamp, &timestamp,
); );
} }
_ => self.message_log.on_other(&message.to_string()), _ => self.message_log.on_other(&message.to_string()),
} }
} }

View file

@ -14,6 +14,7 @@ use std::collections::{HashMap, HashSet};
pub struct Channel { pub struct Channel {
pub messages: Vec<IrcMessage>, pub messages: Vec<IrcMessage>,
pub message_ids: HashSet<String>, pub message_ids: HashSet<String>,
pub names: Vec<String>,
pub topic: Option<String>, pub topic: Option<String>,
pub unread_messages: i32, pub unread_messages: i32,
@ -324,7 +325,11 @@ impl<'a> MessageLog {
.clone() .clone()
.unwrap_or("Server messages".to_string()) .unwrap_or("Server messages".to_string())
), ),
text(channel.topic.clone().unwrap_or_default()) text(format!(
"{} members - {}",
channel.names.len(),
channel.topic.as_deref().unwrap_or_default()
))
]); ]);
let messages = channel let messages = channel
@ -456,7 +461,13 @@ impl<'a> MessageLog {
.width(Length::Fill) .width(Length::Fill)
} }
pub fn on_topic(&mut self, channel: &str, topic: Option<String>) { pub fn on_topic(&mut self, channel: &str, topic: &str) {
self.get_mut(Some(channel.to_string())).topic = topic; self.get_mut(Some(channel.to_string())).topic = Some(topic.to_owned());
}
pub fn on_names_reply(&mut self, channel: &str, names: Vec<&str>) {
self.get_mut(Some(channel.to_string()))
.names
.extend(names.iter().map(|&n| str::to_string(n)));
} }
} }