this repo has no description
0
fork

Configure Feed

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

Improve logging

alice 455892bf 85d95a5b

+48 -28
+41 -25
src/label.ts
··· 16 16 17 17 export const label = async (subject: string | AppBskyActorDefs.ProfileView, rkey: string) => { 18 18 const did = AppBskyActorDefs.isProfileView(subject) ? subject.did : subject; 19 - console.log(`Labeling ${did}...`); 19 + console.group(`Labeling ${did}`); 20 20 console.log(`Received rkey: ${rkey}`); 21 21 22 22 try { 23 23 const labelCategories = fetchCurrentLabels(did); 24 24 25 25 if (rkey.includes(DELETE)) { 26 - console.log('Delete operation detected'); 26 + console.group('Deleting all labels...'); 27 27 await deleteAllLabels(did, labelCategories); 28 + console.groupEnd(); 28 29 } else { 29 - console.log('Add/Update operation detected'); 30 + console.group('Adding/updating label...'); 30 31 await addOrUpdateLabel(did, rkey, labelCategories); 32 + console.groupEnd(); 31 33 } 32 34 } catch (error) { 33 - console.error('Error in label function:', error); 35 + console.error('Error in `label` function:', error); 34 36 } 37 + console.groupEnd(); 35 38 }; 36 39 37 40 function fetchCurrentLabels(did: string) { 38 - console.log('Fetching current labels for:', did); 41 + console.group('Fetching current labels'); 42 + console.log('DID:', did); 39 43 const categories = ['sun', 'moon', 'rising']; 40 44 const labelCategories: Record<string, Set<string>> = {}; 41 45 42 46 for (const category of categories) { 47 + console.group(`Category: ${category}`); 43 48 const prefix = category === 'sun' ? 'aaa-' : category === 'moon' ? 'bbb-' : 'ccc-'; 44 49 const query = server.db 45 50 .prepare< ··· 55 60 }, new Set<string>()); 56 61 57 62 labelCategories[category] = labels; 58 - console.log(`${category} label:`, labelCategories[category]); 63 + console.log(`Labels:`, Array.from(labels)); 64 + console.groupEnd(); 59 65 } 60 66 67 + console.groupEnd(); 61 68 return labelCategories; 62 69 } 63 70 64 71 async function deleteAllLabels(did: string, labelCategories: Record<string, Set<string>>) { 65 - console.log('Attempting to delete all labels for:', did); 72 + console.group('Deleting all labels'); 73 + console.log('DID:', did); 66 74 const labelsToDelete = Object.values(labelCategories).flatMap((set) => Array.from(set)); 67 75 68 76 if (labelsToDelete.length === 0) { 69 - console.log('No labels to delete for', did); 70 - return; 71 - } 72 - 73 - console.log('Labels to delete:', labelsToDelete); 74 - try { 75 - await server.createLabels({ uri: did }, { negate: labelsToDelete }); 76 - console.log(`Successfully deleted all labels for ${did}`); 77 - } catch (error) { 78 - console.error('Error during mass deletion:', error); 77 + console.log('No labels to delete'); 78 + } else { 79 + console.log('Labels to delete:', labelsToDelete); 80 + try { 81 + await server.createLabels({ uri: did }, { negate: labelsToDelete }); 82 + console.log('Successfully deleted all labels'); 83 + } catch (error) { 84 + console.error('Error during mass deletion:', error); 85 + } 79 86 } 87 + console.groupEnd(); 80 88 } 81 89 82 90 async function addOrUpdateLabel(did: string, rkey: string, labelCategories: Record<string, Set<string>>) { 83 - console.log('Adding or updating label for:', did); 91 + console.group('Adding or updating label'); 92 + console.log('DID:', did, 'rkey:', rkey); 84 93 const newLabel = findLabelByPost(rkey); 85 94 if (!newLabel) { 86 - console.log('No matching label found for rkey:', rkey); 95 + console.log('No matching label found for rkey'); 96 + console.groupEnd(); 87 97 return; 88 98 } 89 99 ··· 95 105 console.log('New label:', newLabel.label); 96 106 97 107 if (existingLabels.size > 0) { 98 - console.log('Negating existing labels:', existingLabels); 108 + console.group('Negating existing labels'); 99 109 try { 100 110 await server.createLabels({ uri: did }, { negate: Array.from(existingLabels) }); 101 111 console.log('Successfully negated existing labels'); 102 112 } catch (error) { 103 113 console.error('Error negating existing labels:', error); 104 114 } 115 + console.groupEnd(); 105 116 } 106 117 107 - console.log('Adding new label:', newLabel.label); 118 + console.group('Adding new label'); 108 119 try { 109 120 await server.createLabel({ uri: did, val: newLabel.label }); 110 - console.log(`Successfully labeled ${did} with ${newLabel.label}`); 121 + console.log('Successfully labeled'); 111 122 labelCategories[category] = new Set([newLabel.label]); 112 123 } catch (error) { 113 124 console.error('Error adding new label:', error); 114 125 } 126 + console.groupEnd(); 127 + 128 + console.groupEnd(); 115 129 } 116 130 117 131 function findLabelByPost(rkey: string) { 118 - console.log('Finding label for rkey:', rkey); 132 + console.group('Finding label...'); 133 + console.log('rkey:', rkey); 119 134 for (const category of ['sun', 'moon', 'rising'] as const) { 120 135 const found = SIGNS[category].find((sign) => sign.post === rkey); 121 136 if (found) { 122 137 console.log('Found label:', found); 138 + console.groupEnd(); 123 139 return found; 124 140 } 125 141 } 126 - console.log('No label found for rkey:', rkey); 142 + console.log('No label found'); 143 + console.groupEnd(); 127 144 return null; 128 145 } 129 146 ··· 133 150 return category as Category; 134 151 } 135 152 } 136 - 137 153 throw new Error(`Invalid label: ${label}`); 138 154 }
+7 -3
src/main.ts
··· 47 47 if (op.action !== 'delete' && AppBskyFeedLike.isRecord(op.record)) { 48 48 if (op.record.subject.uri.includes(DID)) { 49 49 if (op.record.subject.uri.includes('app.bsky.feed.post')) { 50 - label(commit.repo, op.record.subject.uri.split('/').pop()!).catch((err: unknown) => { 51 - console.error(err); 52 - }); 50 + label(commit.repo, op.record.subject.uri.split('/').pop()!) 51 + .catch((err: unknown) => { 52 + console.error(err); 53 + }) 54 + .finally(() => { 55 + console.log('--- labeling done ---'); 56 + }); 53 57 } 54 58 } 55 59 }