2024-06-05 03:21:15 +02:00
|
|
|
use core::fmt;
|
|
|
|
use std::fmt::Display;
|
2024-06-05 00:54:43 +02:00
|
|
|
|
|
|
|
// TODO im so angry tokio-tungstenite makes me import this!
|
2024-06-05 03:21:15 +02:00
|
|
|
use futures_util::{StreamExt, SinkExt};
|
|
|
|
use tokio::sync::mpsc;
|
2024-06-05 00:54:43 +02:00
|
|
|
use tokio_tungstenite::tungstenite::Message;
|
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
use crate::proto::{http::RegisterResponse, ws::{Event, EventInner}};
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum ChatError {
|
|
|
|
#[error("failed registering to chat: {0:?}")]
|
|
|
|
Register(#[from] reqwest::Error),
|
|
|
|
|
|
|
|
#[error("connection error with chat: {0:?}")]
|
|
|
|
Connect(#[from] tokio_tungstenite::tungstenite::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ChatEvent(chrono::DateTime<chrono::Utc>, ChatLine);
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ChatLine {
|
|
|
|
Message {
|
|
|
|
user: String,
|
|
|
|
body: String,
|
|
|
|
},
|
|
|
|
Error(String),
|
2025-01-13 22:42:01 +01:00
|
|
|
Rename(String, String),
|
2024-06-05 03:21:15 +02:00
|
|
|
Join(String),
|
|
|
|
Leave(String),
|
|
|
|
Connect(String),
|
2025-01-13 22:42:01 +01:00
|
|
|
Raw(String),
|
2024-06-05 00:54:43 +02:00
|
|
|
}
|
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
impl Display for ChatEvent {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let time = self.0.format("%H:%M:%S");
|
|
|
|
match &self.1 {
|
|
|
|
ChatLine::Error(err) => write!(f, "{time} /!\\ {err}"),
|
2025-01-13 22:42:01 +01:00
|
|
|
ChatLine::Rename(before, after) => write!(f, "{time} user <{before}> renamed to <{after}>"),
|
2024-06-05 03:21:15 +02:00
|
|
|
ChatLine::Join(user) => write!(f, "{time} [{user} joined]"),
|
|
|
|
ChatLine::Leave(user) => write!(f, "{time} [{user} left]"),
|
|
|
|
ChatLine::Connect(user) => write!(f, "{time} [{user} connected]"),
|
|
|
|
ChatLine::Message { user, body } => {
|
|
|
|
let pre = format!("{time} <{user}> ");
|
|
|
|
let pretty_body = dissolve::strip_html_tags(body).join("\n");
|
|
|
|
write!(f, "{pre}{pretty_body}")
|
|
|
|
},
|
2025-01-14 01:13:23 +01:00
|
|
|
ChatLine::Raw(s) => {
|
|
|
|
let without_html = dissolve::strip_html_tags(s).join("\n");
|
|
|
|
write!(f, "{time} {without_html}")
|
|
|
|
},
|
2024-06-05 03:21:15 +02:00
|
|
|
}
|
2024-06-05 00:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
pub struct Chat {
|
|
|
|
sink: ChatSink,
|
|
|
|
rx: mpsc::UnboundedReceiver<ChatEvent>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Chat {
|
2024-06-05 03:32:18 +02:00
|
|
|
pub async fn register(server: &str, token: Option<String>) -> Result<Chat, ChatError> {
|
|
|
|
let token = match token {
|
|
|
|
Some(t) => t,
|
|
|
|
None => {
|
|
|
|
let registration : RegisterResponse = reqwest::Client::new()
|
|
|
|
.post(format!("https://{server}/api/chat/register"))
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?;
|
|
|
|
registration.access_token
|
|
|
|
},
|
|
|
|
};
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:32:18 +02:00
|
|
|
let ws_url = format!("wss://{server}/ws?accessToken={token}");
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
let (ws, _response) = tokio_tungstenite::connect_async(ws_url).await?;
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
let (sink, stream) = ws.split();
|
|
|
|
let (tx, rx) = mpsc::unbounded_channel();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
if let Err(e) = Chat::worker(stream, tx).await {
|
|
|
|
eprintln!("chat channel closed while sending a message: {e}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(Chat { sink, rx })
|
|
|
|
}
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
pub async fn send(&mut self, message: crate::proto::ws::Action) -> Result<(), tokio_tungstenite::tungstenite::Error> {
|
|
|
|
self.sink.send(
|
|
|
|
Message::Text(
|
|
|
|
serde_json::to_string(&message).expect("error serializing ChatAction")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline] // because mpsc::recv is cancelable, but this wrapper may not be
|
|
|
|
pub async fn recv(&mut self) -> Option<ChatEvent> {
|
|
|
|
self.rx.recv().await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn worker(mut stream: ChatStream, chan: mpsc::UnboundedSender<ChatEvent>) -> Result<(), mpsc::error::SendError<ChatEvent>> {
|
|
|
|
while let Some(next) = stream.next().await {
|
2024-06-05 00:54:43 +02:00
|
|
|
match next {
|
2024-06-05 03:21:15 +02:00
|
|
|
Err(e) => chan.send(ChatEvent(chrono::Utc::now(), ChatLine::Error(format!("failed receiving message: {e}"))))?,
|
2024-06-05 00:54:43 +02:00
|
|
|
Ok(msg) => match msg {
|
2024-06-05 03:21:15 +02:00
|
|
|
Message::Close(_) => break,
|
|
|
|
Message::Binary(_) => chan.send(ChatEvent(chrono::Utc::now(), ChatLine::Error("unexpected binary payload".to_string())))?,
|
|
|
|
Message::Frame(_) => chan.send(ChatEvent(chrono::Utc::now(), ChatLine::Error("unexpected raw frame".to_string())))?,
|
2024-06-05 00:54:43 +02:00
|
|
|
Message::Ping(_) | tokio_tungstenite::tungstenite::Message::Pong(_) => {}, // ignore
|
|
|
|
Message::Text(payload) => for line in payload.lines() {
|
2024-06-05 03:21:15 +02:00
|
|
|
match serde_json::from_str::<Event>(line) {
|
2025-01-13 22:42:01 +01:00
|
|
|
Err(e) => {
|
|
|
|
chan.send(ChatEvent(chrono::Utc::now(), ChatLine::Error(format!("failed parsing message: {e}"))))?;
|
|
|
|
chan.send(ChatEvent(chrono::Utc::now(), ChatLine::Raw(line.to_string())))?;
|
|
|
|
},
|
2024-06-05 00:54:43 +02:00
|
|
|
Ok(event) => match event.inner {
|
2025-01-13 22:42:01 +01:00
|
|
|
EventInner::Chat { body, user, visible } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, if visible { ChatLine::Message { user: user.display_name, body } } else { ChatLine::Raw("| REDACTED |".into()) }))?,
|
|
|
|
EventInner::ConnectedUserInfo { user } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, ChatLine::Connect(user.display_name)))?,
|
|
|
|
EventInner::UserJoined { user } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, ChatLine::Join(user.display_name)))?,
|
|
|
|
EventInner::UserParted { user } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, ChatLine::Leave(user.display_name)))?,
|
|
|
|
EventInner::NameChange { old_name, user } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, ChatLine::Rename(old_name, user.display_name)))?,
|
|
|
|
EventInner::ChatAction { body } =>
|
|
|
|
chan.send(ChatEvent(event.timestamp, ChatLine::Raw(body)))?,
|
2024-06-05 00:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-06-05 03:21:15 +02:00
|
|
|
}
|
2024-06-05 00:54:43 +02:00
|
|
|
|
2024-06-05 03:21:15 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-06-05 00:54:43 +02:00
|
|
|
}
|
2024-06-05 03:21:15 +02:00
|
|
|
|
|
|
|
type ChatStream = futures::stream::SplitStream<tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>>;
|
|
|
|
type ChatSink = futures::stream::SplitSink<tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>, Message>;
|