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

Commit

Permalink
Add tags to rooms (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Sep 28, 2020
1 parent 0326e1d commit 87590ba
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/src/api/room.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ time | [int, int] | _required_ | Opening and closing timestamps in seconds
created_at | int | _required_ | Room creation timestamp in seconds.
backend | string | _required_ | Room backend, either `janus` or `none`.
reserve | int | _optional_ | The number of slots for agents reserved on the backend.
tags | json | {} | Arbitrary tags object associated with the room.
1 change: 1 addition & 0 deletions docs/src/api/room/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ time | [i64, i64) | _required_ | A [lt, rt) range of unix time (seconds) or
audience | String | _required_ | The room audience.
backend | String | none | The room backend. Available values: janus, none.
reserve | i32 | _optional_ | The number of slots for subscribers to reserve on the server.
tags | json | {} | Arbitrary tags object associated with the room.


## Unicast response
Expand Down
1 change: 1 addition & 0 deletions docs/src/api/room/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ time | [i64, i64) | _optional_ | A [lt, rt) range of unix time (seconds) or
audience | String | _optional_ | The room audience.
backend | String | _optional_ | The room backend. Available values: janus, none.
reserve | i32 | _optional_ | The number of slots for subscribers to reserve on the server.
tags | json | {} | Arbitrary tags object associated with the room.


## Unicast response
Expand Down
1 change: 1 addition & 0 deletions migrations/2020-09-24-104539_add_tags_to_room/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE room DROP COLUMN tags;
1 change: 1 addition & 0 deletions migrations/2020-09-24-104539_add_tags_to_room/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE room ADD COLUMN tags JSON NOT NULL DEFAULT '{}'::JSON;
21 changes: 19 additions & 2 deletions src/app/endpoint/room.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::ops::Bound;

use async_std::stream;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde_derive::{Deserialize, Serialize};
use std::ops::Bound;
use serde_json::Value as JsonValue;
use svc_agent::mqtt::{
IncomingRequestProperties, IntoPublishableMessage, OutgoingRequest, ResponseStatus,
ShortTermTimingProperties,
Expand Down Expand Up @@ -43,6 +45,7 @@ pub(crate) struct CreateRequest {
#[serde(default = "CreateRequest::default_backend")]
backend: db::room::RoomBackend,
reserve: Option<i32>,
tags: Option<JsonValue>,
}

impl CreateRequest {
Expand Down Expand Up @@ -79,6 +82,10 @@ impl RequestHandler for CreateHandler {
q = q.reserve(reserve);
}

if let Some(ref tags) = payload.tags {
q = q.tags(tags);
}

let conn = context.db().get()?;
q.execute(&conn)?
};
Expand Down Expand Up @@ -442,6 +449,7 @@ mod test {
use std::ops::Bound;

use chrono::{SubsecRound, Utc};
use serde_json::json;

use crate::db::room::Object as Room;
use crate::test_helpers::prelude::*;
Expand All @@ -466,6 +474,7 @@ mod test {
audience: USR_AUDIENCE.to_owned(),
backend: db::room::RoomBackend::Janus,
reserve: Some(123),
tags: Some(json!({ "foo": "bar" })),
};

let messages = handle_request::<CreateHandler>(&context, &agent, payload)
Expand All @@ -479,6 +488,7 @@ mod test {
assert_eq!(room.time(), &time);
assert_eq!(room.backend(), db::room::RoomBackend::Janus);
assert_eq!(room.reserve(), Some(123));
assert_eq!(room.tags(), &json!({ "foo": "bar" }));

// Assert notification.
let (room, evp, topic) = find_event::<Room>(messages.as_slice());
Expand All @@ -488,6 +498,7 @@ mod test {
assert_eq!(room.time(), &time);
assert_eq!(room.backend(), db::room::RoomBackend::Janus);
assert_eq!(room.reserve(), Some(123));
assert_eq!(room.tags(), &json!({ "foo": "bar" }));
});
}

Expand All @@ -503,6 +514,7 @@ mod test {
audience: USR_AUDIENCE.to_owned(),
backend: db::room::RoomBackend::Janus,
reserve: None,
tags: None,
};

let err = handle_request::<CreateHandler>(&context, &agent, payload)
Expand Down Expand Up @@ -604,6 +616,7 @@ mod test {
use std::ops::Bound;

use chrono::{Duration, SubsecRound, Utc};
use serde_json::json;

use crate::db::room::Object as Room;
use crate::test_helpers::prelude::*;
Expand Down Expand Up @@ -647,7 +660,10 @@ mod test {
Bound::Excluded(now + Duration::hours(3)),
);

let payload = UpdateRequest::new(room.id()).time(time).reserve(Some(123));
let payload = UpdateRequest::new(room.id())
.time(time)
.reserve(Some(123))
.tags(json!({"foo": "bar"}));

let messages = handle_request::<UpdateHandler>(&context, &agent, payload)
.await
Expand All @@ -661,6 +677,7 @@ mod test {
assert_eq!(resp_room.time(), &time);
assert_eq!(resp_room.backend(), db::room::RoomBackend::Janus);
assert_eq!(resp_room.reserve(), Some(123));
assert_eq!(resp_room.tags(), &json!({"foo": "bar"}));
});
}

Expand Down
28 changes: 28 additions & 0 deletions src/db/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
use diesel::pg::PgConnection;
use diesel::result::Error;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use uuid::Uuid;

use crate::db::janus_backend::Object as JanusBackend;
Expand Down Expand Up @@ -46,6 +47,7 @@ type AllColumns = (
room::created_at,
room::backend,
room::reserve,
room::tags,
);

const ALL_COLUMNS: AllColumns = (
Expand All @@ -55,6 +57,7 @@ const ALL_COLUMNS: AllColumns = (
room::created_at,
room::backend,
room::reserve,
room::tags,
);

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -89,6 +92,7 @@ pub(crate) struct Object {
backend: RoomBackend,
#[serde(skip_serializing_if = "Option::is_none")]
reserve: Option<i32>,
tags: JsonValue,
}

impl Object {
Expand All @@ -113,6 +117,11 @@ impl Object {
pub(crate) fn reserve(&self) -> Option<i32> {
self.reserve
}

#[cfg(test)]
pub(crate) fn tags(&self) -> &JsonValue {
&self.tags
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -221,6 +230,7 @@ pub(crate) struct InsertQuery<'a> {
audience: &'a str,
backend: RoomBackend,
reserve: Option<i32>,
tags: Option<&'a JsonValue>,
}

impl<'a> InsertQuery<'a> {
Expand All @@ -230,6 +240,7 @@ impl<'a> InsertQuery<'a> {
audience,
backend,
reserve: None,
tags: None,
}
}

Expand All @@ -240,6 +251,13 @@ impl<'a> InsertQuery<'a> {
}
}

pub(crate) fn tags(self, value: &'a JsonValue) -> Self {
Self {
tags: Some(value),
..self
}
}

pub(crate) fn execute(&self, conn: &PgConnection) -> Result<Object, Error> {
use crate::schema::room::dsl::room;
use diesel::RunQueryDsl;
Expand Down Expand Up @@ -278,6 +296,7 @@ pub(crate) struct UpdateQuery {
audience: Option<String>,
backend: Option<RoomBackend>,
reserve: Option<Option<i32>>,
tags: Option<JsonValue>,
}

impl UpdateQuery {
Expand All @@ -289,6 +308,7 @@ impl UpdateQuery {
audience: None,
backend: None,
reserve: None,
tags: None,
}
}

Expand All @@ -312,6 +332,14 @@ impl UpdateQuery {
}
}

#[cfg(test)]
pub(crate) fn tags(self, value: JsonValue) -> Self {
Self {
tags: Some(value),
..self
}
}

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

Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ table! {
created_at -> Timestamptz,
backend -> Room_backend,
reserve -> Nullable<Int4>,
tags -> Json,
}
}

Expand Down

0 comments on commit 87590ba

Please sign in to comment.