|
| 1 | +import * as functions from 'firebase-functions'; |
| 2 | +import algoliasearch from 'algoliasearch'; |
| 3 | +import { algoliaAppId, algoliaApiKey, algoliaIndex } from '../config/config'; |
| 4 | +import { |
| 5 | + getNotionPageMarkdown, |
| 6 | + getPurrfectStreamPageMarkdown, |
| 7 | + queryAll, |
| 8 | +} from '../utilities/notion.server'; |
| 9 | + |
| 10 | +const algolia = algoliasearch(algoliaAppId, algoliaApiKey); |
| 11 | +const ai = algolia.initIndex(algoliaIndex); |
| 12 | + |
| 13 | +function sleep(ms: number) { |
| 14 | + return new Promise((resolve) => { |
| 15 | + console.log('Pausing 3sec'); |
| 16 | + setTimeout(resolve, ms); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +const paginate = async ( |
| 21 | + _type: string, |
| 22 | + raw: any, |
| 23 | + preview?: boolean |
| 24 | +): Promise<any> => { |
| 25 | + // Call to get full content and add to algolia |
| 26 | + for (const p of raw.results) { |
| 27 | + if ( |
| 28 | + p?.properties?.published?.select?.name === 'published' || |
| 29 | + p?.properties?.status?.select?.name === 'Released' |
| 30 | + ) { |
| 31 | + console.log(`finding: ${p?.id}`); |
| 32 | + if (p?.properties?.slug?.url) { |
| 33 | + let post; |
| 34 | + if (_type === 'podcast') { |
| 35 | + post = await getPurrfectStreamPageMarkdown(p?.properties?.slug?.url); |
| 36 | + } else { |
| 37 | + post = await getNotionPageMarkdown({ |
| 38 | + _type, |
| 39 | + slug: p?.properties?.slug?.url, |
| 40 | + preview, |
| 41 | + }); |
| 42 | + } |
| 43 | + const result = await ai.saveObject({ |
| 44 | + ...post, |
| 45 | + objectID: post._id, |
| 46 | + type: post._type, |
| 47 | + }); |
| 48 | + console.log(`Algolia add ${_type}:`, JSON.stringify(result)); |
| 49 | + } |
| 50 | + } else { |
| 51 | + const result = await ai.deleteObject(p?._id); |
| 52 | + console.log(`Algolia delete ${_type}:`, JSON.stringify(result)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (raw.next_cursor) { |
| 57 | + const newRaw = await queryAll(_type, 3, raw.next_cursor); |
| 58 | + await paginate(_type, newRaw); |
| 59 | + } else { |
| 60 | + console.log('finished pagination'); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const scheduledNotionToAlgolia = functions |
| 65 | + .runWith({ |
| 66 | + timeoutSeconds: 540, |
| 67 | + }) |
| 68 | + .pubsub.schedule('every 1 hours') |
| 69 | + .onRun(async () => { |
| 70 | + // .https.onRequest(async (req, res) => { |
| 71 | + // Check to see if ther are scheduled pods |
| 72 | + console.log('Update Algolia from Notion'); |
| 73 | + |
| 74 | + //Get initial cursors |
| 75 | + const [posts, tutorials, courses, pages, podcasts] = await Promise.all([ |
| 76 | + queryAll('post', 3), |
| 77 | + queryAll('tutorial', 3), |
| 78 | + queryAll('course', 3), |
| 79 | + queryAll('page', 3), |
| 80 | + queryAll('podcast', 3), |
| 81 | + ]); |
| 82 | + await paginate('post', posts); |
| 83 | + await sleep(3000); |
| 84 | + await paginate('tutorial', tutorials); |
| 85 | + await sleep(3000); |
| 86 | + await paginate('course', courses); |
| 87 | + await sleep(3000); |
| 88 | + await paginate('page', pages); |
| 89 | + await sleep(3000); |
| 90 | + await paginate('podcast', podcasts); |
| 91 | + return null; |
| 92 | + // res.send(200); |
| 93 | + }); |
0 commit comments