This commit is contained in:
parent
d1a68c635c
commit
a4bb3cfcca
7 changed files with 556 additions and 364 deletions
30
src/chat.rs
30
src/chat.rs
|
|
@ -23,6 +23,10 @@ pub enum ServerEvent {
|
|||
other_user_id: Uuid,
|
||||
message: serde_json::Value,
|
||||
},
|
||||
UserPresence {
|
||||
user_id: Uuid,
|
||||
online: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -43,6 +47,18 @@ impl ChatHub {
|
|||
clients.remove(&user_id);
|
||||
}
|
||||
|
||||
pub async fn get_online_users(&self) -> Vec<Uuid> {
|
||||
let clients = self.clients.read().await;
|
||||
clients.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub async fn broadcast_all(&self, event: ServerEvent) {
|
||||
let clients = self.clients.read().await;
|
||||
for tx in clients.values() {
|
||||
let _ = tx.send(event.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn broadcast_to_user(&self, user_id: Uuid, event: ServerEvent) {
|
||||
let clients = self.clients.read().await;
|
||||
if let Some(tx) = clients.get(&user_id) {
|
||||
|
|
@ -65,6 +81,13 @@ pub async fn handle_socket(state: AppState, socket: WebSocket, user_id: Uuid) {
|
|||
let (tx, mut rx) = mpsc::unbounded_channel::<ServerEvent>();
|
||||
|
||||
state.chat.add_client(user_id, tx).await;
|
||||
state
|
||||
.chat
|
||||
.broadcast_all(ServerEvent::UserPresence {
|
||||
user_id,
|
||||
online: true,
|
||||
})
|
||||
.await;
|
||||
|
||||
let send_task = tokio::spawn(async move {
|
||||
while let Some(event) = rx.recv().await {
|
||||
|
|
@ -85,4 +108,11 @@ pub async fn handle_socket(state: AppState, socket: WebSocket, user_id: Uuid) {
|
|||
|
||||
send_task.abort();
|
||||
state.chat.remove_client(user_id).await;
|
||||
state
|
||||
.chat
|
||||
.broadcast_all(ServerEvent::UserPresence {
|
||||
user_id,
|
||||
online: false,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ pub fn routes() -> Router<AppState> {
|
|||
"/dms/{other_user_id}/messages",
|
||||
get(list_dm_messages).post(send_dm_message),
|
||||
)
|
||||
.route("/presence", get(presence_list))
|
||||
.route("/rtc-config", get(rtc_config))
|
||||
.route("/guilds", get(list_guilds).post(create_guild))
|
||||
.route("/guilds/{guild_id}/members", get(list_guild_members))
|
||||
|
|
@ -293,6 +294,14 @@ async fn me(State(state): State<AppState>, user: AuthUser) -> Result<impl IntoRe
|
|||
Ok(Json(me))
|
||||
}
|
||||
|
||||
async fn presence_list(
|
||||
State(state): State<AppState>,
|
||||
_user: AuthUser,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let users = state.chat.get_online_users().await;
|
||||
Ok(Json(users))
|
||||
}
|
||||
|
||||
async fn list_dm_conversations(
|
||||
State(state): State<AppState>,
|
||||
user: AuthUser,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue