this repo has no description
0
fork

Configure Feed

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

Make cid nullable, neg default false, assume sig is always set

futur 4157d6b7 5331ec39

+17 -31
+13 -29
src/LabelerServer.ts
··· 90 90 id INTEGER PRIMARY KEY AUTOINCREMENT, 91 91 src TEXT NOT NULL, 92 92 uri TEXT NOT NULL, 93 - cid TEXT NOT NULL, 93 + cid TEXT, 94 94 val TEXT NOT NULL, 95 - neg BOOLEAN NOT NULL, 95 + neg BOOLEAN DEFAULT FALSE, 96 96 cts DATETIME NOT NULL, 97 97 exp DATETIME, 98 98 sig BLOB ··· 135 135 const { src, uri, cid, val, neg, cts, exp, sig } = signed; 136 136 const result = stmt.run(src, uri, cid, val, neg, cts, exp, sig); 137 137 if (!result.changes) throw new Error("Failed to insert label"); 138 - await this.emitLabel(signed); 138 + this.emitLabel(signed); 139 139 return signed; 140 140 } 141 141 ··· 175 175 } 176 176 177 177 /** 178 - * Ensure a label is signed, updating if necessary. 179 - * @param label The label to ensure is signed. 180 - */ 181 - private async ensureSignedLabel(label: ComAtprotoLabelDefs.Label): Promise<SignedLabel> { 182 - if (!labelIsSigned(label)) { 183 - const signed = await signLabel(label, this.signingKey); 184 - const stmt = this.db.prepare(` 185 - UPDATE labels 186 - SET sig = ? 187 - WHERE id = ? 188 - `).run(signed.sig, label.id); 189 - if (!stmt.changes) throw new Error("Failed to update label with signature"); 190 - return signed; 191 - } 192 - return formatLabel(label); 193 - } 194 - 195 - /** 196 178 * Emit a label to all subscribers. 197 179 * @param label The label to emit. 198 180 */ 199 - private async emitLabel(label: ComAtprotoLabelDefs.Label) { 200 - const signed = await this.ensureSignedLabel(label); 201 - const frame = new MessageFrame({ seq: label.id, labels: [signed] }, { type: "#labels" }); 181 + private emitLabel({ id, ...label }: ComAtprotoLabelDefs.Label) { 182 + const frame = new MessageFrame({ seq: id, labels: [formatLabel(label)] }, { 183 + type: "#labels", 184 + }); 202 185 this.connections.get("com.atproto.label.subscribeLabels")?.forEach((ws) => { 203 186 ws.send(frame.toBytes()); 204 187 }); ··· 231 214 /** 232 215 * Handler for com.atproto.label.queryLabels. 233 216 */ 234 - queryLabelsHandler: RequestHandler = async (req, res) => { 217 + queryLabelsHandler: RequestHandler = (req, res) => { 235 218 try { 236 219 const { 237 220 uriPatterns = [], ··· 281 264 params.push(limit); 282 265 283 266 const rows = stmt.all(params); 267 + const labels = rows.map(formatLabel); 284 268 285 - const labels = await Promise.all(rows.map((row) => this.ensureSignedLabel(row))); 286 269 const nextCursor = rows[rows.length - 1]?.id ?? 0; 287 270 288 271 res.json({ cursor: nextCursor, labels }); ··· 302 285 /** 303 286 * Handler for com.atproto.label.subscribeLabels. 304 287 */ 305 - subscribeLabelsHandler: WebsocketRequestHandler = async (ws, req) => { 288 + subscribeLabelsHandler: WebsocketRequestHandler = (ws, req) => { 306 289 const cursor = parseInt(req.params.cursor); 307 290 308 291 if (cursor && !Number.isNaN(cursor)) { ··· 326 309 327 310 try { 328 311 for (const row of stmt.iterate(cursor)) { 329 - await this.ensureSignedLabel(row); 330 312 const { id: seq, ...label } = row; 331 - const frame = new MessageFrame({ seq, labels: [label] }, { type: "#labels" }); 313 + const frame = new MessageFrame({ seq, labels: [formatLabel(label)] }, { 314 + type: "#labels", 315 + }); 332 316 ws.send(frame.toBytes()); 333 317 } 334 318 } catch (e) {
+4 -2
src/util/labels.ts
··· 5 5 6 6 const LABEL_VERSION = 1; 7 7 8 - export function formatLabel<T extends ComAtprotoLabelDefs.Label>(label: T): StrictPartial<T> { 8 + export function formatLabel( 9 + label: ComAtprotoLabelDefs.Label, 10 + ): StrictPartial<ComAtprotoLabelDefs.Label> { 9 11 const { src, uri, cid, val, neg, cts, exp } = label; 10 12 return { 11 13 ver: LABEL_VERSION, ··· 13 15 uri, 14 16 ...(cid ? { cid } : {}), 15 17 val, 16 - ...(!!neg ? { neg } : {}), 18 + neg, 17 19 cts, 18 20 ...(exp ? { exp } : {}), 19 21 } as never;