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
42 changes: 41 additions & 1 deletion src/app/api/ai/roast/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
import { NextResponse } from 'next/server';
import { GoogleGenerativeAI } from '@google/generative-ai';
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { resolveAppUser } from "@/lib/resolve-user";

import {
upstashRateLimitFixedWindow,
getUpstashConfig,
} from "@/lib/upstash-rest";

import { createMemoryFixedWindowRateLimiter } from "@/lib/rate-limit";

// Initialize the Google Generative AI SDK
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || '');
const AI_ROAST_LIMIT = 5;
const AI_ROAST_WINDOW_SECONDS = 60 * 60;

const memoryLimiter = createMemoryFixedWindowRateLimiter({
windowMs: AI_ROAST_WINDOW_SECONDS * 1000,
pruneIntervalMs: AI_ROAST_WINDOW_SECONDS * 1000,
maxEntries: 10_000,
});

export async function POST(req: Request) {
try {
const session = await getServerSession(authOptions);
const user = await resolveAppUser(
session.githubId,
session.githubLogin
);

if (!user) {
return NextResponse.json(
{ error: "User not found" },
{ status: 404 }
);
}

const userId = user.id;

if (!session?.githubId) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 401 }
);
}

try {
const body = await req.json();
const { mode, stats } = body;

Expand Down
2 changes: 1 addition & 1 deletion src/app/api/ai/weekly-summary/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Security
* --------
* - Session required — unauthenticated requests are rejected with 401.
* - ANTHROPIC_API_KEY is read only on the server; it never appears in any
* - ANTHROPIC_API_KEY isa read only on the server; it never appears in any
* response or client-accessible bundle.
* - Metrics are validated server-side before the prompt is built.
* - String fields (topRepo) are truncated to prevent over-long inputs.
Expand Down
24 changes: 18 additions & 6 deletions src/app/api/metrics/repo-explorer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import { fetchUserRepos } from "@/lib/github";
import { NextRequest } from "next/server";
import { isMetricsCacheBypassed, metricsCacheKey, withMetricsCache } from "@/lib/metrics-cache";
import { fetchUserRepos } from "@/lib/github";
import { ExplorerRepoCardData } from "@/lib/repo-analytics-types";


export const dynamic = "force-dynamic";
const GITHUB_API = "https://api.github.com";

Expand All @@ -19,11 +21,20 @@
const bypass = isMetricsCacheBypassed(req);
const key = metricsCacheKey(session.githubId ?? session.githubLogin!, "repo-explorer-v2" as any, { days: 7 });

try {
const data = await withMetricsCache({ bypass, key, ttlSeconds: 30 * 60 }, async () => {
// Paginate through all pages (up to 1000 repos) so users with more
// than 100 repositories see their complete list — fixes #2843.
const repos = await fetchUserRepos(accessToken, { perPage: 100, maxPages: 10 });
try {
const data = await withMetricsCache(
{ bypass, key, ttlSeconds: 30 * 60 },
async () => {
// Paginate through all pages (up to 1000 repos) so users with more
// than 100 repositories see their complete list — fixes #2843.
const repos = await fetchUserRepos(accessToken, {
perPage: 100,
maxPages: 10,
});

// ...rest of the existing code
}
);
const since = new Date();
since.setDate(since.getDate() - 30);
const sinceStr = since.toISOString().slice(0, 10);
Expand Down Expand Up @@ -89,10 +100,11 @@
result.sort((a, b) => b.commitCount - a.commitCount || new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());

return { repos: result };
});
}
);

Check failure on line 104 in src/app/api/metrics/repo-explorer/route.ts

View workflow job for this annotation

GitHub Actions / Type check

'catch' or 'finally' expected.
return Response.json(data);
} catch (error) {

Check failure on line 106 in src/app/api/metrics/repo-explorer/route.ts

View workflow job for this annotation

GitHub Actions / Type check

'try' expected.
console.error(error);
return Response.json({ error: "GitHub API error" }, { status: 502 });
}
}

Check failure on line 110 in src/app/api/metrics/repo-explorer/route.ts

View workflow job for this annotation

GitHub Actions / Type check

Declaration or statement expected.
30 changes: 26 additions & 4 deletions src/app/api/rooms/[roomId]/invite/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { getRoomById, getRoomMembers, addRoomMember } from '@/lib/supabase-rooms';
import {
getRoomById,
getRoomMembers,
createRoomInvitation,
getPendingInvitation
} from '@/lib/supabase-rooms';
import { NextResponse } from 'next/server';

export async function POST(
Expand Down Expand Up @@ -36,6 +41,23 @@ export async function POST(
const members = await getRoomMembers(roomId);
if (members.some((m) => m.github_username === github_username))
return NextResponse.json({ error: 'User is already a member' }, { status: 409 });
await addRoomMember(roomId, github_username);
return NextResponse.json({ success: true });
}
const existingInvite = await getPendingInvitation(
roomId,
github_username
);

if (existingInvite) {
return NextResponse.json(
{ error: "User already has a pending invitation." },
{ status: 409 }
);
}
await createRoomInvitation(
roomId,
github_username,
session.user.name
);
return NextResponse.json({
success: true,
message: "Invitation sent."
});}
31 changes: 29 additions & 2 deletions src/lib/supabase-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,36 @@ export async function getRoomMembers(roomId: string): Promise<RoomMember[]> {
if (error) throw error;
return data ?? [];
}
export async function getPendingInvitation(
roomId: string,
githubUsername: string
) {
const { data, error } = await supabaseAdmin
.from("room_invitations")
.select("id")
.eq("room_id", roomId)
.eq("github_username", githubUsername)
.eq("status", "pending")
.maybeSingle();

if (error) throw error;
return data;
}

export async function createRoomInvitation(
roomId: string,
githubUsername: string,
invitedBy: string
) {
const { error } = await supabaseAdmin
.from("room_invitations")
.insert({
room_id: roomId,
github_username: githubUsername,
invited_by: invitedBy,
status: "pending",
});

export async function addRoomMember(roomId: string, githubUsername: string) {
const { error } = await supabaseAdmin.from("room_members").insert({ room_id: roomId, github_username: githubUsername, role: "member" });
if (error) throw error;
}

Expand Down
13 changes: 13 additions & 0 deletions supabase/migrations/20260726_create_room_invitations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
create table room_invitations (
id uuid primary key default gen_random_uuid(),
room_id uuid references collaboration_rooms(id) on delete cascade,
github_username text not null,
invited_by text not null,
status text not null default 'pending',
created_at timestamptz default now(),
responded_at timestamptz
);

create unique index room_invitation_unique
on room_invitations(room_id, github_username)
where status = 'pending';
Loading