Skip to content
Draft
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
37 changes: 19 additions & 18 deletions apps/web/actions/videos/new-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,43 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { comments } from "@cap/database/schema";
import type { Video } from "@cap/web-domain";
import { comments, videos } from "@cap/database/schema";
import { Video } from "@cap/web-domain";
import { revalidatePath } from "next/cache";
import { createNotification } from "@/lib/Notification";
import { eq } from "drizzle-orm";

export async function newComment(data: {
export async function newComment({
content,
videoId,
type,
parentCommentId,
timestamp,
}: {
content: string;
videoId: Video.VideoId;
type: "text" | "emoji";
parentCommentId: string;
timestamp: number;
}) {
const user = await getCurrentUser();
if (!user) throw new Error("User not authenticated");

if (!user) {
throw new Error("User not authenticated");
}

const content = data.content;
const videoId = data.videoId;
const type = data.type;
const parentCommentId = data.parentCommentId;
const timestamp = data.timestamp;
const conditionalType = parentCommentId
? "reply"
: type === "emoji"
? "reaction"
: "comment";

if (!content || !videoId) {
throw new Error("Content and videoId are required");
}
const id = nanoId();
const [video] = await db()
.select({ orgId: videos.orgId })
.from(videos)
.where(eq(videos.id, videoId));
if (!content || !videoId) throw new Error("Content and videoId are required");
if (!video) throw new Error("Video not found");

const newComment = {
id: id,
id: nanoId(),
authorId: user.id,
type: type,
content: content,
Expand All @@ -56,7 +57,7 @@ export async function newComment(data: {
type: conditionalType,
videoId,
authorId: user.id,
comment: { id, content },
comment: { id: newComment.id, content },
parentCommentId,
});
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/(org)/dashboard/caps/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default async function CapsPage(props: {
domainVerified: organizations.domainVerified,
})
.from(organizations)
.where(eq(organizations.id, user.activeOrganizationId))
.where(eq(organizations.id, user.activeOrganizationId ?? sql`NULL`))
.limit(1);

let customDomain: string | null = null;
Expand Down Expand Up @@ -214,7 +214,7 @@ export default async function CapsPage(props: {
.from(folders)
.where(
and(
eq(folders.organizationId, user.activeOrganizationId),
eq(folders.organizationId, user.activeOrganizationId ?? sql`NULL`),
isNull(folders.parentId),
isNull(folders.spaceId),
),
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/(org)/dashboard/settings/organization/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { organizationMembers, organizations } from "@cap/database/schema";
import { and, eq } from "drizzle-orm";
import { and, eq, sql } from "drizzle-orm";
import type { Metadata } from "next";
import { redirect } from "next/navigation";
import { getDashboardData } from "../../dashboard-data";
Expand Down Expand Up @@ -31,7 +31,7 @@ export default async function OrganizationPage() {
.where(
and(
eq(organizationMembers.userId, user.id),
eq(organizations.id, user.activeOrganizationId),
eq(organizations.id, user.activeOrganizationId ?? sql`NULL`),
),
);

Expand Down
10 changes: 8 additions & 2 deletions apps/web/app/api/notifications/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export async function GET() {
.where(
and(
eq(notifications.recipientId, currentUser.id),
eq(notifications.orgId, currentUser.activeOrganizationId),
eq(
notifications.orgId,
currentUser.activeOrganizationId ?? sql`NULL`,
),
),
)
.orderBy(
Expand All @@ -72,7 +75,10 @@ export async function GET() {
.where(
and(
eq(notifications.recipientId, currentUser.id),
eq(notifications.orgId, currentUser.activeOrganizationId),
eq(
notifications.orgId,
currentUser.activeOrganizationId ?? sql`NULL`,
),
),
)
.groupBy(notifications.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Comment: React.FC<{

const handleDelete = () => {
if (window.confirm("Are you sure you want to delete this comment?")) {
onDelete(comment.id, comment.parentCommentId);
onDelete(comment.id, comment.parentCommentId ?? undefined);
}
};

Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/s/[videoId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ async function AuthorizedContent({
.from(comments)
.where(eq(comments.id, replyId))
.limit(1);
toplLevelCommentId = parentComment?.parentCommentId;
toplLevelCommentId = parentComment?.parentCommentId ?? undefined;
}

const commentToBringToTheTop = toplLevelCommentId ?? commentId;
Expand Down
3 changes: 1 addition & 2 deletions apps/web/lib/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
const [recipientUser] = await db()
.select({
preferences: users.preferences,
activeOrganizationId: users.activeOrganizationId,
})
.from(users)
.where(eq(users.id, recipientId))
Expand Down Expand Up @@ -120,9 +119,9 @@

const notificationId = nanoId();

await db().insert(notifications).values({

Check failure on line 122 in apps/web/lib/Notification.ts

View workflow job for this annotation

GitHub Actions / Typecheck

No overload matches this call.
id: notificationId,
orgId: recipientUser.activeOrganizationId,
orgId: undefined, // TODO: BRUHHHHH: recipientUser.activeOrganizationId,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dis needs fixing

recipientId,
type,
data,
Expand Down
7 changes: 6 additions & 1 deletion packages/database/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export const getCurrentUser = cache(
.from(users)
.where(eq(users.id, session.user.id));

return currentUser ?? null;
return currentUser
? {
...currentUser,
activeOrganizationId: currentUser?.activeOrganizationId ?? null,
}
: null;
},
);

Expand Down
38 changes: 9 additions & 29 deletions packages/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Folder, S3Bucket, Video } from "@cap/web-domain";
import {
boolean,
customType,
datetime,
float,
index,
Expand All @@ -11,7 +10,6 @@ import {
primaryKey,
text,
timestamp,
tinyint,
uniqueIndex,
varchar,
} from "drizzle-orm/mysql-core";
Expand All @@ -20,35 +18,17 @@ import { relations } from "drizzle-orm/relations";
import { nanoIdLength } from "./helpers.ts";
import type { VideoMetadata } from "./types/index.ts";

const nanoId = customType<{ data: string; notNull: true }>({
dataType() {
return `varchar(${nanoIdLength})`;
},
});

const nanoIdNullable = customType<{ data: string; notNull: false }>({
dataType() {
return `varchar(${nanoIdLength})`;
},
});

// Add a custom type for encrypted strings
const encryptedText = customType<{ data: string; notNull: true }>({
dataType() {
return "text";
},
});

const encryptedTextNullable = customType<{ data: string; notNull: false }>({
dataType() {
return "text";
},
});
const nanoId = (name: string) =>
varchar(name, { length: nanoIdLength }).notNull();
const nanoIdNullable = (name: string) =>
varchar(name, { length: nanoIdLength });
const encryptedText = (name: string) => text(name).notNull();
const encryptedTextNullable = (name: string) => text(name);

export const users = mysqlTable(
"users",
{
id: nanoId("id").notNull().primaryKey().unique(),
id: nanoId("id").primaryKey().unique(),
name: varchar("name", { length: 255 }),
lastName: varchar("lastName", { length: 255 }),
email: varchar("email", { length: 255 }).unique().notNull(),
Expand Down Expand Up @@ -83,7 +63,7 @@ export const users = mysqlTable(
};
} | null>()
.default(null),
activeOrganizationId: nanoId("activeOrganizationId"),
activeOrganizationId: nanoIdNullable("activeOrganizationId"),
created_at: timestamp("created_at").notNull().defaultNow(),
updated_at: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
onboarding_completed_at: timestamp("onboarding_completed_at"),
Expand Down Expand Up @@ -320,7 +300,7 @@ export const comments = mysqlTable(
videoId: nanoId("videoId").notNull().$type<Video.VideoId>(),
createdAt: timestamp("createdAt").notNull().defaultNow(),
updatedAt: timestamp("updatedAt").notNull().defaultNow().onUpdateNow(),
parentCommentId: nanoId("parentCommentId"),
parentCommentId: nanoIdNullable("parentCommentId"),
},
(table) => ({
videoIdIndex: index("video_id_idx").on(table.videoId),
Expand Down
Loading