Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Fix inserting recording for the second RTC (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Apr 16, 2021
1 parent 6b50b3b commit b61d33c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/app/endpoint/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,20 @@ impl RequestHandler for ConnectHandler {
};

// Create recording if a writer connects for the first time.
if payload.intent == ConnectIntent::Write && room.backend_id().is_none() {
if payload.intent == ConnectIntent::Write {
conn.transaction::<_, diesel::result::Error, _>(|| {
db::room::UpdateQuery::new(room.id())
.backend_id(Some(backend.id()))
.execute(&conn)?;
if room.backend_id().is_none() {
db::room::UpdateQuery::new(room.id())
.backend_id(Some(backend.id()))
.execute(&conn)?;
}

let recording = db::recording::FindQuery::new(payload.id).execute(&conn)?;

if recording.is_none() {
db::recording::InsertQuery::new(payload.id).execute(&conn)?;
}

db::recording::InsertQuery::new(payload.id).execute(&conn)?;
Ok(())
})?;
}
Expand Down
27 changes: 23 additions & 4 deletions src/db/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ impl Object {

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub(crate) struct FindQuery {
rtc_id: Uuid,
}

impl FindQuery {
pub(crate) fn new(rtc_id: Uuid) -> Self {
Self { rtc_id }
}

pub(crate) fn execute(self, conn: &PgConnection) -> Result<Option<Object>, Error> {
use diesel::prelude::*;

recording::table
.filter(recording::rtc_id.eq(self.rtc_id))
.get_result(conn)
.optional()
}
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Insertable)]
#[table_name = "recording"]
pub(crate) struct InsertQuery {
Expand All @@ -96,10 +118,7 @@ impl InsertQuery {
use crate::schema::recording::dsl::recording;
use diesel::RunQueryDsl;

diesel::insert_into(recording)
.values(self)
.on_conflict_do_nothing()
.get_result(conn)
diesel::insert_into(recording).values(self).get_result(conn)
}
}

Expand Down

0 comments on commit b61d33c

Please sign in to comment.