1
0
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:
NGnius (Graham)
2025-04-14 21:17:41 -04:00
parent 4d2ddaeafb
commit 792a6362e3
37 changed files with 1119 additions and 34 deletions

View File

@@ -1,14 +1,17 @@
use polariton_server::operations::SimpleFunc;
use polariton::operation::{ParameterTable, Typed, Arr};
use crate::data::channel::*;
use crate::SimpleChatFunc;
use crate::persist::chat_user::ChatUser;
use polariton::operation::ParameterTable;
const PARAM_KEY: u8 = 18;
pub(super) fn all_channels_provider() -> SimpleFunc<11, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleFunc::new(|params, _| {
pub(super) fn all_channels_provider(chat_system: crate::state::ChatImpl) -> SimpleChatFunc<11, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleChatFunc::new(|params, user: &crate::UserTy, _chat_system: &crate::state::ChatImpl| {
let mut params = params.to_dict();
params.insert(PARAM_KEY, Typed::Arr(Arr {
let user_trait = user.user()?;
let chat_user = super::get_chat_user(user_trait.as_ref().as_ref());
//let chat_user: &ChatUserImpl = user_trait.ext(std::any::TypeId::of::<ChatUserImpl>()).unwrap().downcast_ref().unwrap();
params.insert(PARAM_KEY, chat_user.subscribed_channels());
/*params.insert(PARAM_KEY, Typed::Arr(Arr {
ty: polariton::serdes::TypePrefix::HashMap, // hashtable
items: vec![
ChatChannelInfo {
@@ -52,7 +55,7 @@ pub(super) fn all_channels_provider() -> SimpleFunc<11, crate::UserTy, impl (Fn(
channel_ty: ChatChannelType::Custom,
}.as_transmissible(),
],
}));
}));*/
Ok(params.into())
})
}, chat_system)
}

View File

@@ -0,0 +1,41 @@
use crate::{persist::chat_user::ChatUser, SimpleChatFunc};
use polariton::operation::{ParameterTable, Typed};
const CHANNEL_NAME_PARAM_KEY: u8 = 3; // str; in
const CHANNEL_TYPE_PARAM_KEY: u8 = 1; // int; in
const CHANNEL_PASSWORD_PARAM_KEY: u8 = 16; // str; in
const CHANNEL_INFO_PARAM_KEY: u8 = 17; // hashtable; out
pub(super) fn join_channel_provider(chat_system: crate::state::ChatImpl) -> SimpleChatFunc<1, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleChatFunc::new(|params, user: &crate::UserTy, chat_system| {
let mut params = params.to_dict();
if let Some(Typed::Str(chann_name)) = params.remove(&CHANNEL_NAME_PARAM_KEY) {
if let Some(Typed::Int(chann_ty)) = params.remove(&CHANNEL_TYPE_PARAM_KEY) {
if let Some(Typed::Str(channel_pwd)) = params.remove(&CHANNEL_PASSWORD_PARAM_KEY) {
log::warn!("Received channel password {} which is unsupported", channel_pwd.string);
}
let user_info = user.user()?;
let chat_user = super::get_chat_user(user_info.as_ref().as_ref());
chat_system.system_mut().join_channel(user_info.token().uuid.clone(), chann_name.string.clone());
let response = chat_user.add_subscribed_channel(chann_name.string, crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?);
params.insert(CHANNEL_INFO_PARAM_KEY, response);
}
}
Ok(params.into())
}, chat_system)
}
pub(super) fn leave_channel_provider(chat_system: crate::state::ChatImpl) -> SimpleChatFunc<5, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleChatFunc::new(|params, user: &crate::UserTy, chat_system| {
let mut params = params.to_dict();
if let Some(Typed::Str(chann_name)) = params.remove(&CHANNEL_NAME_PARAM_KEY) {
if let Some(Typed::Int(chann_ty)) = params.remove(&CHANNEL_TYPE_PARAM_KEY) {
let user_info = user.user()?;
let chat_user = super::get_chat_user(user_info.as_ref().as_ref());
chat_system.system_mut().leave_channel(user_info.token().uuid.clone(), chann_name.string.clone());
chat_user.remove_subscribed_channel(chann_name.string, crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?);
}
}
Ok(params.into())
}, chat_system)
}

View File

@@ -2,15 +2,29 @@ mod more_auth;
mod chat_ignores;
mod pending_sanctions;
mod all_joined_channels;
mod send_message;
mod public_channels;
mod join_channel;
mod user_online;
use polariton_server::operations::OperationsHandler;
pub fn handler() -> OperationsHandler<crate::UserTy> {
pub fn handler(chat_system: crate::state::chat::ChatImpl, data_root: impl AsRef<std::path::Path>, conf: &rc_core::persist::config::ConfigImpl) -> OperationsHandler<crate::UserTy> {
OperationsHandler::new()
.add(more_auth::MoreLobbyAuth)
.add(more_auth::MoreLobbyAuth::new(chat_system.clone(), data_root))
.add(chat_ignores::ignores_provider())
.add(pending_sanctions::pending_sanctions_checker())
.add(all_joined_channels::all_channels_provider())
.add(all_joined_channels::all_channels_provider(chat_system.clone()))
.add(polariton_server::operations::Ack::<12, _>::default())
.add(send_message::send_public_message_handler(chat_system.clone()))
.add(public_channels::public_channels_provider(conf))
.add(join_channel::join_channel_provider(chat_system.clone()))
.add(user_online::is_online_provider(chat_system.clone()))
.add(send_message::send_private_message_handler(chat_system.clone()))
.add(join_channel::leave_channel_provider(chat_system.clone()))
//.add(polariton_server::operations::Ack::<00000, _>::default())
}
pub(self) fn get_chat_user<'a, C>(user: &'a dyn rc_core::persist::user::User<C>) -> &'a crate::persist::chat_user::ChatUserImpl {
user.ext(std::any::TypeId::of::<crate::persist::chat_user::ChatUserImpl>()).unwrap().downcast_ref().unwrap()
}

