Add proof-of-concept server intercom service through auth server

This commit is contained in:
NG (Graham) committed 2025-10-26 15:43:51 -04:00
1 parent 3e09a5e554
commit b3ad2c3758
28 files changed
+613 -54

No files matched your search

@@ -0,0 +1,20 @@
pub struct DevMessage {
pub message: String,
pub duration: i32,
}
impl <C: Send + Sync + 'static> polariton_server::events::IntoEvent<C> for DevMessage {
const CHANNEL: u8 = 0;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
let mut params = polariton::operation::ParameterTable::with_capacity(2);
params.insert(2, polariton::operation::Typed::Bytes(self.message.as_bytes().to_vec().into()));
params.insert(15, polariton::operation::Typed::Int(self.duration));
polariton::operation::Event {
code: 1,
params,
}
}
}
+58
View File
@@ -0,0 +1,58 @@
use oj_rc_core::persist::user::IntercomListener;
use oj_rc_core::persist::user::intercom::IntercomWebServiceUserMessage;
pub struct IntercomHandler {
listener: IntercomListener<IntercomWebServiceUserMessage>,
user: std::sync::Weak<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
emitter: polariton_server::events::EventEmitter<()>,
}
impl IntercomHandler {
pub fn new(
listener: IntercomListener<IntercomWebServiceUserMessage>,
user: &std::sync::Arc<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
emitter: &polariton_server::events::EventEmitter<()>,
) -> Self {
Self {
listener,
user: std::sync::Arc::downgrade(user),
emitter: emitter.to_owned(),
}
}
async fn run_loop(
listener: IntercomListener<IntercomWebServiceUserMessage>,
user: std::sync::Weak<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
emitter: polariton_server::events::EventEmitter<()>
) {
use futures::StreamExt;
let mut listener = listener.listen().await;
while let Some(msg) = listener.next().await {
match msg {
Ok(msg) => {
if let Some(_user) = user.upgrade() {
match msg {
IntercomWebServiceUserMessage::DevMessage(msg) => {
let event = super::DevMessage {
message: msg.message,
duration: msg.duration as i32,
};
emitter.emit(event);
},
}
} else {
break;
}
},
Err(e) => {
log::error!("Bad intercom message received: {}", e);
}
}
}
}
pub fn run(self) -> tokio::task::JoinHandle<()> {
tokio::spawn(Self::run_loop(self.listener, self.user, self.emitter))
}
}
+5
View File
@@ -0,0 +1,5 @@
mod handler;
pub use handler::IntercomHandler;
mod dev_message;
pub use dev_message::DevMessage;