cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / lib / products.ts
src/lib/products.ts · 303 lines · 8.7 KB← back to folder
| 1 | import crypto from "node:crypto"; |
| 2 | import { query, isDatabaseConfigured } from "@/db"; |
| 3 | import { isReservedUsername, slugifyProductName } from "./validation"; |
| 4 | |
| 5 | export interface ProductListItem { |
| 6 | id: string; |
| 7 | name: string; |
| 8 | slug: string; |
| 9 | website_url: string; |
| 10 | description: string; |
| 11 | icon_url: string | null; |
| 12 | screenshot_url: string | null; |
| 13 | status: string; |
| 14 | published_at: string; |
| 15 | owner_id: string; |
| 16 | owner_username: string; |
| 17 | star_count: number; |
| 18 | starred_by_me: boolean; |
| 19 | upvote_count: number; |
| 20 | upvoted_by_me: boolean; |
| 21 | } |
| 22 | |
| 23 | export interface ProductDetail extends ProductListItem { |
| 24 | readme_md: string; |
| 25 | created_at: string; |
| 26 | updated_at: string; |
| 27 | } |
| 28 | |
| 29 | export interface MakerProfile { |
| 30 | id: string; |
| 31 | username: string; |
| 32 | name: string | null; |
| 33 | avatar_url: string | null; |
| 34 | bio: string; |
| 35 | website_url: string; |
| 36 | created_at: string; |
| 37 | product_count: number; |
| 38 | } |
| 39 | |
| 40 | export async function listPublishedProducts(options: { |
| 41 | page?: number; |
| 42 | pageSize?: number; |
| 43 | viewerUserId?: string | null; |
| 44 | sort?: "newest" | "trending"; |
| 45 | }): Promise<{ products: ProductListItem[]; hasMore: boolean; page: number; pageSize: number }> { |
| 46 | const page = Math.max(1, options.page ?? 1); |
| 47 | const pageSize = options.pageSize ?? 25; |
| 48 | const sort = options.sort === "trending" ? "trending" : "newest"; |
| 49 | |
| 50 | if (!isDatabaseConfigured()) { |
| 51 | return { products: [], hasMore: false, page, pageSize }; |
| 52 | } |
| 53 | |
| 54 | const orderBy = |
| 55 | sort === "trending" |
| 56 | ? "COUNT(DISTINCT up.user_id) DESC, COUNT(DISTINCT s.user_id) DESC, p.published_at DESC, p.created_at DESC" |
| 57 | : "p.published_at DESC, p.created_at DESC"; |
| 58 | |
| 59 | const offset = (page - 1) * pageSize; |
| 60 | const res = await query<ProductListItem & { star_count: string; upvote_count: string }>( |
| 61 | `SELECT |
| 62 | p.id, |
| 63 | p.name, |
| 64 | p.slug, |
| 65 | p.website_url, |
| 66 | p.description, |
| 67 | p.icon_url, |
| 68 | p.screenshot_url, |
| 69 | p.status, |
| 70 | p.published_at, |
| 71 | p.owner_id, |
| 72 | u.username AS owner_username, |
| 73 | COUNT(DISTINCT s.user_id)::int AS star_count, |
| 74 | BOOL_OR(s.user_id = $1) AS starred_by_me, |
| 75 | COUNT(DISTINCT up.user_id)::int AS upvote_count, |
| 76 | BOOL_OR(up.user_id = $1) AS upvoted_by_me |
| 77 | FROM products p |
| 78 | JOIN users u ON u.id = p.owner_id |
| 79 | LEFT JOIN stars s ON s.product_id = p.id |
| 80 | LEFT JOIN upvotes up ON up.product_id = p.id |
| 81 | WHERE p.status = 'published' |
| 82 | GROUP BY p.id, u.username |
| 83 | ORDER BY ${orderBy} |
| 84 | LIMIT $2 OFFSET $3`, |
| 85 | [options.viewerUserId ?? null, pageSize + 1, offset] |
| 86 | ); |
| 87 | |
| 88 | const hasMore = res.rows.length > pageSize; |
| 89 | const products = res.rows.slice(0, pageSize).map((r) => ({ |
| 90 | ...r, |
| 91 | star_count: Number(r.star_count ?? 0), |
| 92 | starred_by_me: Boolean(r.starred_by_me), |
| 93 | upvote_count: Number(r.upvote_count ?? 0), |
| 94 | upvoted_by_me: Boolean(r.upvoted_by_me), |
| 95 | })); |
| 96 | |
| 97 | return { products, hasMore, page, pageSize }; |
| 98 | } |
| 99 | |
| 100 | export async function getProductBySlug( |
| 101 | slug: string, |
| 102 | viewerUserId?: string | null, |
| 103 | includeNonPublished = false |
| 104 | ): Promise<ProductDetail | null> { |
| 105 | if (!isDatabaseConfigured()) return null; |
| 106 | |
| 107 | const statusClause = includeNonPublished ? "" : "AND p.status = 'published'"; |
| 108 | const res = await query<ProductDetail & { star_count: string; upvote_count: string }>( |
| 109 | `SELECT |
| 110 | p.id, |
| 111 | p.name, |
| 112 | p.slug, |
| 113 | p.website_url, |
| 114 | p.description, |
| 115 | p.readme_md, |
| 116 | p.icon_url, |
| 117 | p.screenshot_url, |
| 118 | p.status, |
| 119 | p.published_at, |
| 120 | p.created_at, |
| 121 | p.updated_at, |
| 122 | p.owner_id, |
| 123 | u.username AS owner_username, |
| 124 | COUNT(DISTINCT s.user_id)::int AS star_count, |
| 125 | BOOL_OR(s.user_id = $2) AS starred_by_me, |
| 126 | COUNT(DISTINCT up.user_id)::int AS upvote_count, |
| 127 | BOOL_OR(up.user_id = $2) AS upvoted_by_me |
| 128 | FROM products p |
| 129 | JOIN users u ON u.id = p.owner_id |
| 130 | LEFT JOIN stars s ON s.product_id = p.id |
| 131 | LEFT JOIN upvotes up ON up.product_id = p.id |
| 132 | WHERE p.slug = $1 ${statusClause} |
| 133 | GROUP BY p.id, u.username |
| 134 | LIMIT 1`, |
| 135 | [slug, viewerUserId ?? null] |
| 136 | ); |
| 137 | |
| 138 | const row = res.rows[0]; |
| 139 | if (!row) return null; |
| 140 | |
| 141 | return { |
| 142 | ...row, |
| 143 | star_count: Number(row.star_count ?? 0), |
| 144 | starred_by_me: Boolean(row.starred_by_me), |
| 145 | upvote_count: Number(row.upvote_count ?? 0), |
| 146 | upvoted_by_me: Boolean(row.upvoted_by_me), |
| 147 | }; |
| 148 | } |
| 149 | |
| 150 | export async function generateUniqueSlug(name: string): Promise<string> { |
| 151 | const base = slugifyProductName(name); |
| 152 | let candidate = base; |
| 153 | for (let i = 0; i < 20; i++) { |
| 154 | if (!isReservedUsername(candidate)) { |
| 155 | const [prod, user] = await Promise.all([ |
| 156 | query(`SELECT 1 FROM products WHERE LOWER(slug) = LOWER($1)`, [candidate]), |
| 157 | query(`SELECT 1 FROM users WHERE LOWER(username) = LOWER($1)`, [candidate]), |
| 158 | ]); |
| 159 | if (prod.rowCount === 0 && user.rowCount === 0) return candidate; |
| 160 | } |
| 161 | const suffix = i === 0 ? "2" : crypto.randomBytes(2).toString("hex"); |
| 162 | candidate = `${base.slice(0, 72)}-${suffix}`; |
| 163 | } |
| 164 | return `${base.slice(0, 68)}-${crypto.randomBytes(4).toString("hex")}`; |
| 165 | } |
| 166 | |
| 167 | export async function listMakers(): Promise<MakerProfile[]> { |
| 168 | if (!isDatabaseConfigured()) return []; |
| 169 | |
| 170 | const res = await query<MakerProfile & { product_count: string }>( |
| 171 | `SELECT |
| 172 | u.id, |
| 173 | u.username, |
| 174 | u.name, |
| 175 | u.avatar_url, |
| 176 | u.bio, |
| 177 | u.website_url, |
| 178 | u.created_at, |
| 179 | COUNT(p.id) FILTER (WHERE p.status = 'published')::int AS product_count |
| 180 | FROM users u |
| 181 | LEFT JOIN products p ON p.owner_id = u.id |
| 182 | GROUP BY u.id |
| 183 | HAVING COUNT(p.id) FILTER (WHERE p.status = 'published') > 0 |
| 184 | ORDER BY product_count DESC, u.created_at ASC` |
| 185 | ); |
| 186 | |
| 187 | return res.rows.map((r) => ({ |
| 188 | ...r, |
| 189 | product_count: Number(r.product_count ?? 0), |
| 190 | })); |
| 191 | } |
| 192 | |
| 193 | export async function getMakerByUsername(username: string): Promise<MakerProfile | null> { |
| 194 | if (!isDatabaseConfigured()) return null; |
| 195 | |
| 196 | const res = await query<MakerProfile & { product_count: string }>( |
| 197 | `SELECT |
| 198 | u.id, |
| 199 | u.username, |
| 200 | u.name, |
| 201 | u.avatar_url, |
| 202 | u.bio, |
| 203 | u.website_url, |
| 204 | u.created_at, |
| 205 | COUNT(p.id) FILTER (WHERE p.status = 'published')::int AS product_count |
| 206 | FROM users u |
| 207 | LEFT JOIN products p ON p.owner_id = u.id |
| 208 | WHERE LOWER(u.username) = LOWER($1) |
| 209 | GROUP BY u.id |
| 210 | LIMIT 1`, |
| 211 | [username] |
| 212 | ); |
| 213 | |
| 214 | const row = res.rows[0]; |
| 215 | if (!row) return null; |
| 216 | return { |
| 217 | ...row, |
| 218 | product_count: Number(row.product_count ?? 0), |
| 219 | }; |
| 220 | } |
| 221 | |
| 222 | export async function listProductsByMaker( |
| 223 | ownerId: string, |
| 224 | viewerUserId?: string | null, |
| 225 | includeNonPublished = false |
| 226 | ): Promise<ProductListItem[]> { |
| 227 | if (!isDatabaseConfigured()) return []; |
| 228 | |
| 229 | const statusClause = includeNonPublished ? "AND p.status <> 'removed'" : "AND p.status = 'published'"; |
| 230 | const res = await query<ProductListItem & { star_count: string; upvote_count: string }>( |
| 231 | `SELECT |
| 232 | p.id, |
| 233 | p.name, |
| 234 | p.slug, |
| 235 | p.website_url, |
| 236 | p.description, |
| 237 | p.icon_url, |
| 238 | p.screenshot_url, |
| 239 | p.status, |
| 240 | p.published_at, |
| 241 | p.owner_id, |
| 242 | u.username AS owner_username, |
| 243 | COUNT(DISTINCT s.user_id)::int AS star_count, |
| 244 | BOOL_OR(s.user_id = $2) AS starred_by_me, |
| 245 | COUNT(DISTINCT up.user_id)::int AS upvote_count, |
| 246 | BOOL_OR(up.user_id = $2) AS upvoted_by_me |
| 247 | FROM products p |
| 248 | JOIN users u ON u.id = p.owner_id |
| 249 | LEFT JOIN stars s ON s.product_id = p.id |
| 250 | LEFT JOIN upvotes up ON up.product_id = p.id |
| 251 | WHERE p.owner_id = $1 ${statusClause} |
| 252 | GROUP BY p.id, u.username |
| 253 | ORDER BY p.published_at DESC, p.created_at DESC`, |
| 254 | [ownerId, viewerUserId ?? null] |
| 255 | ); |
| 256 | |
| 257 | return res.rows.map((r) => ({ |
| 258 | ...r, |
| 259 | star_count: Number(r.star_count ?? 0), |
| 260 | starred_by_me: Boolean(r.starred_by_me), |
| 261 | upvote_count: Number(r.upvote_count ?? 0), |
| 262 | upvoted_by_me: Boolean(r.upvoted_by_me), |
| 263 | })); |
| 264 | } |
| 265 | |
| 266 | export async function listAllProductsForModeration(): Promise<ProductListItem[]> { |
| 267 | if (!isDatabaseConfigured()) return []; |
| 268 | |
| 269 | const res = await query<ProductListItem & { star_count: string; upvote_count: string }>( |
| 270 | `SELECT |
| 271 | p.id, |
| 272 | p.name, |
| 273 | p.slug, |
| 274 | p.website_url, |
| 275 | p.description, |
| 276 | p.icon_url, |
| 277 | p.screenshot_url, |
| 278 | p.status, |
| 279 | p.published_at, |
| 280 | p.owner_id, |
| 281 | u.username AS owner_username, |
| 282 | COUNT(DISTINCT s.user_id)::int AS star_count, |
| 283 | FALSE AS starred_by_me, |
| 284 | COUNT(DISTINCT up.user_id)::int AS upvote_count, |
| 285 | FALSE AS upvoted_by_me |
| 286 | FROM products p |
| 287 | JOIN users u ON u.id = p.owner_id |
| 288 | LEFT JOIN stars s ON s.product_id = p.id |
| 289 | LEFT JOIN upvotes up ON up.product_id = p.id |
| 290 | GROUP BY p.id, u.username |
| 291 | ORDER BY p.created_at DESC |
| 292 | LIMIT 200` |
| 293 | ); |
| 294 | |
| 295 | return res.rows.map((r) => ({ |
| 296 | ...r, |
| 297 | star_count: Number(r.star_count ?? 0), |
| 298 | starred_by_me: false, |
| 299 | upvote_count: Number(r.upvote_count ?? 0), |
| 300 | upvoted_by_me: false, |
| 301 | })); |
| 302 | } |
| 303 |