mirror of
https://github.com/modrinth/code.git
synced 2026-08-28 10:34:53 +00:00
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:
@@ -7,12 +7,14 @@ use crate::database::models::user_item::DBUser;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::users::Role;
|
||||
use crate::models::v3::notifications::{
|
||||
NotificationBody, NotificationDeliveryStatus,
|
||||
Notification, NotificationBody, NotificationDeliveryStatus,
|
||||
};
|
||||
use crate::models::v3::pats::Scopes;
|
||||
use crate::queue::email::EmailQueue;
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::ApiError;
|
||||
use crate::routes::internal::statuses::broadcast_friends_message;
|
||||
use crate::sync::friends::RedisFriendsMessage;
|
||||
use crate::util::guards::external_notification_key_guard;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::web;
|
||||
@@ -58,12 +60,39 @@ pub async fn create(
|
||||
));
|
||||
}
|
||||
|
||||
NotificationBuilder { body }
|
||||
let notification_ids = NotificationBuilder { body }
|
||||
.insert_many(user_ids, &mut txn, &redis)
|
||||
.await?;
|
||||
|
||||
let notifications = DBNotification::get_many(¬ification_ids, &mut txn)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(Notification::from)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
for notification in notifications {
|
||||
let notification_id = notification.id;
|
||||
let to_user = notification.user_id;
|
||||
if let Err(error) = broadcast_friends_message(
|
||||
&redis,
|
||||
RedisFriendsMessage::Notification {
|
||||
to_user,
|
||||
notification,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
?error,
|
||||
?notification_id,
|
||||
?to_user,
|
||||
"failed to broadcast realtime notification"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Accepted().finish())
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@ use crate::auth::AuthenticationError;
|
||||
use crate::auth::validate::get_user_record_from_bearer_token;
|
||||
use crate::database::PgPool;
|
||||
use crate::database::models::friend_item::DBFriend;
|
||||
use crate::database::models::notification_item::DBNotification;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::notifications::{Notification, NotificationBody};
|
||||
use crate::models::pats::Scopes;
|
||||
use crate::models::users::User;
|
||||
use crate::queue::session::AuthQueue;
|
||||
@@ -42,6 +44,7 @@ struct LauncherHeartbeatInit {
|
||||
code: String,
|
||||
}
|
||||
|
||||
// TODO: Move launcher-specific tunnel traffic to a proper launcher websocket endpoint.
|
||||
#[get("launcher_socket")]
|
||||
pub async fn ws_init(
|
||||
req: HttpRequest,
|
||||
@@ -127,6 +130,26 @@ pub async fn ws_init(
|
||||
)?)
|
||||
.await;
|
||||
|
||||
let unread_server_invites = DBNotification::get_many_user_exposed_on_site(
|
||||
user_id.into(),
|
||||
&**pool,
|
||||
&redis,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|notification| {
|
||||
!notification.read
|
||||
&& matches!(
|
||||
¬ification.body,
|
||||
NotificationBody::ServerInvite { .. }
|
||||
)
|
||||
})
|
||||
.map(Notification::from);
|
||||
|
||||
for notification in unread_server_invites {
|
||||
let _ = session.text(serde_json::to_string(¬ification)?).await;
|
||||
}
|
||||
|
||||
let db = db.clone();
|
||||
let socket_id = db.next_socket_id.fetch_add(1, Ordering::Relaxed);
|
||||
db.sockets
|
||||
@@ -449,6 +472,25 @@ pub async fn send_message_to_user(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_notification_to_user(
|
||||
db: &ActiveSockets,
|
||||
user: UserId,
|
||||
notification: &Notification,
|
||||
) -> Result<(), crate::database::models::DatabaseError> {
|
||||
let message = serde_json::to_string(notification)?;
|
||||
|
||||
if let Some(socket_ids) = db.sockets_by_user_id.get(&user) {
|
||||
for socket_id in socket_ids.iter() {
|
||||
if let Some(socket) = db.sockets.get(&socket_id) {
|
||||
let mut socket = socket.socket.clone();
|
||||
let _ = socket.text(message.clone()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn close_socket(
|
||||
id: SocketId,
|
||||
pool: &PgPool,
|
||||
|
||||
Reference in New Issue
Block a user