1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//! # 🧑 User API
//!
//! This API lets you get the details of the currently signed in user.
use crate::ffi::user_v1 as ffi;
#[doc(hidden)]
pub use ffi::API as FFI_API;
type PlayerId = u64;
// ----------------------------------------------------------------------------
/// The unique identifier of a user.
///
/// Note: This type intentionally doesn't implement `Display` because it is designed to
/// be opaque and not be displayed or printed out (except for debug purposes)
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "with_speedy", derive(speedy::Writable, speedy::Readable))]
pub struct UserId(pub String);
impl From<UserId> for Vec<u8> {
fn from(user_id: UserId) -> Self {
user_id.0.into_bytes()
}
}
impl AsRef<[u8]> for UserId {
fn as_ref(&self) -> &[u8] {
self.0.as_bytes()
}
}
const LOCAL_PLAYER_ID: PlayerId = 0;
// ----------------------------------------------------------------------------
/// The User API gives you access to the currently signed in user's name.
#[derive(Copy, Clone)]
pub struct User {}
impl User {
/// Returns the display name of the currently signed in user.
///
/// If the signed in user does not have a preferred nickname, their real name
/// will be used instead. If no name is available the function will return
/// None.
pub fn display_name() -> Option<String> {
Self::display_name_of(LOCAL_PLAYER_ID)
}
/// Returns the id of the currently signed in user.
///
/// This is the Embark external user id and is safe for external users to
/// know.
pub fn id() -> Option<UserId> {
Self::id_of(LOCAL_PLAYER_ID)
}
/// Returns the user id of the player with the given `player_id`.
/// Returns `None` if player is not on server or is not logged in.
pub fn id_of(player_id: PlayerId) -> Option<UserId> {
ffi::id_of(player_id)
.ok()
.and_then(|bytes| String::from_utf8(bytes).ok().map(UserId))
}
/// Returns the display name of the user with the given `player_id`.
/// Returns `None` if player is not on server or is not logged in.
pub fn display_name_of(player_id: PlayerId) -> Option<String> {
ffi::display_name_of(player_id).ok()
}
}