[Cleaned]
This commit is contained in:
parent
d2a4d0c7ae
commit
88f5752af5
11 changed files with 86 additions and 464 deletions
|
|
@ -1,38 +1,27 @@
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ConnectionType {
|
||||
use strum::IntoEnumIterator;
|
||||
use strum_macros::EnumIter;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, EnumIter, Eq)]
|
||||
#[allow(unused)]
|
||||
pub enum UserStatus {
|
||||
Online,
|
||||
UserOffline,
|
||||
IotaOffline,
|
||||
PhoneOnline,
|
||||
Away,
|
||||
DoNotDisturb,
|
||||
UserOffline,
|
||||
IotaOffline,
|
||||
}
|
||||
impl ConnectionType {
|
||||
pub fn to_str(&self) -> &str {
|
||||
match self {
|
||||
ConnectionType::Online => "online",
|
||||
ConnectionType::UserOffline => "user_offline",
|
||||
ConnectionType::IotaOffline => "iota_offline",
|
||||
ConnectionType::Away => "away",
|
||||
ConnectionType::DoNotDisturb => "do_not_disturb",
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
impl UserStatus {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
ConnectionType::Online => "online".to_string(),
|
||||
ConnectionType::UserOffline => "user_offline".to_string(),
|
||||
ConnectionType::IotaOffline => "iota_offline".to_string(),
|
||||
ConnectionType::Away => "away".to_string(),
|
||||
ConnectionType::DoNotDisturb => "do_not_disturb".to_string(),
|
||||
}
|
||||
format!("{:?}", self)
|
||||
}
|
||||
pub fn from_str(s: &str) -> Option<ConnectionType> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"online" => Some(ConnectionType::Online),
|
||||
"user_offline" => Some(ConnectionType::UserOffline),
|
||||
"iota_offline" => Some(ConnectionType::IotaOffline),
|
||||
"away" => Some(ConnectionType::Away),
|
||||
"do_not_disturb" => Some(ConnectionType::DoNotDisturb),
|
||||
_ => None,
|
||||
pub fn from_str(s: &str) -> Option<UserStatus> {
|
||||
for sel in UserStatus::iter() {
|
||||
if &sel.to_string() == s {
|
||||
return Some(sel);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -553,7 +553,7 @@ pub async fn get_iota_by_id(id: i64) -> Result<(i64, String), sqlx::Error> {
|
|||
id_u64 as i64,
|
||||
String::from_utf8_lossy(&public_key).to_string(),
|
||||
)),
|
||||
None => Err(sqlx::Error::RowNotFound),
|
||||
_ => Err(sqlx::Error::RowNotFound),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use crate::sql;
|
||||
use crate::sql::connection_status::ConnectionType;
|
||||
use crate::sql::connection_status::UserStatus;
|
||||
use dashmap::DashMap;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserStatus {
|
||||
pub connection_type: ConnectionType,
|
||||
pub struct UserConnection {
|
||||
pub connection_type: UserStatus,
|
||||
pub omikron_id: i64,
|
||||
}
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ static IOTA_PRIMARY_OMIKRON_CONNECTION: Lazy<DashMap<i64, i64>> = Lazy::new(Dash
|
|||
static IOTA_OMIKRON_CONNECTIONS: Lazy<DashMap<i64, Vec<i64>>> = Lazy::new(DashMap::new);
|
||||
|
||||
// UserID -> UserStatus
|
||||
static USER_STATUS_MAP: Lazy<DashMap<i64, UserStatus>> = Lazy::new(DashMap::new);
|
||||
static USER_STATUS_MAP: Lazy<DashMap<i64, UserConnection>> = Lazy::new(DashMap::new);
|
||||
|
||||
pub fn track_iota_connection(iota_id: i64, omikron_id: i64, primary: bool) {
|
||||
let mut entry = IOTA_OMIKRON_CONNECTIONS
|
||||
|
|
@ -60,24 +60,20 @@ pub fn get_iota_omikron_connections(iota_id: i64) -> Option<Vec<i64>> {
|
|||
IOTA_OMIKRON_CONNECTIONS.get(&iota_id).map(|v| v.clone())
|
||||
}
|
||||
|
||||
pub fn track_user_status(user_id: i64, status: ConnectionType, omikron_id: i64) {
|
||||
pub fn track_user_status(user_id: i64, status: UserStatus, omikron_id: i64) {
|
||||
USER_STATUS_MAP.insert(
|
||||
user_id,
|
||||
UserStatus {
|
||||
UserConnection {
|
||||
connection_type: status,
|
||||
omikron_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn get_user_status(user_id: i64) -> Option<UserStatus> {
|
||||
pub fn get_user_status(user_id: i64) -> Option<UserConnection> {
|
||||
USER_STATUS_MAP.get(&user_id).map(|v| v.clone())
|
||||
}
|
||||
|
||||
pub fn untrack_user(user_id: i64) {
|
||||
USER_STATUS_MAP.remove(&user_id);
|
||||
}
|
||||
|
||||
pub fn untrack_many_users(user_ids: &[i64]) {
|
||||
for user_id in user_ids {
|
||||
USER_STATUS_MAP.remove(user_id);
|
||||
|
|
|
|||
Loading…
Reference in a new issue