mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement basic chat functionality
This commit is contained in:
@@ -1,10 +1,52 @@
|
||||
use polariton::operation::Typed;
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
|
||||
pub struct MoreLobbyAuth;
|
||||
use crate::persist::chat_user::{ChatUser, ChatUserImpl};
|
||||
|
||||
pub struct MoreLobbyAuth {
|
||||
chat_system: crate::state::chat::ChatImpl,
|
||||
root: std::path::PathBuf,
|
||||
}
|
||||
|
||||
impl MoreLobbyAuth {
|
||||
const AUTH_PAYLOAD_KEY: u8 = 245;
|
||||
|
||||
pub fn new(chat_system: crate::state::chat::ChatImpl, root: impl AsRef<std::path::Path>) -> Self {
|
||||
Self {
|
||||
chat_system,
|
||||
root: root.as_ref().to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_ext_map(&self, token: &rc_core::persist::user::UserToken) -> Option<std::collections::HashMap<std::any::TypeId, Box<dyn std::any::Any + Send + Sync + 'static>>> {
|
||||
let user_dir = self.root.join(rc_core::persist::user::USERS_DIR).join(&token.uuid);
|
||||
let data = if let Ok(data) = ChatUserImpl::load(&user_dir) {
|
||||
data
|
||||
} else {
|
||||
let data = ChatUserImpl::default_load(&user_dir);
|
||||
data
|
||||
};
|
||||
let mut map = std::collections::HashMap::with_capacity(1);
|
||||
map.insert(std::any::TypeId::of::<ChatUserImpl>(), Box::new(data) as _);
|
||||
Some(map)
|
||||
}
|
||||
|
||||
fn do_auth<C>(&self, params: std::collections::HashMap<u8, Typed<C>>, user: &crate::UserTy) -> Result<polariton::operation::ParameterTable<C>, i16> {
|
||||
if let Some(Typed::Str(auth_payload)) = params.get(&Self::AUTH_PAYLOAD_KEY) {
|
||||
if user.update_with_auth_ext(&auth_payload.string, |t| self.build_ext_map(t)) {
|
||||
let user_impl = user.user()?;
|
||||
let name = user_impl.token().uuid.clone();
|
||||
let chat_user = super::get_chat_user(user_impl.as_ref().as_ref());
|
||||
let channels = chat_user.subscribed_channels_strings();
|
||||
let event_tx = user.event_sender();
|
||||
self.chat_system.system_mut().connect_user(name, channels, event_tx);
|
||||
let mut resp_params = std::collections::HashMap::new();
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return Ok(resp_params.into());
|
||||
}
|
||||
}
|
||||
Err(120)
|
||||
}
|
||||
}
|
||||
|
||||
impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
|
||||
@@ -12,24 +54,24 @@ impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
|
||||
|
||||
fn handle(&self, params: polariton::operation::ParameterTable<C>, user: &Self::User) -> polariton::operation::OperationResponse<C> {
|
||||
let params_dict = params.to_dict();
|
||||
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
|
||||
if user.update_with_auth(&auth_payload.string) {
|
||||
let mut resp_params = std::collections::HashMap::new();
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return polariton::operation::OperationResponse {
|
||||
code: 230,
|
||||
match self.do_auth(params_dict, user) {
|
||||
Ok(params) => {
|
||||
polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: 0,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: resp_params.into(),
|
||||
params,
|
||||
}
|
||||
},
|
||||
Err(code) => {
|
||||
polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: code,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: std::collections::HashMap::new().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
polariton::operation::OperationResponse {
|
||||
code: 230,
|
||||
return_code: 120,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: std::collections::HashMap::new().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user