feat: add notifs onto friends ws temporarily (#6290)

* feat: add notifs onto friends ws temporarily

* fix: lint + styling

* fix: regressions
This commit is contained in:
Calum H.
2026-06-02 20:47:37 +01:00
committed by GitHub
parent 940a796ba5
commit 3c051f5b1d
14 changed files with 369 additions and 45 deletions
+15
View File
@@ -8,6 +8,7 @@ use crate::event::{
LoadingPayload, ProcessPayload, ProfilePayload, WarningPayload,
};
use futures::prelude::*;
use serde_json::Value;
#[cfg(feature = "tauri")]
use tauri::{Emitter, Manager};
use uuid::Uuid;
@@ -303,6 +304,20 @@ pub async fn emit_friend(payload: FriendPayload) -> crate::Result<()> {
Ok(())
}
#[allow(unused_variables)]
pub async fn emit_notification(payload: Value) -> crate::Result<()> {
#[cfg(feature = "tauri")]
{
let event_state = crate::EventState::get()?;
event_state
.app
.emit("notification", payload)
.map_err(EventError::from)?;
}
Ok(())
}
// loading_join! macro
// loading_join!(key: Option<&LoadingBarId>, total: f64, message: Option<&str>; task1, task2, task3...)
// This will submit a loading event with the given message for each task as they complete
+39 -7
View File
@@ -1,7 +1,7 @@
use crate::ErrorKind;
use crate::data::ModrinthCredentials;
use crate::event::FriendPayload;
use crate::event::emit::emit_friend;
use crate::event::emit::{emit_friend, emit_notification};
use crate::state::tunnel::InternalTunnelSocket;
use crate::state::{ProcessManager, Profile, TunnelSocket};
use crate::util::fetch::{FetchSemaphore, fetch_advanced, fetch_json};
@@ -22,6 +22,7 @@ use futures::{SinkExt, StreamExt};
use reqwest::Method;
use reqwest::header::HeaderValue;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::net::SocketAddr;
use std::ops::Deref;
use std::sync::Arc;
@@ -120,16 +121,34 @@ impl FriendsSocket {
Ok(msg) => {
let server_message = match msg {
Message::Text(text) => {
ServerToClientMessage::deserialize(
match ServerToClientMessage::deserialize(
Either::Left(&text),
)
.ok()
) {
Ok(message) => Some(message),
Err(_) => {
if let Ok(notification) =
serde_json::from_str::<Value>(&text)
{
let _ = Self::handle_notification(notification).await;
}
None
}
}
}
Message::Binary(bytes) => {
ServerToClientMessage::deserialize(
match ServerToClientMessage::deserialize(
Either::Right(&bytes),
)
.ok()
) {
Ok(message) => Some(message),
Err(_) => {
if let Ok(notification) =
serde_json::from_slice::<Value>(&bytes)
{
let _ = Self::handle_notification(notification).await;
}
None
}
}
}
Message::Ping(bytes) => {
if let Some(write) = write_handle
@@ -224,6 +243,19 @@ impl FriendsSocket {
Ok(())
}
async fn handle_notification(notification: Value) -> crate::Result<()> {
if notification
.get("body")
.and_then(|body| body.get("type"))
.and_then(Value::as_str)
.is_some()
{
emit_notification(notification).await?;
}
Ok(())
}
#[tracing::instrument(skip_all)]
pub async fn socket_loop() -> crate::Result<()> {
let state = crate::State::get().await?;