View File

@@ -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(),
}
}
}

View File

@@ -0,0 +1,20 @@
use polariton_server::operations::Immediate;
//use polariton::operation::{ParameterTable, Typed};
use rc_core::ConfigProvider;
const PARAM_KEY: u8 = 20;
pub(super) fn public_channels_provider(conf: &rc_core::persist::config::ConfigImpl) -> Immediate<13, crate::UserTy> {
let pub_channs = conf.public_channels();
Immediate::new(move || {
let mut params = std::collections::HashMap::with_capacity(1);
params.insert(PARAM_KEY, pub_channs.to_owned());
/*params.insert(PARAM_KEY, Typed::Arr(Arr {
ty: polariton::serdes::TypePrefix::Str,
items: vec![
Typed::Str("Pluto".into()),
],
}));*/
params.into()
})
}

View File

@@ -0,0 +1,51 @@
use polariton::operation::{ParameterTable, Typed};
const CHANNEL_TYPE_PARAM_KEY: u8 = 1; // in; int
const MESSAGE_TEXT_PARAM_KEY: u8 = 2; // in; str
const CHANNEL_NAME_PARAM_KEY: u8 = 3; // in; str
const CHAT_LOCATION_PARAM_KEY: u8 = 29; // in; str
pub fn send_public_message_handler(chat_system: crate::state::chat::ChatImpl) -> crate::SimpleChatFunc<2, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::chat::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
crate::SimpleChatFunc::new(|params, user: &crate::UserTy, chat| {
let mut params = params.to_dict();
if let Some(Typed::Int(channel_ty)) = params.remove(&CHANNEL_TYPE_PARAM_KEY) {
let channel_enum = crate::data::channel::ChatChannelType::from_u8(channel_ty as u8)?;
if let Some(Typed::Str(channel_name)) = params.remove(&CHANNEL_NAME_PARAM_KEY) {
if let Some(Typed::Str(message_text)) = params.remove(&MESSAGE_TEXT_PARAM_KEY) {
let chat_loc = if let Some(Typed::Str(chat_loc)) = params.remove(&CHAT_LOCATION_PARAM_KEY) {
chat_loc.string.clone()
} else {
"<unknown location>".to_owned()
};
let user = user.user()?;
let chat_system = chat.system();
log::debug!("Got message `{}` from user {} ({} @ {}/{:?})", message_text.string, user.token().uuid, chat_loc, channel_name.string, channel_enum);
chat_system.handle_public_message(user.as_ref().as_ref(), message_text.string, channel_name.string, channel_enum);
}
}
}
Ok(params.into())
}, chat_system)
}
pub const USERNAME_PARAM_KEY: u8 = 7; // in; str
pub fn send_private_message_handler(chat_system: crate::state::chat::ChatImpl) -> crate::SimpleChatFunc<3, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::chat::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
crate::SimpleChatFunc::new(|params, user: &crate::UserTy, chat| {
let mut params = params.to_dict();
if let Some(Typed::Str(username)) = params.remove(&USERNAME_PARAM_KEY) {
if let Some(Typed::Str(message_text)) = params.remove(&MESSAGE_TEXT_PARAM_KEY) {
let chat_loc = if let Some(Typed::Str(chat_loc)) = params.remove(&CHAT_LOCATION_PARAM_KEY) {
chat_loc.string.clone()
} else {
"<unknown location>".to_owned()
};
let user = user.user()?;
let chat_system = chat.system();
log::debug!("Got message `{}` from user {} (@ {} to {})", message_text.string, user.token().uuid, chat_loc, username.string);
chat_system.handle_private_message(user.as_ref().as_ref(), message_text.string, username.string);
}
}
Ok(params.into())
}, chat_system)
}

View File

@@ -0,0 +1,36 @@
use crate::SimpleChatFunc;
use polariton::operation::{ParameterTable, Typed};
const USERNAME_PARAM_KEY: u8 = 22; // str; in & out
const DISPLAY_NAME_PARAM_KEY: u8 = 30; // str; out
const CAN_SEND_DM_PARAM_KEY: u8 = 27; // int; out
#[allow(dead_code)]
#[repr(u8)]
enum CanSendMessageResult {
Ok = 0,
UserDoesNotExist = 1,
UserOffline = 2,
}
pub(super) fn is_online_provider(chat_system: crate::state::ChatImpl) -> SimpleChatFunc<14, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy, &crate::state::ChatImpl) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleChatFunc::new(|params, _: &crate::UserTy, chat_system| {
let mut params = params.to_dict();
if let Some(Typed::Str(user_name)) = params.get(&USERNAME_PARAM_KEY) {
let username = user_name.string.clone();
params.insert(DISPLAY_NAME_PARAM_KEY, Typed::Str(username.clone().into()));
if chat_system.system().is_user_online(&username) {
params.insert(CAN_SEND_DM_PARAM_KEY, Typed::Int(CanSendMessageResult::Ok as _));
Ok(params.into())
} else {
log::debug!("User {} is not online", username);
params.insert(CAN_SEND_DM_PARAM_KEY, Typed::Int(CanSendMessageResult::UserOffline as _));
//params.insert(CAN_SEND_DM_PARAM_KEY, Typed::Int(CanSendMessageResult::Ok as _));
Ok(params.into())
}
} else {
Err(rc_core::data::error_codes::ChatErrorCodes::UnexpectedError as _)
}
}, chat_system)
}