Skip to content

Commit b669515

Browse files
committed
add firebase notion to algolia
1 parent 24cbc0a commit b669515

12 files changed

Lines changed: 16883 additions & 3044 deletions

File tree

backend/firebase/firebase.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"predeploy": [
1111
"npm --prefix \"$RESOURCE_DIR\" run lint",
1212
"npm --prefix \"$RESOURCE_DIR\" run build"
13-
]
13+
],
14+
"source": "functions",
15+
"engines": { "node": "16" }
1416
},
1517
"storage": {
1618
"rules": "storage.rules"
@@ -33,6 +35,9 @@
3335
},
3436
"ui": {
3537
"enabled": true
38+
},
39+
"storage": {
40+
"port": 9199
3641
}
3742
},
3843
"remoteconfig": {

backend/firebase/functions/package-lock.json

Lines changed: 4324 additions & 2790 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/firebase/functions/package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"logs": "firebase functions:log"
1010
},
1111
"engines": {
12-
"node": "12"
12+
"node": "16"
1313
},
1414
"main": "lib/index.js",
1515
"dependencies": {
@@ -20,21 +20,23 @@
2020
"algoliasearch": "^4.8.3",
2121
"cloudinary": "^1.23.0",
2222
"firebase-admin": "^9.2.0",
23-
"firebase-functions": "^3.11.0",
23+
"firebase-functions": "^3.21.2",
2424
"googleapis": "^89.0.0",
2525
"node-fetch": "^2.6.7",
26+
"notion-to-md": "^2.3.3",
2627
"slugify": "^1.5.0",
2728
"twitter-api-v2": "^1.6.5",
2829
"uuid": "^8.3.2"
2930
},
3031
"devDependencies": {
3132
"@types/uuid": "^8.3.0",
32-
"@typescript-eslint/eslint-plugin": "^3.9.1",
33-
"@typescript-eslint/parser": "^3.8.0",
34-
"eslint": "^7.6.0",
35-
"eslint-plugin-import": "^2.22.0",
33+
"@typescript-eslint/eslint-plugin": "^5.12.0",
34+
"@typescript-eslint/parser": "^5.12.0",
35+
"eslint": "^8.9.0",
36+
"eslint-config-google": "^0.14.0",
37+
"eslint-plugin-import": "^2.25.4",
3638
"firebase-functions-test": "^0.2.0",
37-
"typescript": "^3.8.0"
39+
"typescript": "^4.5.4"
3840
},
3941
"private": true
4042
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
});

backend/firebase/functions/src/backups/firestore.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export const scheduledFirestoreExport = functions.pubsub
1919
.schedule('every 24 hours')
2020
.onRun(() => {
2121
const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT;
22+
23+
if (!projectId) {
24+
return false;
25+
}
26+
2227
const databaseName = client.databasePath(projectId, '(default)');
2328

2429
return client

backend/firebase/functions/src/config/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ export const notionPurrfectCompanyDatabaseId = checkMain(
5757
'notion',
5858
'purrfect_company_database_id'
5959
);
60+
export const notionPurrfectPicks = checkMain('notion', 'purrfect_picks');
61+
export const notionAuthors = checkMain('notion', 'authors');
62+
export const notionLessons = checkMain('notion', 'lessons');
63+
export const notionCourses = checkMain('notion', 'courses');
64+
export const notionFrameworks = checkMain('notion', 'frameworks');
65+
export const notionPosts = checkMain('notion', 'posts');
66+
export const notionTutorials = checkMain('notion', 'tutorials');
67+
export const notionPages = checkMain('notion', 'pages');
68+
export const notionSections = checkMain('notion', 'sections');
69+
export const notionLanguages = checkMain('notion', 'languages');

backend/firebase/functions/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ export {
1616
scheduledNotionToCloudinary,
1717
cloudinaryToNotionPubSub,
1818
} from './cloudinary/scheduledNotionCheck';
19+
20+
// Algolia scheduled
21+
export { scheduledNotionToAlgolia } from './algolia/algolia';

0 commit comments

Comments
 (0)