CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /**
* Block J13 — Pinned repositories on user profile.
*
* Users select up to 6 repositories to feature on their profile. Pure helpers
* here are driven by the route handler + unit tests; DB helpers swallow
* errors and return safe defaults.
*/
import { and, asc, eq, inArray } from "drizzle-orm";
import { db } from "../db";
import {
pinnedRepositories,
repositories,
users,
} from "../db/schema";
export const MAX_PINS = 6;
/**
* Sanitise an incoming pin list: de-dup, preserve first-seen order, clamp
* to MAX_PINS, drop empty strings. Pure; used by the form handler.
*/
export function sanitisePinIds(ids: Array<string | null | undefined>): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const id of ids) {
if (!id) continue;
const trimmed = id.trim();
if (!trimmed) continue;
if (seen.has(trimmed)) continue;
seen.add(trimmed);
out.push(trimmed);
if (out.length >= MAX_PINS) break;
}
return out;
}
/**
* List a user's pinned repos in display order, joined to the owning user so
* the profile can render "owner/name" links. Returns at most MAX_PINS rows.
*/
export async function listPinnedForUser(userId: string): Promise<
Array<{
repositoryId: string;
name: string;
ownerUsername: string;
description: string | null;
starCount: number;
forkCount: number;
isPrivate: boolean;
position: number;
}>
> {
try {
const rows = await db
.select({
repositoryId: pinnedRepositories.repositoryId,
position: pinnedRepositories.position,
name: repositories.name,
description: repositories.description,
starCount: repositories.starCount,
forkCount: repositories.forkCount,
isPrivate: repositories.isPrivate,
ownerUsername: users.username,
})
.from(pinnedRepositories)
.innerJoin(
repositories,
eq(repositories.id, pinnedRepositories.repositoryId)
)
.innerJoin(users, eq(users.id, repositories.ownerId))
.where(eq(pinnedRepositories.userId, userId))
.orderBy(asc(pinnedRepositories.position))
.limit(MAX_PINS);
return rows;
} catch (err) {
console.error("[pinned-repos] listPinnedForUser failed:", err);
return [];
}
}
/**
* Replace a user's entire pin set with the given ordered list of repo IDs.
* - Ignores IDs the user can't pin (private repos they don't own).
* - Uses delete-then-insert for atomicity-lite.
* Returns the resulting canonical list of pinned repo IDs.
*/
export async function setPinsForUser(
userId: string,
repoIds: string[]
): Promise<string[]> {
const clean = sanitisePinIds(repoIds);
if (clean.length === 0) {
try {
await db
.delete(pinnedRepositories)
.where(eq(pinnedRepositories.userId, userId));
} catch (err) {
console.error("[pinned-repos] clear failed:", err);
}
return [];
}
try {
const rows = await db
.select({
id: repositories.id,
ownerId: repositories.ownerId,
isPrivate: repositories.isPrivate,
})
.from(repositories)
.where(inArray(repositories.id, clean));
const byId = new Map(rows.map((r) => [r.id, r]));
// Filter: must exist; if private, viewer must be the owner.
const allowed = clean.filter((id) => {
const r = byId.get(id);
if (!r) return false;
if (r.isPrivate && r.ownerId !== userId) return false;
return true;
});
await db
.delete(pinnedRepositories)
.where(eq(pinnedRepositories.userId, userId));
if (allowed.length === 0) return [];
await db.insert(pinnedRepositories).values(
allowed.map((repositoryId, position) => ({
userId,
repositoryId,
position,
}))
);
return allowed;
} catch (err) {
console.error("[pinned-repos] setPinsForUser failed:", err);
return [];
}
}
/**
* List candidate repositories for the pin-chooser UI — repos the user owns
* (public + private) and repos they've starred (public only here; we don't
* want a user to pin someone else's private repo). Capped to 50 rows each.
*/
export async function listPinCandidates(userId: string): Promise<
Array<{ id: string; name: string; ownerUsername: string; isPrivate: boolean }>
> {
try {
const owned = await db
.select({
id: repositories.id,
name: repositories.name,
ownerUsername: users.username,
isPrivate: repositories.isPrivate,
})
.from(repositories)
.innerJoin(users, eq(users.id, repositories.ownerId))
.where(eq(repositories.ownerId, userId))
.limit(50);
return owned;
} catch (err) {
console.error("[pinned-repos] listPinCandidates failed:", err);
return [];
}
}
export const __internal = { MAX_PINS, sanitisePinIds };
|