user active app reporting
This commit is contained in:
@@ -6,6 +6,7 @@ pub mod dolls;
|
||||
pub mod friends;
|
||||
pub mod interaction;
|
||||
pub mod sprite;
|
||||
pub mod user_status;
|
||||
|
||||
use crate::state::{init_app_data_scoped, AppDataRefreshScope, FDOLL};
|
||||
use crate::lock_r;
|
||||
|
||||
9
src-tauri/src/commands/user_status.rs
Normal file
9
src-tauri/src/commands/user_status.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use crate::services::ws::UserStatusPayload;
|
||||
use crate::services::ws::report_user_status;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn send_user_status_cmd(active_app: String, state: String) -> Result<(), String> {
|
||||
let payload = UserStatusPayload { active_app, state };
|
||||
report_user_status(payload).await;
|
||||
Ok(())
|
||||
}
|
||||
@@ -16,6 +16,7 @@ use commands::friends::{
|
||||
};
|
||||
use commands::interaction::send_interaction_cmd;
|
||||
use commands::sprite::recolor_gif_base64;
|
||||
use commands::user_status::send_user_status_cmd;
|
||||
use tauri::async_runtime;
|
||||
use tauri::Manager;
|
||||
use tracing_subscriber::{self, util::SubscriberInitExt};
|
||||
@@ -32,6 +33,7 @@ mod state;
|
||||
mod system_tray;
|
||||
mod utilities;
|
||||
|
||||
|
||||
/// Tauri app handle
|
||||
pub fn get_app_handle<'a>() -> &'a tauri::AppHandle<tauri::Wry> {
|
||||
APP_HANDLE
|
||||
@@ -138,7 +140,8 @@ pub fn run() {
|
||||
set_pet_menu_state,
|
||||
start_auth_flow,
|
||||
logout_and_restart,
|
||||
send_interaction_cmd
|
||||
send_interaction_cmd,
|
||||
send_user_status_cmd
|
||||
])
|
||||
.setup(|app| {
|
||||
APP_HANDLE
|
||||
|
||||
@@ -174,3 +174,18 @@ pub fn on_friend_active_doll_changed(payload: Payload, _socket: RawClient) {
|
||||
_ => error!("Received unexpected payload format for friend-active-doll-changed"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_friend_user_status(payload: Payload, _socket: RawClient) {
|
||||
match payload {
|
||||
Payload::Text(values) => {
|
||||
if let Some(first_value) = values.first() {
|
||||
if let Err(e) = get_app_handle().emit(WS_EVENT::FRIEND_USER_STATUS, first_value) {
|
||||
error!("Failed to emit friend-user-status event: {:?}", e);
|
||||
}
|
||||
} else {
|
||||
info!("Received friend-user-status event with empty payload");
|
||||
}
|
||||
}
|
||||
_ => error!("Received unexpected payload format for friend-user-status"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ pub fn register_event_handlers(builder: ClientBuilder) -> ClientBuilder {
|
||||
WS_EVENT::FRIEND_ACTIVE_DOLL_CHANGED,
|
||||
super::friend::on_friend_active_doll_changed,
|
||||
)
|
||||
.on(WS_EVENT::FRIEND_USER_STATUS, super::friend::on_friend_user_status)
|
||||
.on(WS_EVENT::DOLL_CREATED, super::doll::on_doll_created)
|
||||
.on(WS_EVENT::DOLL_UPDATED, super::doll::on_doll_updated)
|
||||
.on(WS_EVENT::DOLL_DELETED, super::doll::on_doll_deleted)
|
||||
|
||||
@@ -29,6 +29,8 @@ impl WS_EVENT {
|
||||
pub const FRIEND_DOLL_UPDATED: &str = "friend-doll-updated";
|
||||
pub const FRIEND_DOLL_DELETED: &str = "friend-doll-deleted";
|
||||
pub const FRIEND_ACTIVE_DOLL_CHANGED: &str = "friend-active-doll-changed";
|
||||
pub const FRIEND_USER_STATUS: &str = "friend-user-status";
|
||||
pub const CLIENT_REPORT_USER_STATUS: &str = "client-report-user-status";
|
||||
pub const DOLL_CREATED: &str = "doll_created";
|
||||
pub const DOLL_UPDATED: &str = "doll_updated";
|
||||
pub const DOLL_DELETED: &str = "doll_deleted";
|
||||
@@ -46,6 +48,8 @@ mod doll;
|
||||
mod friend;
|
||||
mod handlers;
|
||||
mod interaction;
|
||||
mod user_status;
|
||||
|
||||
pub use client::init_ws_client;
|
||||
pub use cursor::report_cursor_data;
|
||||
pub use user_status::{report_user_status, UserStatusPayload};
|
||||
|
||||
71
src-tauri/src/services/ws/user_status.rs
Normal file
71
src-tauri/src/services/ws/user_status.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use rust_socketio::Payload;
|
||||
use tauri::async_runtime;
|
||||
use tracing::error;
|
||||
|
||||
use crate::{
|
||||
lock_r,
|
||||
services::health_manager::show_health_manager_with_error,
|
||||
state::FDOLL,
|
||||
};
|
||||
|
||||
use super::WS_EVENT;
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UserStatusPayload {
|
||||
pub active_app: String,
|
||||
pub state: String,
|
||||
}
|
||||
|
||||
pub async fn report_user_status(status: UserStatusPayload) {
|
||||
let payload_value = match serde_json::to_value(&status) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
error!("Failed to serialize user status payload: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let (client_opt, is_initialized) = {
|
||||
let guard = lock_r!(FDOLL);
|
||||
if let Some(clients) = &guard.network.clients {
|
||||
(
|
||||
clients.ws_client.as_ref().cloned(),
|
||||
clients.is_ws_initialized,
|
||||
)
|
||||
} else {
|
||||
(None, false)
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(client) = client_opt {
|
||||
if !is_initialized {
|
||||
return;
|
||||
}
|
||||
|
||||
match async_runtime::spawn_blocking(move || {
|
||||
client.emit(
|
||||
WS_EVENT::CLIENT_REPORT_USER_STATUS,
|
||||
Payload::Text(vec![payload_value]),
|
||||
)
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Ok(_)) => (),
|
||||
Ok(Err(e)) => {
|
||||
error!("Failed to emit user status report: {}", e);
|
||||
show_health_manager_with_error(Some(format!(
|
||||
"WebSocket emit failed: {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to execute blocking task for user status report: {}", e);
|
||||
show_health_manager_with_error(Some(format!(
|
||||
"WebSocket task failed: {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user