Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/broadcast/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ pub mod utils;
pub mod errors;
pub mod router;
pub mod cache;

pub mod welcome;
21 changes: 15 additions & 6 deletions src/rooms/room_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ impl RoomService {

pub async fn create_room(state: Arc<AppState>, client_id: Uuid, new_room: NewRoom) -> Result<ChatRoomDto, AppError> {
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 {
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -159,11 +162,17 @@ impl RoomService {
}

pub async fn invite_to_room(state: Arc<AppState>, 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()))
};
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/user_relationship/user_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl UserService {
}
}
state.user_repository.delete_relationship_state(&mut tx, relationship).await?;
tx.commit().await?;
Ok(())
}

Expand Down
Loading