Acquired user IDs

Removed emotes unwrap
This commit is contained in:
Xithrius 2023-08-13 21:39:12 -07:00
parent 30f4fe45be
commit e103e18ced
No known key found for this signature in database
GPG Key ID: DF6738B80C155B71
5 changed files with 76 additions and 20 deletions

View File

@ -173,6 +173,15 @@ impl App {
self.components.chat.scroll_offset.jump_to(0);
}
pub fn purge_user_messages(&mut self, _user_id: &str) {
// let filtered_messages: Rc<RefCell<VecDeque<MessageData>>> = self.messages.borrow().iter().filter(|m| {
// m.user_id.clone().map_or(true, |user| user != user_id)
// }).collect();
// self.messages = filtered_messages;
todo!();
}
pub fn remove_message_with(&mut self, message_id: &str) {
let index = self
.messages

View File

@ -46,10 +46,9 @@ impl EmoteData {
}
}
#[allow(dead_code)]
pub enum TwitchToTerminalAction {
Message(MessageData),
ClearChat,
ClearChat(Option<String>),
DeleteMessage(String),
}
@ -57,6 +56,7 @@ pub enum TwitchToTerminalAction {
pub struct MessageData {
pub time_sent: DateTime<Local>,
pub author: String,
pub user_id: Option<String>,
pub system: bool,
pub payload: String,
pub emotes: Vec<EmoteData>,
@ -64,10 +64,17 @@ pub struct MessageData {
}
impl MessageData {
pub fn new(author: String, system: bool, payload: String, message_id: Option<String>) -> Self {
pub fn new(
author: String,
user_id: Option<String>,
system: bool,
payload: String,
message_id: Option<String>,
) -> Self {
Self {
time_sent: Local::now(),
author,
user_id,
system,
payload,
emotes: vec![],
@ -336,16 +343,33 @@ impl<'conf> DataBuilder<'conf> {
DataBuilder { datetime_format }
}
pub fn user(user: String, payload: String, id: Option<String>) -> TwitchToTerminalAction {
TwitchToTerminalAction::Message(MessageData::new(user, false, payload, id))
pub fn user(
user: String,
user_id: Option<String>,
payload: String,
message_id: Option<String>,
) -> TwitchToTerminalAction {
TwitchToTerminalAction::Message(MessageData::new(user, user_id, false, payload, message_id))
}
pub fn system(self, payload: String) -> TwitchToTerminalAction {
TwitchToTerminalAction::Message(MessageData::new("System".to_string(), true, payload, None))
TwitchToTerminalAction::Message(MessageData::new(
"System".to_string(),
None,
true,
payload,
None,
))
}
pub fn twitch(self, payload: String) -> TwitchToTerminalAction {
TwitchToTerminalAction::Message(MessageData::new("Twitch".to_string(), true, payload, None))
TwitchToTerminalAction::Message(MessageData::new(
"Twitch".to_string(),
None,
true,
payload,
None,
))
}
}
@ -356,8 +380,14 @@ mod tests {
#[test]
fn test_username_hash() {
assert_eq!(
MessageData::new("human".to_string(), false, "beep boop".to_string(), None)
.hash_username(&Palette::Pastel),
MessageData::new(
"human".to_string(),
None,
false,
"beep boop".to_string(),
None
)
.hash_username(&Palette::Pastel),
Rgb(159, 223, 221)
);
}

View File

@ -77,8 +77,12 @@ pub async fn ui_driver(
app.components.chat.scroll_offset.up();
}
}
TwitchToTerminalAction::ClearChat => {
app.clear_messages();
TwitchToTerminalAction::ClearChat(user_id) => {
if let Some(user) = user_id {
app.purge_user_messages(user.as_str());
} else {
app.clear_messages();
}
}
TwitchToTerminalAction::DeleteMessage(message_id) => {
app.remove_message_with(message_id.as_str());
@ -117,6 +121,7 @@ pub async fn ui_driver(
TwitchAction::Privmsg(message) => {
let message_data = DataBuilder::user(
config.twitch.username.to_string(),
None,
message.to_string(),
None,
);

View File

@ -175,12 +175,14 @@ async fn handle_message_command(
// An attempt to remove null bytes from the message.
let cleaned_message = msg.trim_matches(char::from(0));
let id = tags.get("id").map(|&s| s.to_string());
let message_id = tags.get("id").map(|&s| s.to_string());
let user_id = tags.get("user-id").map(|&s| s.to_string());
tx.send(DataBuilder::user(
name.to_string(),
user_id,
cleaned_message.to_string(),
id,
message_id,
))
.await
.unwrap();
@ -217,7 +219,11 @@ async fn handle_message_command(
}
// https://dev.twitch.tv/docs/irc/tags/#clearchat-tags
"CLEARCHAT" => {
tx.send(TwitchToTerminalAction::ClearChat).await.unwrap();
let user_id = tags.get("target-user-id").map(|&s| s.to_string());
tx.send(TwitchToTerminalAction::ClearChat(user_id))
.await
.unwrap();
tx.send(data_builder.twitch("Chat cleared by a moderator.".to_string()))
.await
.unwrap();
@ -269,9 +275,14 @@ pub async fn handle_roomstate(tx: &Sender<TwitchToTerminalAction>, tags: &HashMa
return;
}
let id = tags.get("target-msg-id").map(|&s| s.to_string());
let message_id = tags.get("target-msg-id").map(|&s| s.to_string());
tx.send(DataBuilder::user(String::from("Info"), room_state, id))
.await
.unwrap();
tx.send(DataBuilder::user(
String::from("Info"),
None,
room_state,
message_id,
))
.await
.unwrap();
}

View File

@ -212,8 +212,9 @@ impl ChatWidget {
impl Component for ChatWidget {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect, emotes: Option<&mut Emotes>) {
// TODO: Don't let this be a thing
let emotes = emotes.unwrap();
let mut default_emotes = Emotes::default();
let emotes = emotes.map_or(&mut default_emotes, |e| e);
let config = self.config.borrow();