From 7c50b55331e1e0c82673458b239389e274525045 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 1 Feb 2022 12:29:14 -0600 Subject: [PATCH] Match lowercase tags for folds --- common/fold.ts | 1 + functions/src/create-fold.ts | 1 + functions/src/scripts/lowercase-fold-tags.ts | 34 ++++++++++++++++++++ web/lib/firebase/folds.ts | 7 ++-- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 functions/src/scripts/lowercase-fold-tags.ts diff --git a/common/fold.ts b/common/fold.ts index 3c6be6ab..e732e34d 100644 --- a/common/fold.ts +++ b/common/fold.ts @@ -7,6 +7,7 @@ export type Fold = { createdTime: number tags: string[] + lowercaseTags: string[] contractIds: string[] excludedContractIds: string[] diff --git a/functions/src/create-fold.ts b/functions/src/create-fold.ts index e86db2a1..36d1d269 100644 --- a/functions/src/create-fold.ts +++ b/functions/src/create-fold.ts @@ -59,6 +59,7 @@ export const createFold = functions.runWith({ minInstances: 1 }).https.onCall( name, about, tags, + lowercaseTags: tags.map((tag) => tag.toLowerCase()), createdTime: Date.now(), contractIds: [], excludedContractIds: [], diff --git a/functions/src/scripts/lowercase-fold-tags.ts b/functions/src/scripts/lowercase-fold-tags.ts new file mode 100644 index 00000000..bf7335ee --- /dev/null +++ b/functions/src/scripts/lowercase-fold-tags.ts @@ -0,0 +1,34 @@ +import * as admin from 'firebase-admin' +import * as _ from 'lodash' + +import { initAdmin } from './script-init' +initAdmin('james') + +import { getValues } from '../utils' +import { Fold } from '../../../common/fold' + +async function lowercaseFoldTags() { + const firestore = admin.firestore() + console.log('Updating fold tags') + + const folds = await getValues(firestore.collection('folds')) + + console.log('Loaded', folds.length, 'folds') + + for (const fold of folds) { + const foldRef = firestore.doc(`folds/${fold.id}`) + + const { tags } = fold + const lowercaseTags = _.uniq(tags.map((tag) => tag.toLowerCase())) + + console.log('Adding lowercase tags', fold.slug, lowercaseTags) + + await foldRef.update({ + lowercaseTags, + } as Partial) + } +} + +if (require.main === module) { + lowercaseFoldTags().then(() => process.exit()) +} diff --git a/web/lib/firebase/folds.ts b/web/lib/firebase/folds.ts index 2d3bef42..ef7090b1 100644 --- a/web/lib/firebase/folds.ts +++ b/web/lib/firebase/folds.ts @@ -135,9 +135,12 @@ export function listenForFollow( export function getFoldsByTags(tags: string[]) { if (tags.length === 0) return [] - const lowercaseTags = tags.map((tag) => tag) + const lowercaseTags = tags.map((tag) => tag.toLowerCase()) return getValues( // TODO: split into multiple queries if tags.length > 10. - query(foldCollection, where('tags', 'array-contains-any', lowercaseTags)) + query( + foldCollection, + where('lowercaseTags', 'array-contains-any', lowercaseTags) + ) ) }