Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projects integration #2057

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/api/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import session from "./session";
import playback from "./playback";
import did from "./did";
import room from "./room";
import project from "./project";

// Annoying but necessary to get the routing correct
export default {
Expand Down Expand Up @@ -53,4 +54,5 @@ export default {
did,
room,
clip,
project,
};
26 changes: 26 additions & 0 deletions packages/api/src/controllers/project.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) => {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
res.status(200);
res.json({});
});

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

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.project.create({
id: id,
name: "foo",
userId: req.user.id,
createdAt: Date.now(),
});
res.status(201).end();
});

export default app;
14 changes: 14 additions & 0 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ app.get("/", authorizer({}), async (req, res) => {
filters,
userId,
count,
projectId,
} = toStringValues(req.query);
if (isNaN(parseInt(limit))) {
limit = undefined;
Expand All @@ -396,6 +397,8 @@ app.get("/", authorizer({}), async (req, res) => {
userId = req.user.id;
}

console.log(`DEBUG: req.query: ${JSON.stringify(req.query)}`);

const query = parseFilters(fieldsMap, filters);
if (!all || all === "false" || !req.user.admin) {
query.push(sql`stream.data->>'deleted' IS NULL`);
Expand All @@ -416,6 +419,11 @@ app.get("/", authorizer({}), async (req, res) => {
if (userId) {
query.push(sql`stream.data->>'userId' = ${userId}`);
}
if (projectId) {
query.push(sql`stream.data->>'projectId' = ${projectId}`);
} else {
query.push(sql`stream.data->>'projectId' IS NULL`);
}

if (!order) {
order = "lastSeen-true,createdAt-true";
Expand Down Expand Up @@ -1940,6 +1948,12 @@ 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 ||
(!req.user.admin && (stream.deleted || stream.userId !== req.user.id))
Expand Down
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 @@ -334,6 +334,25 @@ components:
type: string
url:
$ref: "#/components/schemas/multistream-target/properties/url"
project:
type: object
required:
- name
additionalProperties: false
properties:
id:
type: string
readOnly: true
example: de7818e7-610a-4057-8f6f-b785dc1e6f88
name:
type: string
example: test_project
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 @@ -644,6 +644,17 @@ components:
type: string
probability:
type: number
project:
table: project
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
3 changes: 3 additions & 0 deletions packages/api/src/store/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Attestation,
JwtRefreshToken,
WebhookLog,
Project,
} from "../schema/types";
import BaseTable, { TableOptions } from "./table";
import StreamTable from "./stream-table";
Expand Down Expand Up @@ -65,6 +66,7 @@ export class DB {
region: Table<Region>;
session: SessionTable;
room: Table<Room>;
project: Table<Project>;

postgresUrl: string;
replicaUrl: string;
Expand Down Expand Up @@ -175,6 +177,7 @@ 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"] });

const tables = Object.entries(schema.components.schemas).filter(
([name, schema]) => "table" in schema && schema.table
Expand Down