forked from coracle/caravel
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7696b9e1d4 |
@@ -1,11 +0,0 @@
|
|||||||
CREATE INDEX IF NOT EXISTS idx_tenant_stripe_customer_id
|
|
||||||
ON tenant (stripe_customer_id);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_relay_tenant_id
|
|
||||||
ON relay (tenant, id);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_relay_tenant_status_plan
|
|
||||||
ON relay (tenant, status, plan);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_activity_resource_type_resource_id_created_at_id
|
|
||||||
ON activity (resource_type, resource_id, created_at DESC, id DESC);
|
|
||||||
+51
-107
@@ -251,16 +251,22 @@ impl Api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_relay(&self, mut relay: Relay) -> std::result::Result<Relay, RelayValidationError> {
|
fn prepare_relay(&self, mut relay: Relay) -> anyhow::Result<Relay> {
|
||||||
validate_subdomain_label(&relay.subdomain)?;
|
if !relay
|
||||||
|
.subdomain
|
||||||
|
.chars()
|
||||||
|
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
|
||||||
|
{
|
||||||
|
return Err(anyhow!("invalid-subdomain"));
|
||||||
|
}
|
||||||
|
|
||||||
let plan = Query::get_plan(&relay.plan).ok_or(RelayValidationError::InvalidPlan)?;
|
let plan = Query::get_plan(&relay.plan).ok_or_else(|| anyhow!("invalid-plan"))?;
|
||||||
|
|
||||||
if !plan.blossom && relay.blossom_enabled == 1 {
|
if !plan.blossom && relay.blossom_enabled == 1 {
|
||||||
return Err(RelayValidationError::PremiumFeature);
|
return Err(anyhow!("premium-feature"));
|
||||||
}
|
}
|
||||||
if !plan.livekit && relay.livekit_enabled == 1 {
|
if !plan.livekit && relay.livekit_enabled == 1 {
|
||||||
return Err(RelayValidationError::PremiumFeature);
|
return Err(anyhow!("premium-feature"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if relay.schema.is_empty() {
|
if relay.schema.is_empty() {
|
||||||
@@ -283,96 +289,6 @@ impl Api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const SUBDOMAIN_LABEL_MAX_LEN: usize = 63;
|
|
||||||
const RESERVED_SUBDOMAIN_LABELS: [&str; 2] = ["api", "admin"];
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
enum SubdomainValidationError {
|
|
||||||
Empty,
|
|
||||||
TooLong,
|
|
||||||
Reserved,
|
|
||||||
EdgeHyphen,
|
|
||||||
InvalidCharacters,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SubdomainValidationError {
|
|
||||||
fn code(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::Empty => "subdomain-empty",
|
|
||||||
Self::TooLong => "subdomain-too-long",
|
|
||||||
Self::Reserved => "subdomain-reserved",
|
|
||||||
Self::EdgeHyphen => "subdomain-invalid-hyphen",
|
|
||||||
Self::InvalidCharacters => "subdomain-invalid-characters",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn message(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::Empty => "subdomain is required",
|
|
||||||
Self::TooLong => "subdomain must be 63 characters or fewer",
|
|
||||||
Self::Reserved => "subdomain is reserved",
|
|
||||||
Self::EdgeHyphen => "subdomain cannot start or end with a hyphen",
|
|
||||||
Self::InvalidCharacters => {
|
|
||||||
"subdomain may only contain lowercase letters, numbers, and hyphens"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
enum RelayValidationError {
|
|
||||||
InvalidPlan,
|
|
||||||
PremiumFeature,
|
|
||||||
Subdomain(SubdomainValidationError),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RelayValidationError {
|
|
||||||
fn code(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::InvalidPlan => "invalid-plan",
|
|
||||||
Self::PremiumFeature => "premium-feature",
|
|
||||||
Self::Subdomain(reason) => reason.code(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn message(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::InvalidPlan => "plan not found",
|
|
||||||
Self::PremiumFeature => "feature requires a paid plan",
|
|
||||||
Self::Subdomain(reason) => reason.message(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SubdomainValidationError> for RelayValidationError {
|
|
||||||
fn from(value: SubdomainValidationError) -> Self {
|
|
||||||
Self::Subdomain(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validate_subdomain_label(subdomain: &str) -> std::result::Result<(), SubdomainValidationError> {
|
|
||||||
if subdomain.is_empty() {
|
|
||||||
return Err(SubdomainValidationError::Empty);
|
|
||||||
}
|
|
||||||
if subdomain.len() > SUBDOMAIN_LABEL_MAX_LEN {
|
|
||||||
return Err(SubdomainValidationError::TooLong);
|
|
||||||
}
|
|
||||||
if subdomain.starts_with('-') || subdomain.ends_with('-') {
|
|
||||||
return Err(SubdomainValidationError::EdgeHyphen);
|
|
||||||
}
|
|
||||||
if RESERVED_SUBDOMAIN_LABELS.contains(&subdomain) {
|
|
||||||
return Err(SubdomainValidationError::Reserved);
|
|
||||||
}
|
|
||||||
if !subdomain
|
|
||||||
.chars()
|
|
||||||
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
|
|
||||||
{
|
|
||||||
return Err(SubdomainValidationError::InvalidCharacters);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ok<T: Serialize>(status: StatusCode, data: T) -> Response {
|
fn ok<T: Serialize>(status: StatusCode, data: T) -> Response {
|
||||||
(status, Json(OkResponse { data, code: "ok" })).into_response()
|
(status, Json(OkResponse { data, code: "ok" })).into_response()
|
||||||
}
|
}
|
||||||
@@ -400,14 +316,6 @@ fn parse_bool_default(value: i64, default: i64) -> i64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn relay_validation_error_response(error: RelayValidationError) -> Response {
|
|
||||||
err(
|
|
||||||
StatusCode::UNPROCESSABLE_ENTITY,
|
|
||||||
error.code(),
|
|
||||||
error.message(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_unique_error(err: &anyhow::Error) -> Option<&'static str> {
|
fn map_unique_error(err: &anyhow::Error) -> Option<&'static str> {
|
||||||
let sqlx_err = err.downcast_ref::<sqlx::Error>()?;
|
let sqlx_err = err.downcast_ref::<sqlx::Error>()?;
|
||||||
let sqlx::Error::Database(db_err) = sqlx_err else {
|
let sqlx::Error::Database(db_err) = sqlx_err else {
|
||||||
@@ -700,9 +608,27 @@ async fn create_relay(
|
|||||||
};
|
};
|
||||||
|
|
||||||
relay = match state.api.prepare_relay(relay) {
|
relay = match state.api.prepare_relay(relay) {
|
||||||
|
Err(e) if e.to_string() == "invalid-plan" => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"invalid-plan",
|
||||||
|
"plan not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) if e.to_string() == "premium-feature" => {
|
||||||
return Ok(relay_validation_error_response(e));
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"premium-feature",
|
||||||
|
"feature requires a paid plan",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"invalid-relay",
|
||||||
|
"relay validation failed",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -786,9 +712,27 @@ async fn update_relay(
|
|||||||
}
|
}
|
||||||
|
|
||||||
relay = match state.api.prepare_relay(relay) {
|
relay = match state.api.prepare_relay(relay) {
|
||||||
|
Err(e) if e.to_string() == "invalid-plan" => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"invalid-plan",
|
||||||
|
"plan not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) if e.to_string() == "premium-feature" => {
|
||||||
return Ok(relay_validation_error_response(e));
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"premium-feature",
|
||||||
|
"feature requires a paid plan",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
"invalid-relay",
|
||||||
|
"relay validation failed",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { createEffect, createMemo, createSignal, For } from "solid-js"
|
import { createEffect, createMemo, createSignal, For } from "solid-js"
|
||||||
import type { Relay } from "@/lib/hooks"
|
import type { Relay } from "@/lib/hooks"
|
||||||
import { slugify } from "@/lib/slugify"
|
import { slugify } from "@/lib/slugify"
|
||||||
import { validateSubdomainLabel } from "@/lib/subdomain"
|
|
||||||
import { setToastMessage } from "@/components/Toast"
|
import { setToastMessage } from "@/components/Toast"
|
||||||
import { plans } from "@/lib/state"
|
import { plans } from "@/lib/state"
|
||||||
|
|
||||||
@@ -32,12 +31,6 @@ export default function RelayForm(props: RelayFormProps) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const subdomainError = validateSubdomainLabel(subdomain())
|
|
||||||
if (subdomainError) {
|
|
||||||
setToastMessage(subdomainError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setToastMessage("")
|
setToastMessage("")
|
||||||
setSubmitting(true)
|
setSubmitting(true)
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
const SUBDOMAIN_LABEL_MAX_LEN = 63
|
|
||||||
const RESERVED_SUBDOMAIN_LABELS = new Set(["api", "admin"])
|
|
||||||
|
|
||||||
export function validateSubdomainLabel(subdomain: string): string | null {
|
|
||||||
if (subdomain.length === 0) {
|
|
||||||
return "subdomain is required"
|
|
||||||
}
|
|
||||||
if (subdomain.length > SUBDOMAIN_LABEL_MAX_LEN) {
|
|
||||||
return "subdomain must be 63 characters or fewer"
|
|
||||||
}
|
|
||||||
if (subdomain.startsWith("-") || subdomain.endsWith("-")) {
|
|
||||||
return "subdomain cannot start or end with a hyphen"
|
|
||||||
}
|
|
||||||
if (RESERVED_SUBDOMAIN_LABELS.has(subdomain)) {
|
|
||||||
return "subdomain is reserved"
|
|
||||||
}
|
|
||||||
if (!/^[a-z0-9-]+$/.test(subdomain)) {
|
|
||||||
return "subdomain may only contain lowercase letters, numbers, and hyphens"
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { useNavigate, useParams } from "@solidjs/router"
|
import { useNavigate, useParams } from "@solidjs/router"
|
||||||
import { Show } from "solid-js"
|
import { Show } from "solid-js"
|
||||||
import RelayForm, { type RelayFormValues } from "@/components/RelayForm"
|
import RelayForm, { type RelayFormValues } from "@/components/RelayForm"
|
||||||
|
import { slugify } from "@/lib/slugify"
|
||||||
import BackLink from "@/components/BackLink"
|
import BackLink from "@/components/BackLink"
|
||||||
import PageContainer from "@/components/PageContainer"
|
import PageContainer from "@/components/PageContainer"
|
||||||
import ResourceState from "@/components/ResourceState"
|
import ResourceState from "@/components/ResourceState"
|
||||||
@@ -17,7 +18,7 @@ export default function RelayEdit(props: { basePath?: string; title?: string })
|
|||||||
|
|
||||||
async function handleSubmit(values: RelayFormValues) {
|
async function handleSubmit(values: RelayFormValues) {
|
||||||
await updateRelayById(relayId(), {
|
await updateRelayById(relayId(), {
|
||||||
subdomain: values.subdomain,
|
subdomain: slugify(values.subdomain),
|
||||||
info_name: values.info_name.trim(),
|
info_name: values.info_name.trim(),
|
||||||
info_icon: values.info_icon.trim(),
|
info_icon: values.info_icon.trim(),
|
||||||
info_description: values.info_description.trim(),
|
info_description: values.info_description.trim(),
|
||||||
|
|||||||
Reference in New Issue
Block a user