diff --git a/src/broadcast/notification.rs b/src/broadcast/notification.rs index f5663fe..4b54ca9 100644 --- a/src/broadcast/notification.rs +++ b/src/broadcast/notification.rs @@ -38,7 +38,8 @@ pub enum NotificationEvent { /** * Sending this event to a newly invited user */ - NewRoom {room: ChatRoomDto }, + #[serde(rename_all = "camelCase")] + NewRoom {room: ChatRoomDto, created_by: User }, /** * Sending this event to a user who has left a room diff --git a/src/lib.rs b/src/lib.rs index f41afc9..9b1ff93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,5 +12,4 @@ pub mod utils; pub mod errors; pub mod router; pub mod cache; - pub mod welcome; \ No newline at end of file diff --git a/src/rooms/room_service.rs b/src/rooms/room_service.rs index ad117f1..e5b1430 100644 --- a/src/rooms/room_service.rs +++ b/src/rooms/room_service.rs @@ -85,6 +85,9 @@ impl RoomService { pub async fn create_room(state: Arc, client_id: Uuid, new_room: NewRoom) -> Result { let room_entity = state.room_repository.insert_room(new_room.clone()).await?; + let creator_entity = state.user_repository.find_user_by_id(&client_id).await?.ok_or_else(|| { + AppError::NotFound("UserID not found.".to_string()) + })?; let users = new_room.invited_users; if room_entity.room_type == RoomType::Single { @@ -104,12 +107,12 @@ impl RoomService { let broadcast = BroadcastChannel::get(); broadcast.send_event(Notification { - body: crate::broadcast::NotificationEvent::NewRoom {room: participator_room.to_dto()}, + body: crate::broadcast::NotificationEvent::NewRoom {room: participator_room.to_dto(), created_by: creator_entity.clone()}, created_at: Utc::now() }, other_user).await; broadcast.send_event(Notification { - body: crate::broadcast::NotificationEvent::NewRoom {room: creator_room.to_dto()}, + body: crate::broadcast::NotificationEvent::NewRoom {room: creator_room.to_dto(), created_by: creator_entity}, created_at: Utc::now() }, &client_id).await; @@ -122,7 +125,7 @@ impl RoomService { BroadcastChannel::get().send_event_to_all( users, Notification { - body: crate::broadcast::NotificationEvent::NewRoom {room: room_dto.clone()}, + body: crate::broadcast::NotificationEvent::NewRoom {room: room_dto.clone(), created_by: creator_entity.clone()}, created_at: Utc::now() } ).await; @@ -159,11 +162,17 @@ impl RoomService { } pub async fn invite_to_room(state: Arc, client_id: Uuid, room_id: Uuid, user_id: Uuid) -> Result<(), AppError> { - let (room, users) = tokio::try_join!( //executing 2 queries async + let (room, users, creator) = tokio::try_join!( //executing 3 queries async state.room_repository.select_room(&room_id), - state.room_repository.select_joined_user_in_room(&room_id) + state.room_repository.select_joined_user_in_room(&room_id), + state.user_repository.find_user_by_id(&client_id) )?; + let creator_entity = creator.ok_or_else(|| { + AppError::NotFound("UserID not found.".to_string()) + })?; + + if room.room_type == RoomType::Single { return Err(AppError::ValidationError("Private rooms doesn't allow invites!.".to_string())) }; @@ -204,7 +213,7 @@ impl RoomService { BroadcastChannel::get().send_event( Notification { - body: crate::broadcast::NotificationEvent::NewRoom {room: room_for_user.to_dto()}, + body: crate::broadcast::NotificationEvent::NewRoom {room: room_for_user.to_dto(), created_by: creator_entity}, created_at: Utc::now() }, &user.id diff --git a/src/user_relationship/user_service.rs b/src/user_relationship/user_service.rs index 86fb6c1..f7f7513 100644 --- a/src/user_relationship/user_service.rs +++ b/src/user_relationship/user_service.rs @@ -212,6 +212,7 @@ impl UserService { } } state.user_repository.delete_relationship_state(&mut tx, relationship).await?; + tx.commit().await?; Ok(()) }