Start moving Store state into the database

This commit is contained in:
Antonio Scandurra 2022-11-11 12:06:43 +01:00
parent 28aa1567ce
commit 6871bbbc71
11 changed files with 447 additions and 337 deletions

View file

@ -2,7 +2,7 @@ mod store;
use crate::{
auth,
db::{self, ProjectId, User, UserId},
db::{self, ProjectId, RoomId, User, UserId},
AppState, Result,
};
use anyhow::anyhow;
@ -486,7 +486,7 @@ impl Server {
for project_id in projects_to_unshare {
self.app_state
.db
.unregister_project(project_id)
.unshare_project(project_id)
.await
.trace_err();
}
@ -559,11 +559,11 @@ impl Server {
request: Message<proto::CreateRoom>,
response: Response<proto::CreateRoom>,
) -> Result<()> {
let room;
{
let mut store = self.store().await;
room = store.create_room(request.sender_connection_id)?.clone();
}
let room = self
.app_state
.db
.create_room(request.sender_user_id, request.sender_connection_id)
.await?;
let live_kit_connection_info =
if let Some(live_kit) = self.app_state.live_kit_client.as_ref() {
@ -710,8 +710,9 @@ impl Server {
request: Message<proto::Call>,
response: Response<proto::Call>,
) -> Result<()> {
let caller_user_id = request.sender_user_id;
let recipient_user_id = UserId::from_proto(request.payload.recipient_user_id);
let room_id = RoomId::from_proto(request.payload.room_id);
let calling_user_id = request.sender_user_id;
let called_user_id = UserId::from_proto(request.payload.called_user_id);
let initial_project_id = request
.payload
.initial_project_id
@ -719,31 +720,44 @@ impl Server {
if !self
.app_state
.db
.has_contact(caller_user_id, recipient_user_id)
.has_contact(calling_user_id, called_user_id)
.await?
{
return Err(anyhow!("cannot call a user who isn't a contact"))?;
}
let room_id = request.payload.room_id;
let mut calls = {
let mut store = self.store().await;
let (room, recipient_connection_ids, incoming_call) = store.call(
room_id,
recipient_user_id,
initial_project_id,
request.sender_connection_id,
)?;
self.room_updated(room);
recipient_connection_ids
.into_iter()
.map(|recipient_connection_id| {
self.peer
.request(recipient_connection_id, incoming_call.clone())
})
.collect::<FuturesUnordered<_>>()
let room = self
.app_state
.db
.call(room_id, calling_user_id, called_user_id, initial_project_id)
.await?;
self.room_updated(&room);
self.update_user_contacts(called_user_id).await?;
let incoming_call = proto::IncomingCall {
room_id: room_id.to_proto(),
calling_user_id: calling_user_id.to_proto(),
participant_user_ids: room
.participants
.iter()
.map(|participant| participant.user_id)
.collect(),
initial_project: room.participants.iter().find_map(|participant| {
let initial_project_id = initial_project_id?.to_proto();
participant
.projects
.iter()
.find(|project| project.id == initial_project_id)
.cloned()
}),
};
self.update_user_contacts(recipient_user_id).await?;
let mut calls = self
.store()
.await
.connection_ids_for_user(called_user_id)
.map(|connection_id| self.peer.request(connection_id, incoming_call.clone()))
.collect::<FuturesUnordered<_>>();
while let Some(call_response) = calls.next().await {
match call_response.as_ref() {
@ -757,12 +771,13 @@ impl Server {
}
}
{
let mut store = self.store().await;
let room = store.call_failed(room_id, recipient_user_id)?;
self.room_updated(&room);
}
self.update_user_contacts(recipient_user_id).await?;
let room = self
.app_state
.db
.call_failed(room_id, called_user_id)
.await?;
self.room_updated(&room);
self.update_user_contacts(called_user_id).await?;
Err(anyhow!("failed to ring call recipient"))?
}
@ -772,7 +787,7 @@ impl Server {
request: Message<proto::CancelCall>,
response: Response<proto::CancelCall>,
) -> Result<()> {
let recipient_user_id = UserId::from_proto(request.payload.recipient_user_id);
let recipient_user_id = UserId::from_proto(request.payload.called_user_id);
{
let mut store = self.store().await;
let (room, recipient_connection_ids) = store.cancel_call(
@ -814,15 +829,17 @@ impl Server {
request: Message<proto::UpdateParticipantLocation>,
response: Response<proto::UpdateParticipantLocation>,
) -> Result<()> {
let room_id = request.payload.room_id;
let room_id = RoomId::from_proto(request.payload.room_id);
let location = request
.payload
.location
.ok_or_else(|| anyhow!("invalid location"))?;
let mut store = self.store().await;
let room =
store.update_participant_location(room_id, location, request.sender_connection_id)?;
self.room_updated(room);
let room = self
.app_state
.db
.update_room_participant_location(room_id, request.sender_user_id, location)
.await?;
self.room_updated(&room);
response.send(proto::Ack {})?;
Ok(())
}
@ -868,22 +885,20 @@ impl Server {
request: Message<proto::ShareProject>,
response: Response<proto::ShareProject>,
) -> Result<()> {
let project_id = self
let (project_id, room) = self
.app_state
.db
.register_project(request.sender_user_id)
.share_project(
request.sender_user_id,
request.sender_connection_id,
RoomId::from_proto(request.payload.room_id),
&request.payload.worktrees,
)
.await?;
let mut store = self.store().await;
let room = store.share_project(
request.payload.room_id,
project_id,
request.payload.worktrees,
request.sender_connection_id,
)?;
response.send(proto::ShareProjectResponse {
project_id: project_id.to_proto(),
})?;
self.room_updated(room);
self.room_updated(&room);
Ok(())
}