Skip to content

Commit

Permalink
chore: add ai-tag handling to cloud function
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Nov 14, 2024
1 parent a6d5c58 commit ae039b9
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions functions-v2/src/on-document-tagged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export const onDocumentTagged = onDocumentWritten(

const {root, space, documentId} = event.params;
const firestore = admin.firestore();
const docKey = await firestore.collection(`${root}/${space}/documents`).doc(documentId).get().then((doc) => {
let docKey = await firestore.collection(`${root}/${space}/documents`).doc(documentId).get().then((doc) => {
return doc.data()?.key;
});
if (!docKey) return;
if (!docKey) {
docKey = documentId;
}

const collectionPath = `${root}/${space}/documents`;
const documentCollection = admin.firestore().collection(collectionPath);
Expand Down Expand Up @@ -45,6 +47,27 @@ export const onDocumentTagged = onDocumentWritten(
}
}

// TODO: Make it so this second pass to pick up AI comments isn't needed.
// The AI system should be writing out metadata documents that get picked up by the query above.
const commentsUrl = `${root}/${space}/documents/${docKey}/comments`;
const commentCollection = admin.firestore().collection(commentsUrl);
const commentSnapshots = await commentCollection.get();

for (const _commentSnapshot of commentSnapshots.docs) {
const commentTags = _commentSnapshot.data()?.tags ?? [];

if (commentTags != null && !Array.isArray(commentTags)) {
console.warn("Found invalid comment tags", _commentSnapshot.ref.path, commentTags);
continue;
}

commentTags.forEach((tag: string) => {
if (tag && !strategies.includes(tag)) {
strategies.push(tag);
}
});
}

const metadataQuery = documentCollection.where("key", "==", docKey);
const querySnapshot = await metadataQuery.get();

Expand Down

0 comments on commit ae039b9

Please sign in to comment.