this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix param defaults in queryLabels handler

futur 5331ec39 6a0781a9

+27 -18
+27 -18
src/LabelerServer.ts
··· 233 233 */ 234 234 queryLabelsHandler: RequestHandler = async (req, res) => { 235 235 try { 236 - const { uriPatterns, sources, limit: limitStr, cursor: cursorStr } = req.query as { 236 + const { 237 + uriPatterns = [], 238 + sources = [], 239 + limit: limitStr = "50", 240 + cursor: cursorStr = "0", 241 + } = req.query as { 237 242 uriPatterns?: Array<string>; 238 243 sources?: Array<string>; 239 244 limit?: string; 240 245 cursor?: string; 241 246 }; 242 247 243 - const cursor = cursorStr ? parseInt(cursorStr, 10) : undefined; 244 - if (cursor && Number.isNaN(cursor)) { 248 + const cursor = parseInt(cursorStr, 10); 249 + if (cursor !== undefined && Number.isNaN(cursor)) { 245 250 throw new InvalidRequestError("Cursor must be an integer"); 246 251 } 247 252 248 - const limit = parseInt(limitStr ?? "50", 10); 253 + const limit = parseInt(limitStr, 10); 249 254 if (Number.isNaN(limit) || limit < 1 || limit > 250) { 250 255 throw new InvalidRequestError("Limit must be an integer between 1 and 250"); 251 256 } 252 257 253 - const patterns = uriPatterns?.includes("*") 254 - ? undefined 255 - : uriPatterns?.map((pattern) => { 256 - if (pattern.indexOf("*") !== pattern.length - 1) { 257 - throw new InvalidRequestError( 258 - "Only trailing wildcards are supported in uriPatterns", 259 - ); 260 - } 261 - return pattern.replaceAll(/%/g, "").replaceAll(/_/g, "\\_").slice(0, -1) + "%"; 262 - }); 258 + const patterns = uriPatterns.includes("*") ? [] : uriPatterns.map((pattern) => { 259 + if (pattern.indexOf("*") !== pattern.length - 1) { 260 + throw new InvalidRequestError( 261 + "Only trailing wildcards are supported in uriPatterns", 262 + ); 263 + } 264 + return pattern.replaceAll(/%/g, "").replaceAll(/_/g, "\\_").slice(0, -1) + "%"; 265 + }); 263 266 264 267 const stmt = this.db.prepare<unknown[], ComAtprotoLabelDefs.Label>(` 265 268 SELECT * FROM labels 266 - ${patterns?.length ? patterns.map(() => "WHERE uri LIKE ?").join(" OR ") : ""} 267 - ${sources?.length ? "AND src IN (?)" : ""} 269 + WHERE 1 = 1 270 + ${patterns.length ? "AND " + patterns.map(() => "uri LIKE ?").join(" OR ") : ""} 271 + ${sources.length ? "AND src IN (?)" : ""} 268 272 ${cursor ? "AND id > ?" : ""} 269 273 ORDER BY id ASC 270 274 LIMIT ? 271 275 `); 272 276 273 - const rows = stmt.all([...(patterns ?? []), sources ?? [], cursor ?? 0, limit]); 277 + const params = []; 278 + if (patterns.length) params.push(...patterns); 279 + if (sources.length) params.push(sources); 280 + if (cursor) params.push(cursor); 281 + params.push(limit); 282 + 283 + const rows = stmt.all(params); 274 284 275 285 const labels = await Promise.all(rows.map((row) => this.ensureSignedLabel(row))); 276 286 const nextCursor = rows[rows.length - 1]?.id ?? 0; ··· 286 296 message: "An unknown error occurred", 287 297 }); 288 298 } 289 - return; 290 299 } 291 300 }; 292 301