Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/webapp/drizzle/0001_type_hardening.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE "chatrooms"
ALTER COLUMN "is_private" TYPE boolean
USING CASE
WHEN "is_private" IN ('1', 'true', 't', 'yes', 'y', 'on') THEN true
ELSE false
END,
ALTER COLUMN "is_private" SET DEFAULT false,
ALTER COLUMN "is_private" SET NOT NULL;

--> statement-breakpoint
ALTER TABLE "chatroom_invites"
ALTER COLUMN "max_uses" TYPE integer
USING NULLIF(regexp_replace("max_uses", '[^0-9-]', '', 'g'), '')::integer;
7 changes: 7 additions & 0 deletions packages/webapp/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1751891704768,
"tag": "0000_initial_schema",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1771172700000,
"tag": "0001_type_hardening",
"breakpoints": true
}
]
}
23 changes: 14 additions & 9 deletions packages/webapp/src/app/api/chatrooms/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { NextRequest, NextResponse } from "next/server";
import { db } from "@/db/client";
import { chatrooms, chatroom_members, users, messages } from "@/db/schema";
import { auth } from "@clerk/nextjs/server";
import { eq, inArray, count } from "drizzle-orm";
import { eq, count } from "drizzle-orm";
import { z } from "zod";

// TODO: Import db, schema, and Clerk auth utilities
const createChatroomSchema = z.object({
name: z.string().trim().min(1, "Chatroom name is required").max(80),
isPrivate: z.boolean().optional().default(false),
});

export async function GET(req: NextRequest) {
try {
Expand Down Expand Up @@ -81,15 +85,16 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

const { name, isPrivate = false } = await req.json();

if (!name || name.trim().length === 0) {
const parseResult = createChatroomSchema.safeParse(await req.json());
if (!parseResult.success) {
return NextResponse.json(
{ error: "Chatroom name is required" },
{ error: parseResult.error.issues[0]?.message || "Invalid request" },
{ status: 400 }
);
}

const { name, isPrivate } = parseResult.data;

// Find the user's internal database ID
const user = await db
.select()
Expand All @@ -104,9 +109,9 @@ export async function POST(req: NextRequest) {
const newChatroom = await db
.insert(chatrooms)
.values({
name: name.trim(),
isPrivate: isPrivate ? "1" : "0", // Convert boolean to string
createdBy: user[0].id, // UUID
name,
isPrivate,
createdBy: user[0].id,
})
.returning();

Expand Down
3 changes: 1 addition & 2 deletions packages/webapp/src/app/api/invite/[code]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ export async function POST(

// Check maxUses if set
if (invite[0].maxUses && invite[0].usedBy) {
const maxUsesNum = parseInt(invite[0].maxUses);
if (invite[0].usedBy.length >= maxUsesNum) {
if (invite[0].usedBy.length >= invite[0].maxUses) {
return NextResponse.json(
{ error: "This invite link has reached its usage limit" },
{ status: 400 }
Expand Down
5 changes: 3 additions & 2 deletions packages/webapp/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
uniqueIndex,
uuid,
jsonb,
integer,
AnyPgColumn,
} from "drizzle-orm/pg-core";

Expand All @@ -23,7 +24,7 @@ export const users = pgTable("users", {
export const chatrooms = pgTable("chatrooms", {
id: uuid("id").defaultRandom().primaryKey(),
name: text("name").notNull(),
isPrivate: text("is_private").default("0"), // Changed from integer to text for consistency
isPrivate: boolean("is_private").default(false).notNull(),
createdBy: uuid("created_by")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
Expand Down Expand Up @@ -96,5 +97,5 @@ export const chatroom_invites = pgTable("chatroom_invites", {
createdAt: timestamp("created_at").defaultNow().notNull(),
isActive: boolean("is_active").default(true).notNull(), // Can be deactivated manually
usedBy: jsonb("used_by").default([]).$type<string[]>(), // Array of user IDs who used this invite
maxUses: text("max_uses"), // Optional limit on how many times it can be used (null = unlimited)
maxUses: integer("max_uses"), // Optional limit on how many times it can be used (null = unlimited)
});
4 changes: 2 additions & 2 deletions packages/webapp/src/lib/usePartySocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface SettingsUpdateMessage {
};
timestamp: number;
updatedBy: {
id: number;
id: string;
displayName: string;
};
roomId: string;
Expand All @@ -47,7 +47,7 @@ export interface SettingsUpdateMessage {
export interface MemberEventMessage {
type: "member-joined" | "member-removed";
member: {
id: number;
id: string;
name: string;
role: string;
};
Expand Down