aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/store/route.ts
blob: cb10db2476cea812cd37700f21671099fe63096b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { db } from "@/server/db";
import { and, eq, sql, inArray } from "drizzle-orm";
import {
  contentToSpace,
  sessions,
  storedContent,
  users,
  space,
} from "@/server/db/schema";
import { type NextRequest, NextResponse } from "next/server";
import { getMetaData } from "@/lib/get-metadata";
import { ensureAuth } from "../ensureAuth";
import { limit } from "@/app/actions/doers";
import { LIMITS } from "@/lib/constants";

export const runtime = "edge";

export async function POST(req: NextRequest) {
  const session = await ensureAuth(req);

  if (!session) {
    return new Response("Unauthorized", { status: 401 });
  }

  const data = (await req.json()) as {
    pageContent: string;
    url: string;
    spaces?: string[];
  };

  const metadata = await getMetaData(data.url);
  let storeToSpaces = data.spaces;

  if (!storeToSpaces) {
    storeToSpaces = [];
  }

  if (!(await limit(session.user.id))) {
    return NextResponse.json(
      {
        message: "Error: Ratelimit exceeded",
        error: `You have exceeded the limit of ${LIMITS["page"]} pages.`,
      },
      { status: 429 },
    );
  }

  const rep = await db
    .insert(storedContent)
    .values({
      content: data.pageContent,
      title: metadata.title,
      description: metadata.description,
      url: data.url,
      baseUrl: metadata.baseUrl,
      image: metadata.image,
      savedAt: new Date(),
      userId: session.user.id,
    })
    .returning({ id: storedContent.id });

  const id = rep[0]?.id;

  if (!id) {
    return NextResponse.json(
      { message: "Error", error: "Error in CF function" },
      { status: 500 },
    );
  }

  if (storeToSpaces.length > 0) {
    const spaceData = await db
      .select()
      .from(space)
      .where(
        and(
          inArray(space.name, storeToSpaces ?? []),
          eq(space.user, session.user.id),
        ),
      )
      .all();

    await Promise.all([
      spaceData.forEach(async (space) => {
        await db
          .insert(contentToSpace)
          .values({ contentId: id, spaceId: space.id });
      }),
    ]);
  }

  const res = (await Promise.race([
    fetch("https://cf-ai-backend.dhravya.workers.dev/add", {
      method: "POST",
      headers: {
        "X-Custom-Auth-Key": process.env.BACKEND_SECURITY_KEY,
      },
      body: JSON.stringify({ ...data, user: session.user.email }),
    }),
    new Promise((_, reject) =>
      setTimeout(() => reject(new Error("Request timed out")), 40000),
    ),
  ])) as Response;

  if (res.status !== 200) {
    console.log(res.status, res.statusText);
    return NextResponse.json(
      { message: "Error", error: "Error in CF function" },
      { status: 500 },
    );
  }

  return NextResponse.json({ message: "OK", data: "Success" }, { status: 200 });
}