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
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
src/events/user-status.ts
Normal file
94
src/events/user-status.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export type FriendUserStatus = {
|
||||
activeApp: string;
|
||||
state: "idle" | "resting";
|
||||
};
|
||||
|
||||
export const friendsUserStatuses = writable<Record<string, FriendUserStatus>>({});
|
||||
|
||||
let unlistenStatus: UnlistenFn | null = null;
|
||||
let unlistenFriendDisconnected: UnlistenFn | null = null;
|
||||
let isListening = false;
|
||||
|
||||
export async function initUserStatusListeners() {
|
||||
if (isListening) return;
|
||||
|
||||
try {
|
||||
unlistenStatus = await listen<unknown>("friend-user-status", (event) => {
|
||||
let payload = event.payload as any;
|
||||
if (typeof payload === "string") {
|
||||
try {
|
||||
payload = JSON.parse(payload);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse friend-user-status payload", error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const userId = payload?.userId as string | undefined;
|
||||
const status = payload?.status as FriendUserStatus | undefined;
|
||||
|
||||
if (!userId || !status) return;
|
||||
if (typeof status.activeApp !== "string" || status.activeApp.trim() === "") return;
|
||||
if (status.state !== "idle" && status.state !== "resting") return;
|
||||
|
||||
friendsUserStatuses.update((current) => ({
|
||||
...current,
|
||||
[userId]: {
|
||||
activeApp: status.activeApp,
|
||||
state: status.state,
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
unlistenFriendDisconnected = await listen<[{ userId: string }] | { userId: string } | string>(
|
||||
"friend-disconnected",
|
||||
(event) => {
|
||||
let payload = event.payload as any;
|
||||
if (typeof payload === "string") {
|
||||
try {
|
||||
payload = JSON.parse(payload);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse friend-disconnected payload", error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const data = Array.isArray(payload) ? payload[0] : payload;
|
||||
const userId = data?.userId as string | undefined;
|
||||
if (!userId) return;
|
||||
|
||||
friendsUserStatuses.update((current) => {
|
||||
const next = { ...current };
|
||||
delete next[userId];
|
||||
return next;
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
isListening = true;
|
||||
} catch (error) {
|
||||
console.error("Failed to initialize user status listeners", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function stopUserStatusListeners() {
|
||||
if (unlistenStatus) {
|
||||
unlistenStatus();
|
||||
unlistenStatus = null;
|
||||
}
|
||||
if (unlistenFriendDisconnected) {
|
||||
unlistenFriendDisconnected();
|
||||
unlistenFriendDisconnected = null;
|
||||
}
|
||||
isListening = false;
|
||||
}
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.dispose(() => {
|
||||
stopUserStatusListeners();
|
||||
});
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
initSceneInteractiveListener,
|
||||
stopSceneInteractiveListener,
|
||||
} from "../events/scene-interactive";
|
||||
import { initUserStatusListeners, stopUserStatusListeners } from "../events/user-status";
|
||||
|
||||
let { children } = $props();
|
||||
if (browser) {
|
||||
@@ -17,6 +18,7 @@
|
||||
await initAppDataListener();
|
||||
await initSceneInteractiveListener();
|
||||
await initInteractionListeners();
|
||||
await initUserStatusListeners();
|
||||
} catch (err) {
|
||||
console.error("Failed to initialize event listeners:", err);
|
||||
}
|
||||
@@ -26,6 +28,7 @@
|
||||
stopCursorTracking();
|
||||
stopSceneInteractiveListener();
|
||||
stopInteractionListeners();
|
||||
stopUserStatusListeners();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
} from "../../events/cursor";
|
||||
import { appData } from "../../events/app-data";
|
||||
import { sceneInteractive } from "../../events/scene-interactive";
|
||||
import { friendsUserStatuses } from "../../events/user-status";
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
@@ -33,11 +34,25 @@
|
||||
return friend?.friend.activeDoll;
|
||||
}
|
||||
|
||||
function getFriendStatus(userId: string) {
|
||||
return $friendsUserStatuses[userId];
|
||||
}
|
||||
|
||||
let appMetadata: AppMetadata | null = $state(null);
|
||||
|
||||
onMount(() => {
|
||||
const unlisten = listen<AppMetadata>("active-app-changed", (event) => {
|
||||
appMetadata = event.payload;
|
||||
const activeAppValue =
|
||||
appMetadata?.localized ?? appMetadata?.unlocalized ?? "";
|
||||
if (activeAppValue.trim()) {
|
||||
invoke("send_user_status_cmd", {
|
||||
activeApp: activeAppValue,
|
||||
state: "idle",
|
||||
}).catch((error) => {
|
||||
console.error("Failed to send user status", error);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
@@ -86,14 +101,20 @@
|
||||
<div class="flex flex-col gap-2">
|
||||
<div>
|
||||
{#each Object.entries($friendsCursorPositions) as [userId, position]}
|
||||
{@const status = getFriendStatus(userId)}
|
||||
<div class="badge py-3 text-xs text-left flex flex-row gap-2">
|
||||
<span class="font-bold">{getFriendById(userId).name}</span>
|
||||
<div class="flex flex-col font-mono">
|
||||
<div class="flex flex-row font-mono gap-2">
|
||||
<span>
|
||||
({position.mapped.x.toFixed(3)}, {position.mapped.y.toFixed(
|
||||
3,
|
||||
)})
|
||||
</span>
|
||||
{#if status}
|
||||
<span>
|
||||
{status.state} in {status.activeApp}
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
Reference in New Issue
Block a user