yggdrasil/src/util/structs/profile_attributes.rs

94 lines
3.2 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
#[derive(Deserialize, Debug)]
pub struct AttributeEnabled {
pub enabled: bool
}
#[derive(Deserialize, Debug)]
pub struct ProfanityFilter {
#[serde(rename = "profanityFilterOn")]
pub profanity_filter: bool
}
#[derive(Deserialize, Debug)]
pub struct ProfileAttributesPrivileges {
#[serde(rename = "onlineChat")]
pub online_chat: AttributeEnabled,
#[serde(rename = "multiplayerServer")]
pub multiplayer_server: AttributeEnabled,
#[serde(rename = "multiplayerRealms")]
pub multiplayer_realms: AttributeEnabled,
pub telemetry: AttributeEnabled,
}
#[derive(Deserialize, Debug)]
pub struct ProfileAttributes {
pub privileges: ProfileAttributesPrivileges,
#[serde(rename = "profanityFilterPreferences")]
pub profanity_filter: ProfanityFilter,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ProfileAttributesSimple {
pub can_chat: bool,
pub can_play_multiplayer: bool,
pub can_play_realms: bool,
pub use_filter: bool,
}
impl ProfileAttributesSimple {
pub fn to_json(&self) -> Value {
json!({
"privileges": {
"onlineChat": { "enabled": self.can_chat },
"multiplayerServer": { "enabled": self.can_play_multiplayer },
"multiplayerRealms": { "enabled": self.can_play_realms },
"telemetry": { "enabled": false },
},
"profanityFilterPreferences": {
"profanityFilterOn": self.use_filter
}
})
}
pub fn to_full(&self) -> ProfileAttributes {
ProfileAttributes {
privileges: ProfileAttributesPrivileges {
online_chat: AttributeEnabled { enabled: self.can_chat },
multiplayer_server: AttributeEnabled { enabled: self.can_play_multiplayer },
multiplayer_realms: AttributeEnabled { enabled: self.can_play_realms },
telemetry: AttributeEnabled { enabled: false },
},
profanity_filter: ProfanityFilter { profanity_filter: self.use_filter },
}
}
}
impl ProfileAttributes {
pub fn to_simple(&self) -> ProfileAttributesSimple {
ProfileAttributesSimple {
can_chat: self.privileges.online_chat.enabled,
can_play_multiplayer: self.privileges.multiplayer_server.enabled,
can_play_realms: self.privileges.multiplayer_realms.enabled,
use_filter: self.profanity_filter.profanity_filter,
}
}
}