summaryrefslogtreecommitdiff
path: root/src/api/chat.rs
diff options
context:
space:
mode:
authorTyler Murphy <=>2023-08-21 23:19:53 -0400
committerTyler Murphy <=>2023-08-21 23:19:53 -0400
commitc4c26f42b6b06dbc75578314a483db66bf7ddb4c (patch)
treefba137f6fe2e8470b0abf6d3c139fa9a4fc9b4e8 /src/api/chat.rs
parentwebsocket (diff)
downloadxssbook-c4c26f42b6b06dbc75578314a483db66bf7ddb4c.tar.gz
xssbook-c4c26f42b6b06dbc75578314a483db66bf7ddb4c.tar.bz2
xssbook-c4c26f42b6b06dbc75578314a483db66bf7ddb4c.zip
dms
Diffstat (limited to 'src/api/chat.rs')
-rw-r--r--src/api/chat.rs17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/api/chat.rs b/src/api/chat.rs
index 1e56c3e..02bdfbd 100644
--- a/src/api/chat.rs
+++ b/src/api/chat.rs
@@ -6,7 +6,7 @@ use tokio::sync::{Mutex, mpsc::{Sender, self}};
use crate::{
public::docs::{EndpointDocumentation, EndpointMethod},
types::{
- extract::{AuthorizedUser, Check, CheckResult, Database, Json},
+ extract::{AuthorizedUser, Check, CheckResult, Database, Json, Log},
http::ResponseCode,
chat::{ChatRoom, ChatEvent}, user::User,
},
@@ -75,7 +75,8 @@ pub const CHAT_LIST: EndpointDocumentation = EndpointDocumentation {
async fn list (
AuthorizedUser(user): AuthorizedUser,
- Database(db): Database
+ Database(db): Database,
+ _: Log
) -> Response {
let Ok(rooms) = ChatRoom::from_user_id(&db, user.user_id) else {
return ResponseCode::InternalServerError.text("Failed to retrieve rooms")
@@ -137,7 +138,7 @@ async fn create (
for user in &room.users {
send_event(ChatEvent::Add {
user_id: *user,
- room_id: room.room_id
+ room: room.clone()
}, &room).await;
}
@@ -191,10 +192,14 @@ async fn add (
return ResponseCode::BadRequest.text("User does not exist")
};
- let Ok(room) = ChatRoom::from_user_and_room_id(&db, user.user_id, body.room_id) else {
+ let Ok(mut room) = ChatRoom::from_user_and_room_id(&db, user.user_id, body.room_id) else {
return ResponseCode::BadRequest.text("Room doesnt exist or you are not in it")
};
+ if room.users.contains(&to_add.user_id) {
+ return ResponseCode::BadRequest.text("User is already in the room")
+ }
+
let Ok(success) = room.add_user(&db, to_add.user_id) else {
return ResponseCode::InternalServerError.text("Failed to add user")
};
@@ -202,10 +207,12 @@ async fn add (
if !success {
return ResponseCode::BadRequest.text("User is already in the room")
}
+
+ room.users.push(to_add.user_id);
send_event(ChatEvent::Add {
user_id: to_add.user_id,
- room_id: room.room_id
+ room: room.clone()
}, &room).await;
ResponseCode::Success.text("Successfully added user")