moved user status data validation from front to backend

This commit is contained in:
2026-03-09 12:45:34 +08:00
parent 69cfebee3d
commit 03ae3e0829
4 changed files with 23 additions and 8 deletions

View File

@@ -19,6 +19,20 @@ pub struct UserStatusPayload {
pub state: UserStatusState,
}
impl UserStatusPayload {
pub fn has_presence_content(&self) -> bool {
self.presence_status
.title
.as_ref()
.is_some_and(|title| !title.trim().is_empty())
|| self
.presence_status
.subtitle
.as_ref()
.is_some_and(|subtitle| !subtitle.trim().is_empty())
}
}
#[derive(Clone, Serialize, Deserialize, Debug, Type)]
#[serde(rename_all = "camelCase")]
pub struct FriendUserStatusPayload {

View File

@@ -122,6 +122,10 @@ pub fn on_friend_user_status(payload: Payload, _socket: RawClient) {
if let Ok(data) =
utils::extract_and_parse::<FriendUserStatusPayload>(payload, "friend-user-status")
{
if !data.status.has_presence_content() {
return;
}
emitter::emit_to_frontend_typed(&FriendUserStatusChanged(data));
}
}

View File

@@ -1,6 +1,6 @@
use once_cell::sync::Lazy;
use tauri_specta::Event as _;
use tauri::async_runtime::{self, JoinHandle};
use tauri_specta::Event as _;
use tokio::sync::Mutex;
use tokio::time::Duration;
use tracing::warn;
@@ -24,6 +24,10 @@ pub async fn report_user_status(status: UserStatusPayload) {
handle.abort();
}
if !status.has_presence_content() {
return;
}
if let Err(e) = UserStatusChanged(status.clone()).emit(crate::get_app_handle()) {
warn!("Failed to emit user-status-changed event: {e}");
}

View File

@@ -13,13 +13,6 @@ export const { start: startUserStatus, stop: stopUserStatus } =
await events.friendUserStatusChanged.listen((event) => {
const { userId, status } = event.payload;
const hasValidName =
(typeof status.presenceStatus.title === "string" &&
status.presenceStatus.title.trim() !== "") ||
(typeof status.presenceStatus.subtitle === "string" &&
status.presenceStatus.subtitle.trim() !== "");
if (!hasValidName) return;
friendsPresenceStates.update((current) => ({
...current,
[userId]: status,