71 lines
2.1 KiB
Rust
71 lines
2.1 KiB
Rust
use std::time::Duration;
|
|
|
|
use freya::query::{Query, QueryCapability, UseQuery, use_query};
|
|
use oneclient_core::LauncherError;
|
|
use oneclient_mc::{self as minecraft, PlayerProfileView};
|
|
|
|
const PROFILE_STALE: Duration = Duration::from_secs(30 * 60);
|
|
const PROFILE_CLEAN: Duration = Duration::from_secs(2 * 60 * 60);
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct FetchPlayerProfileQuery;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
pub struct PlayerProfileQueryKeys {
|
|
pub uuid: String,
|
|
pub access_token: Option<String>,
|
|
}
|
|
|
|
impl QueryCapability for FetchPlayerProfileQuery {
|
|
type Ok = PlayerProfileView;
|
|
type Err = LauncherError;
|
|
type Keys = PlayerProfileQueryKeys;
|
|
|
|
async fn run(&self, keys: &Self::Keys) -> Result<Self::Ok, Self::Err> {
|
|
let uuid_str = &keys.uuid;
|
|
if uuid_str.is_empty() {
|
|
return Ok(PlayerProfileView::default());
|
|
}
|
|
|
|
if let Ok(u) = uuid::Uuid::parse_str(uuid_str) {
|
|
let is_offline = u.get_version() == Some(uuid::Version::Md5) || u.is_nil();
|
|
if is_offline {
|
|
return Ok(PlayerProfileView {
|
|
uuid: uuid_str.clone(),
|
|
..Default::default()
|
|
});
|
|
}
|
|
}
|
|
|
|
let access_token = keys.access_token.clone();
|
|
let state = crate::launcher::state()?;
|
|
let client = &state.services.requester;
|
|
|
|
Ok(
|
|
minecraft::fetch_player_profile_view(client, &keys.uuid, access_token.as_deref())
|
|
.await?,
|
|
)
|
|
}
|
|
|
|
// TODO Cache
|
|
// fn matches(&self, keys: &Self::Keys) -> bool {
|
|
// keys.uuid.as_deref().is_some_and(|id| !id.is_empty())
|
|
// }
|
|
}
|
|
|
|
pub fn use_player_profile(
|
|
uuid: impl Into<String>,
|
|
access_token: Option<impl Into<String>>,
|
|
) -> UseQuery<FetchPlayerProfileQuery> {
|
|
use_query(
|
|
Query::new(
|
|
PlayerProfileQueryKeys {
|
|
uuid: uuid.into(),
|
|
access_token: access_token.map(|s| s.into()),
|
|
},
|
|
FetchPlayerProfileQuery,
|
|
)
|
|
.stale_time(PROFILE_STALE)
|
|
.clean_time(PROFILE_CLEAN),
|
|
)
|
|
}
|