mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Move chat system into core, merge configs, save to database
This commit is contained in:
@@ -1,61 +1,35 @@
|
||||
use crate::SimpleChatFunc;
|
||||
use crate::persist::chat_user::ChatUser;
|
||||
use polariton::operation::ParameterTable;
|
||||
//use rc_core::persist::user::ChatUser;
|
||||
use polariton::operation::{ParameterTable, OperationResponse};
|
||||
|
||||
const CODE: u8 = 11;
|
||||
|
||||
const PARAM_KEY: u8 = 18;
|
||||
|
||||
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();
|
||||
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 {
|
||||
channel_name: "RE_public_channel0".to_owned(),
|
||||
members: vec![
|
||||
ChatChannelMember {
|
||||
name: "RE_chat0_username0".to_owned(),
|
||||
use_custom_avatar: false,
|
||||
state: ChatPlayerState::Idk1,
|
||||
custom_avatar: Vec::default(),
|
||||
avatar_id: 2,
|
||||
},
|
||||
ChatChannelMember {
|
||||
name: "RE_chat0_username1".to_owned(),
|
||||
use_custom_avatar: false,
|
||||
state: ChatPlayerState::Idk2,
|
||||
custom_avatar: Vec::default(),
|
||||
avatar_id: 3,
|
||||
},
|
||||
],
|
||||
channel_ty: ChatChannelType::Public,
|
||||
}.as_transmissible(),
|
||||
ChatChannelInfo {
|
||||
channel_name: "RE_custom_channel1".to_owned(),
|
||||
members: vec![
|
||||
ChatChannelMember {
|
||||
name: "RE_chat1_username0".to_owned(),
|
||||
use_custom_avatar: false,
|
||||
state: ChatPlayerState::Idk0,
|
||||
custom_avatar: Vec::default(),
|
||||
avatar_id: 2,
|
||||
},
|
||||
ChatChannelMember {
|
||||
name: "RE_chat1_username1".to_owned(),
|
||||
use_custom_avatar: false,
|
||||
state: ChatPlayerState::Idk1,
|
||||
custom_avatar: Vec::default(),
|
||||
avatar_id: 3,
|
||||
},
|
||||
],
|
||||
channel_ty: ChatChannelType::Custom,
|
||||
}.as_transmissible(),
|
||||
],
|
||||
}));*/
|
||||
Ok(params.into())
|
||||
}, chat_system)
|
||||
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
let user_info = user.user()?;
|
||||
params.insert(PARAM_KEY, user_info.subscribed_channels().await?);
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct JoinedChannelsProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for JoinedChannelsProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for JoinedChannelsProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(super) fn all_channels_provider() -> JoinedChannelsProvider {
|
||||
JoinedChannelsProvider
|
||||
}
|
||||
|
||||
@@ -1,41 +1,85 @@
|
||||
use crate::{persist::chat_user::ChatUser, SimpleChatFunc};
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
use polariton::operation::{ParameterTable, Typed, OperationResponse};
|
||||
|
||||
const JOIN_CODE: u8 = 1;
|
||||
const LEAVE_CODE: u8 = 5;
|
||||
|
||||
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 struct JoinChannelProvider {
|
||||
chat_system: crate::state::ChatImpl,
|
||||
}
|
||||
|
||||
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 _)?);
|
||||
pub(super) fn join_channel_provider(chat_system: crate::state::ChatImpl) -> JoinChannelProvider {
|
||||
JoinChannelProvider { chat_system }
|
||||
}
|
||||
|
||||
async fn do_join_handling(params: ParameterTable<()>, user: &crate::UserTy, chat_system: &crate::state::ChatImpl) -> Result<ParameterTable, i16> {
|
||||
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 = user_info.add_subscribed_channel(chann_name.string, crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?).await?;
|
||||
params.insert(CHANNEL_INFO_PARAM_KEY, response);
|
||||
}
|
||||
Ok(params.into())
|
||||
}, chat_system)
|
||||
}
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for JoinChannelProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<JOIN_CODE, ()>(do_join_handling(params, user, &self.chat_system).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for JoinChannelProvider {
|
||||
fn op_code() -> u8 {
|
||||
JOIN_CODE
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LeaveChannelProvider {
|
||||
chat_system: crate::state::ChatImpl,
|
||||
}
|
||||
|
||||
pub(super) fn leave_channel_provider(chat_system: crate::state::ChatImpl) -> LeaveChannelProvider {
|
||||
LeaveChannelProvider { chat_system }
|
||||
}
|
||||
|
||||
async fn do_leave_handling(params: ParameterTable<()>, user: &crate::UserTy, chat_system: &crate::state::ChatImpl) -> Result<ParameterTable, i16> {
|
||||
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());
|
||||
user_info.remove_subscribed_channel(chann_name.string, crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?).await?;
|
||||
}
|
||||
}
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for LeaveChannelProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<LEAVE_CODE, ()>(do_leave_handling(params, user, &self.chat_system).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for LeaveChannelProvider {
|
||||
fn op_code() -> u8 {
|
||||
LEAVE_CODE
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,26 +6,24 @@ mod send_message;
|
||||
mod public_channels;
|
||||
mod join_channel;
|
||||
mod user_online;
|
||||
mod subscribed_channels;
|
||||
|
||||
use polariton_server::operations::OperationsHandler;
|
||||
|
||||
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> {
|
||||
pub fn handler(chat_system: crate::state::chat::ChatImpl, conf: &rc_core::persist::config::ConfigImpl) -> OperationsHandler<crate::UserTy> {
|
||||
OperationsHandler::new()
|
||||
.modify(rc_core::polariton::OpIdCopy)
|
||||
.add(more_auth::MoreLobbyAuth::new(chat_system.clone(), data_root))
|
||||
.add(more_auth::MoreLobbyAuth::new(chat_system.clone()))
|
||||
.add(chat_ignores::ignores_provider())
|
||||
.add(pending_sanctions::pending_sanctions_checker())
|
||||
.add(all_joined_channels::all_channels_provider(chat_system.clone()))
|
||||
.add(polariton_server::operations::Ack::<12, _>::default())
|
||||
.add(all_joined_channels::all_channels_provider())
|
||||
//.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(subscribed_channels::all_subbed_channels_provider())
|
||||
//.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()
|
||||
}
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
use polariton::operation::Typed;
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
|
||||
use crate::persist::chat_user::{ChatUser, ChatUserImpl};
|
||||
//use crate::persist::chat_user::{ChatUser, ChatUserImpl};
|
||||
//use rc_core::persist::user::ChatUser;
|
||||
|
||||
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 {
|
||||
pub fn new(chat_system: crate::state::chat::ChatImpl) -> 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>>> {
|
||||
/*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
|
||||
@@ -29,15 +28,15 @@ impl MoreLobbyAuth {
|
||||
let mut map = std::collections::HashMap::with_capacity(1);
|
||||
map.insert(std::any::TypeId::of::<ChatUserImpl>(), Box::new(data) as _);
|
||||
Some(map)
|
||||
}
|
||||
}*/
|
||||
|
||||
async 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)).await {
|
||||
if user.update_with_auth(&auth_payload.string).await {
|
||||
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 chat_user = super::get_chat_user(user_impl.as_ref().as_ref());
|
||||
let channels = user_impl.subscribed_channels_strings().await?;
|
||||
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();
|
||||
|
||||
35
rc_chat_room/src/operations/subscribed_channels.rs
Normal file
35
rc_chat_room/src/operations/subscribed_channels.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
//use rc_core::persist::user::ChatUser;
|
||||
use polariton::operation::{ParameterTable, OperationResponse};
|
||||
|
||||
const CODE: u8 = 12;
|
||||
|
||||
const PARAM_KEY: u8 = 18;
|
||||
|
||||
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
let user_info = user.user()?;
|
||||
params.insert(PARAM_KEY, user_info.subscribed_channels().await?);
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct SubscribedChannelsProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for SubscribedChannelsProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for SubscribedChannelsProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(super) fn all_subbed_channels_provider() -> SubscribedChannelsProvider {
|
||||
SubscribedChannelsProvider
|
||||
}
|
||||
Reference in New Issue
Block a user