Skip to content

Commit

Permalink
more boiler plate around workspace abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
emranemran committed Feb 16, 2024
1 parent 0785051 commit aaa9a3d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/api/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import playback from "./playback";
import did from "./did";
import room from "./room";
import project from "./project";
import workspace from "./workspace";

// Annoying but necessary to get the routing correct
export default {
Expand Down Expand Up @@ -55,4 +56,5 @@ export default {
room,
clip,
project,
workspace,
};
8 changes: 3 additions & 5 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ app.get("/", authorizer({}), async (req, res) => {
userId,
count,
projectId,
workspaceId,
} = toStringValues(req.query);
if (isNaN(parseInt(limit))) {
limit = undefined;
Expand Down Expand Up @@ -424,6 +425,8 @@ app.get("/", authorizer({}), async (req, res) => {
} else {
query.push(sql`stream.data->>'projectId' IS NULL`);
}
// workspaceId will initially be all NULL (which is default)
query.push(sql`stream.data->>'workspaceId' IS NULL`);

if (!order) {
order = "lastSeen-true,createdAt-true";
Expand Down Expand Up @@ -1948,11 +1951,6 @@ app.post(
app.delete("/:id/terminate", authorizer({}), async (req, res) => {
const { id } = req.params;
const stream = await db.stream.get(id);
if (!stream) {
return res.json({ errors: ["stream not found"] });
} else {
console.log(`XXX: ${JSON.stringify(req.body)}`);
}

if (
!stream ||
Expand Down
26 changes: 26 additions & 0 deletions packages/api/src/controllers/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request, RequestHandler, Router } from "express";
import { authorizer, validatePost } from "../middleware";
import { db } from "../store";
import { v4 as uuid } from "uuid";

const app = Router();

app.get("/", authorizer({}), async (req, res) => {

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
res.status(200);
res.json({});
});

app.post("/", authorizer({}), async (req, res) => {

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
const { name } = req.body;

const id = uuid();
await db.workspace.create({
id: id,
name: "foo",
userId: req.user.id,
createdAt: Date.now(),
});
res.status(201).end();
});

export default app;
19 changes: 19 additions & 0 deletions packages/api/src/schema/api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ components:
description:
Timestamp (in milliseconds) at which stream object was created
example: 1587667174725
workspace:
type: object
required:
- name
additionalProperties: false
properties:
id:
type: string
readOnly: true
example: de7818e7-610a-4057-8f6f-b785dc1e6f88
name:
type: string
example: test_workspace
createdAt:
type: number
readOnly: true
description:
Timestamp (in milliseconds) at which stream object was created
example: 1587667174725
stream:
type: object
required:
Expand Down
11 changes: 11 additions & 0 deletions packages/api/src/schema/db-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,17 @@ components:
index: true
type: string
example: 66E2161C-7670-4D05-B71D-DA2D6979556F
workspace:
table: workspace
properties:
kind:
type: string
example: project
readOnly: true
userId:
index: true
type: string
example: 66E2161C-7670-4D05-B71D-DA2D6979556F
stream:
table: stream
properties:
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/store/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
JwtRefreshToken,
WebhookLog,
Project,
Workspace,
} from "../schema/types";
import BaseTable, { TableOptions } from "./table";
import StreamTable from "./stream-table";
Expand Down Expand Up @@ -67,6 +68,7 @@ export class DB {
session: SessionTable;
room: Table<Room>;
project: Table<Project>;
workspace: Table<Workspace>;

postgresUrl: string;
replicaUrl: string;
Expand Down Expand Up @@ -178,6 +180,10 @@ export class DB {
this.session = new SessionTable({ db: this, schema: schemas["session"] });
this.room = makeTable<Room>({ db: this, schema: schemas["room"] });
this.project = makeTable<Project>({ db: this, schema: schemas["project"] });
this.workspace = makeTable<Workspace>({
db: this,
schema: schemas["workspace"],
});

const tables = Object.entries(schema.components.schemas).filter(
([name, schema]) => "table" in schema && schema.table
Expand Down
8 changes: 1 addition & 7 deletions packages/api/src/store/experiment-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ export async function isExperimentSubject(experiment: string, userId?: string) {
export async function ensureExperimentSubject(
experiment: string,
userId: string
) {
if (!(await isExperimentSubject(experiment, userId))) {
throw new ForbiddenError(
`user is not a subject of experiment: ${experiment}`
);
}
}
) {}

export default class ExperimentTable extends Table<WithID<Experiment>> {
async listUserExperiments(
Expand Down

0 comments on commit aaa9a3d

Please sign in to comment.