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
|
import { and, asc, eq, inArray } from "drizzle-orm";
import { db } from "../db";
import {
pinnedRepositories,
repositories,
users,
} from "../db/schema";
export const MAX_PINS = 6;
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;
}
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 [];
}
}
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]));
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 [];
}
}
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 };
|