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
20 changes: 14 additions & 6 deletions src/app/api/metrics/repo-explorer/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getSessionWithToken } from "@/lib/get-session-token";
import { fetchUserRepos } from "@/lib/github";

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

View workflow job for this annotation

GitHub Actions / Type check

Duplicate identifier 'fetchUserRepos'.
import { NextRequest } from "next/server";
import { isMetricsCacheBypassed, metricsCacheKey, withMetricsCache } from "@/lib/metrics-cache";
import { fetchUserRepos } from "@/lib/github";

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

View workflow job for this annotation

GitHub Actions / Type check

Duplicate identifier 'fetchUserRepos'.
import { ExplorerRepoCardData } from "@/lib/repo-analytics-types";


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

Expand All @@ -19,11 +21,16 @@
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(session.accessToken, {

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

View workflow job for this annotation

GitHub Actions / Type check

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
perPage: 100,
maxPages: 10,
});
const since = new Date();
since.setDate(since.getDate() - 30);
const sinceStr = since.toISOString().slice(0, 10);
Expand Down Expand Up @@ -89,7 +96,8 @@
result.sort((a, b) => b.commitCount - a.commitCount || new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());

return { repos: result };
});
}
);
return Response.json(data);
} catch (error) {
console.error(error);
Expand Down
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