this repo has no description
0
fork

Configure Feed

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

Cleanup

alice f7b755c8 2aeb93c2

+23 -97
+22 -62
src/label.ts
··· 11 11 12 12 export const label = async (subject: string | AppBskyActorDefs.ProfileView, rkey: string) => { 13 13 const did = AppBskyActorDefs.isProfileView(subject) ? subject.did : subject; 14 - logger.info(`>>> Labeling ${did}`); 15 - logger.info(`Received rkey: ${rkey}`); 14 + logger.info(`Received rkey: ${rkey} for ${did}`); 16 15 17 16 if (rkey === 'self') { 18 17 logger.info(`${did} liked the labeler. Returning.`); ··· 22 21 const labelCategories = fetchCurrentLabels(did); 23 22 24 23 if (rkey.includes(DELETE)) { 25 - logger.info('>>> Deleting all labels'); 26 24 await deleteAllLabels(did, labelCategories); 27 - logger.info('<<< Deleted all labels'); 28 25 } else { 29 - logger.info('>>> Adding/updating label...'); 30 26 await addOrUpdateLabel(did, rkey, labelCategories); 31 - logger.info('<<< Added/updated labels'); 32 27 } 33 28 } catch (error) { 34 - logger.error('Error in `label` function: %s', error); 35 - } finally { 36 - logger.info(`<<< ${did} labeling complete`); 29 + logger.error(`Error in \`label\` function: ${error}`); 37 30 } 38 31 }; 39 32 40 33 function fetchCurrentLabels(did: string) { 41 - logger.info('>>> fetchCurrentLabels started'); 42 - logger.info('did: %s', did); 43 34 const categories = ['sun', 'moon', 'rising']; 44 35 const labelCategories: Record<string, Set<string>> = {}; 45 36 46 37 for (const category of categories) { 47 - logger.info(`>>> Finding category ${category}`); 48 - const prefix = 49 - category === 'sun' ? 'aaa-' 50 - : category === 'moon' ? 'bbb-' 51 - : 'ccc-'; 38 + const prefix = CATEGORY_PREFIXES[category as Category]; 52 39 const query = labelerServer.db 53 40 .prepare< 54 41 unknown[], 55 42 ComAtprotoLabelDefs.Label 56 - >(`SELECT * FROM labels WHERE uri = ? AND val LIKE '${prefix}${category}-%' ORDER BY cts DESC`) 57 - .all(did); 43 + >(`SELECT * FROM labels WHERE uri = ? AND val LIKE ? ORDER BY cts DESC`) 44 + .all(did, `${prefix}-${category}-%`); 58 45 59 46 const labels = query.reduce((set, label) => { 60 47 if (!label.neg) set.add(label.val); ··· 63 50 }, new Set<string>()); 64 51 65 52 labelCategories[category] = labels; 66 - logger.info(`Labels: ${Array.from(labels)}`); 67 - logger.info(`<<< Finding category ${category} complete`); 53 + logger.info(`Current labels: ${Array.from(labels).join(', ')}`); 68 54 } 69 55 70 - logger.info('<<< fetchCurrentLabels returning'); 71 56 return labelCategories; 72 57 } 73 58 74 59 async function deleteAllLabels(did: string, labelCategories: Record<string, Set<string>>) { 75 - logger.info('>>> deleteAllLabels started'); 76 - logger.info('did: %s', did); 77 - const labelsToDelete = Object.values(labelCategories).flatMap((set) => Array.from(set)); 60 + const labelsToDelete: string[] = Object.values(labelCategories).flatMap((set) => Array.from(set)); 78 61 79 62 if (labelsToDelete.length === 0) { 80 - logger.info('No labels to delete'); 63 + logger.info(`No labels to delete`); 81 64 } else { 82 - logger.info(`Labels to delete: ${labelsToDelete}`); 65 + logger.info(`Labels to delete: ${labelsToDelete.join(', ')}`); 83 66 try { 84 67 await labelerServer.createLabels({ uri: did }, { negate: labelsToDelete }); 85 68 logger.info('Successfully deleted all labels'); 86 69 } catch (error) { 87 - logger.error('Error deleting all labels: %s', error); 88 - } finally { 89 - logger.info('<<< deleteAllLabels returning'); 70 + logger.error(`Error deleting all labels: ${error}`); 90 71 } 91 72 } 92 73 } 93 74 94 75 async function addOrUpdateLabel(did: string, rkey: string, labelCategories: Record<string, Set<string>>) { 95 - logger.info('>>> addOrUpdateLabel'); 96 - logger.info('did: %s, rkey: %s', did, rkey); 97 76 const newLabel = findLabelByPost(rkey); 98 77 if (!newLabel) { 99 - logger.info('No matching label found for rkey'); 100 - logger.info('<<< addOrUpdateLabel returning'); 78 + logger.info(`No matching label found for rkey: ${rkey}`); 101 79 return; 102 80 } 103 81 ··· 109 87 logger.info(`New label: ${newLabel.label}`); 110 88 111 89 if (existingLabels.size > 0) { 112 - logger.info('>>> Negating existing labels'); 113 90 try { 114 91 await labelerServer.createLabels({ uri: did }, { negate: Array.from(existingLabels) }); 115 92 logger.info('Successfully negated existing labels'); 116 93 } catch (error) { 117 - logger.error('Error negating existing labels: %s', error); 118 - } finally { 119 - logger.info('<<< Negating all labels complete'); 94 + logger.error(`Error negating existing labels: ${error}`); 120 95 } 121 96 } 122 97 123 - logger.info('>>> Adding new label'); 98 + logger.info(`Adding new label ${newLabel.label} for ${did}`); 124 99 try { 125 100 await labelerServer.createLabel({ uri: did, val: newLabel.label }); 126 101 logger.info('Successfully labeled'); 127 102 labelCategories[category] = new Set([newLabel.label]); 128 103 } catch (error) { 129 - logger.error('Error adding new label: %s', error); 130 - } finally { 131 - logger.info('<<< Adding new label complete'); 104 + logger.error(`Error adding new label: ${error}`); 132 105 } 133 - 134 - logger.info('<<< addOrUpdateLabel returning'); 135 106 } 136 107 137 108 function findLabelByPost(rkey: string) { 138 - logger.info('>>> findLabelByPost started'); 139 - logger.info('rkey: %s', rkey); 140 109 for (const category of ['sun', 'moon', 'rising'] as const) { 141 - const found = SIGNS[category].find((sign) => sign.post === rkey); 142 - if (found) { 143 - logger.info('Found label: %o', found); 144 - logger.info('<<< findLabelByPost returning'); 145 - return found; 110 + const label = SIGNS[category].find((sign) => sign.post === rkey); 111 + if (label) { 112 + return label; 146 113 } 147 114 } 148 - logger.info('No label found'); 149 - logger.info('<<< findLabelByPost returning'); 150 - return null; 151 115 } 152 116 153 - function getCategoryFromLabel(label: string): Category { 154 - for (const [category, prefix] of Object.entries(CATEGORY_PREFIXES)) { 155 - if (label.startsWith(`${prefix}-${category}-`)) { 156 - return category as Category; 157 - } 158 - } 159 - 160 - throw new Error(`Invalid label: ${label}`); 161 - } 117 + const getCategoryFromLabel = (label: string): Category => { 118 + return Object.entries(CATEGORY_PREFIXES).find(([category, prefix]) => 119 + label.startsWith(`${prefix}-${category}-`), 120 + )?.[0] as Category; 121 + };
+1 -2
src/metrics.ts
··· 22 22 }); 23 23 24 24 export const startMetricsServer = (port: number, host = '127.0.0.1') => { 25 - const server = app.listen(port, host, () => { 25 + return app.listen(port, host, () => { 26 26 logger.info(`Metrics server is listening on ${host}:${port}`); 27 27 }); 28 - return server; 29 28 };
-33
src/types.ts
··· 17 17 moon: 'bbb', 18 18 rising: 'ccc', 19 19 }; 20 - 21 - export interface EventStream { 22 - did: string; 23 - time_us: number; 24 - type: string; 25 - commit?: { 26 - rev: string; 27 - type: string; 28 - collection: string; 29 - rkey: string; 30 - record: { 31 - $type: string; 32 - createdAt: string; 33 - subject: { 34 - cid: string; 35 - uri: string; 36 - }; 37 - }; 38 - }; 39 - } 40 - 41 - export interface Label { 42 - ver?: number; 43 - src: string; 44 - uri: string; 45 - cid?: string; 46 - val: string; 47 - neg?: boolean; 48 - cts: string; 49 - exp?: string; 50 - sig?: Uint8Array; 51 - [k: string]: unknown; 52 - }