Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rename all library functions
Browse files Browse the repository at this point in the history
benfurber committed Jan 21, 2025
1 parent 5fd7570 commit 94c7264
Showing 127 changed files with 1,032 additions and 1,030 deletions.
2 changes: 0 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@
"globby",
"gsutil",
"hakkens",
"howto",
"howtos",
"indexeddb",
"kamp",
"mappins",
2 changes: 1 addition & 1 deletion .github/labels.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
{ 'name': 'In progress', 'color': '440B89', 'description': '' },
{ 'name': 'Mod: DevOps 🤖', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: Discussions 💬', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: HowTo 📰', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: Library 📰', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: Maps 🗺', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: Other ⬜️', 'color': 'BFD4F2', 'description': '' },
{ 'name': 'Mod: Profiles 👱', 'color': 'BFD4F2', 'description': '' },
2 changes: 1 addition & 1 deletion BOUNTIES.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ We currently have 3 bounty levels
| Level | Amount | It should take... | E.g. |
| ----- | ------ | ----------------------------------------------------------- | ----------------------------------- |
| 1 | €20 | Roughly 1-2 hours work for a junior/intermediate developer | Small bugfixes, UI tweaks |
| 2 | €40 | Roughly 3-5 hours work for an intermediate/senior developer | Minor feature, e.g. Howto search |
| 2 | €40 | Roughly 3-5 hours work for an intermediate/senior developer | Minor feature, e.g. Library search |
| 3 | €85 | Roughly 6+ hours work for a more senior developer | Major feature, e.g. Research module |

---
17 changes: 15 additions & 2 deletions docs/supabase.md
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ How?

### Comment Counts

Currently we can sort questions/research/howtos by the number of comments.
Currently we can sort questions/research/library by the number of comments.
With supabase there are a few ways we can do this:

1. A comment count view
@@ -96,4 +96,17 @@ How?

- Whenever a comment is created or deleted, it triggers the update_comment_count function.
- The function checks the Operation kind (Insert/Delete), the source_type and source_id.
- Fron the source_type it will update the according content total (howtos, research, questions) that matches the source_id
- Fron the source_type it will update the according content total (library, research, questions) that matches the source_id

# Local firebase sync testing/debugging

_This is temporary until we fully migrate to supabase!_
We can create and deploy the sync function to the firebase dev environment.
Then, using ngrok, expose our local supabase url to the internet, and point the firebase function to it.
(ngrok http http://127.0.0.1:54321)

To authenticate, we need to create these 3 secrets, for each firebase project:
firebase functions:secrets:set SUPABASE_API_URL
firebase functions:secrets:set SUPABASE_API_KEY
firebase functions:secrets:set TENANT_ID
(Check values with firebase functions:secrets:access SECRET_NAME)
2 changes: 1 addition & 1 deletion functions/README.md
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ A couple tips to help implementing:
gcloud firestore export gs://[BUCKET_NAME] --collection-ids=[COLLECTION_ID_1],[COLLECTION_ID_2]
```

E.g. for the staging server, updating howtos:
E.g. for the staging server, updating the library:

```
gcloud firestore export gs://precious-plastics-v4-dev-exports/2020-10-12 --collection-ids=v3_howtos
7 changes: 4 additions & 3 deletions functions/src/Firebase/firestoreDB.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { firebaseApp } from './admin'
import { getFirestore } from 'firebase-admin/firestore'

import { DB_ENDPOINTS } from '../models'
import { getFirestore } from 'firebase-admin/firestore'
import { DBDoc, DBEndpoint } from 'oa-shared/models/db'
import { firebaseApp } from './admin'

import type { DBDoc, DBEndpoint } from 'oa-shared/models/db'

// TODO - ideally should remove default export to force using functions which have mapping
export const db = getFirestore(firebaseApp)
2 changes: 1 addition & 1 deletion functions/src/Integrations/firebase-discord.ts
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ export const notifyPinPublished = functions
.catch(handleErr)
})

export const notifyHowtoPublished = functions
export const notifyLibraryItemPublished = functions
.runWith({ memory: '512MB' })
.firestore.document('v3_howtos/{id}')
.onUpdate(async (change, context) => {
4 changes: 2 additions & 2 deletions functions/src/Integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Slack from './firebase-slack'
import * as Discord from './firebase-discord'
import * as Slack from './firebase-slack'
import * as Patreon from './patreon'

exports.notifyPinAwaitingModeration = Slack.notifyPinAwaitingModeration
exports.notifyHowtoAwaitingModeration = Slack.notifyHowtoAwaitingModeration

exports.notifyPinPublished = Discord.notifyPinPublished
exports.notifyHowtoPublished = Discord.notifyHowtoPublished
exports.notifyLibraryItemPublished = Discord.notifyLibraryItemPublished
exports.notifyQuestionPublished = Discord.notifyQuestionPublished
exports.notifyResearchUpdatePublished = Discord.notifyResearchUpdatePublished

37 changes: 20 additions & 17 deletions functions/src/aggregations/common.aggregations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { firestore } from 'firebase-admin'
import { Change, logger } from 'firebase-functions'
import { DB_ENDPOINTS } from '../models'
import { db } from '../Firebase/firestoreDB'
import { compareObjectDiffs, splitArrayToChunks } from '../Utils/data.utils'
import { FieldValue } from 'firebase-admin/firestore'
import { logger } from 'firebase-functions'
import { IModerationStatus } from 'oa-shared/models/moderation'
import { DBEndpoint } from 'oa-shared/models/db'

import { db } from '../Firebase/firestoreDB'
import { DB_ENDPOINTS } from '../models'
import { compareObjectDiffs, splitArrayToChunks } from '../Utils/data.utils'

import type { firestore } from 'firebase-admin'
import type { Change } from 'firebase-functions'
import type { DBEndpoint } from 'oa-shared/models/db'

type IDocumentRef = FirebaseFirestore.DocumentReference
type ICollectionRef = FirebaseFirestore.CollectionReference
@@ -130,17 +133,17 @@ export class AggregationHandler {

// Seed total useful aggregation
if (this.aggregation.targetDocId === 'users_totalUseful') {
const howtos = await db
.collection(DB_ENDPOINTS.howtos)
const library = await db
.collection(DB_ENDPOINTS.library)
.where('votedUsefulBy', '!=', [])
.where('moderation', '==', IModerationStatus.ACCEPTED)
.get()

logger.info(`${targetAggregation} Howtos - ${howtos.docs.length}`)
logger.info(`${targetAggregation} Library - ${library.docs.length}`)
const userUseful = {}
if (!howtos.empty) {
for (let i = 0; i < howtos.docs.length; i++) {
const data = howtos.docs[i].data()
if (!library.empty) {
for (let i = 0; i < library.docs.length; i++) {
const data = library.docs[i].data()
userUseful[data._createdBy] =
(userUseful[data._createdBy] || 0) + data.votedUsefulBy.length
}
@@ -242,18 +245,18 @@ export class AggregationHandler {
private async calculateTotalUseful(id: string) {
const userTotalUseful = {}

const howtos = await db
.collection(DB_ENDPOINTS.howtos)
const library = await db
.collection(DB_ENDPOINTS.library)
.where('_createdBy', '==', id)
.where('votedUsefulBy', '!=', [])
.where('moderation', '==', IModerationStatus.ACCEPTED)
.get()

let totalUseful = 0

if (!howtos.empty) {
for (let i = 0; i < howtos.docs.length; i++) {
const data = howtos.docs[i].data()
if (!library.empty) {
for (let i = 0; i < library.docs.length; i++) {
const data = library.docs[i].data()
totalUseful += data.votedUsefulBy.length
}
}
20 changes: 10 additions & 10 deletions functions/src/database/updates.ts
Original file line number Diff line number Diff line change
@@ -28,21 +28,21 @@ export const contentModifiedTimestamp = functions
`${logPrefix}Starting contentModifiedTimestamp Setting`,
)

const howtoUpdates = []
const libraryUpdates = []
const researchUpdates = []
const howtos = await db.collection(DB_ENDPOINTS.howtos).get()
const library = await db.collection(DB_ENDPOINTS.library).get()
const research = await db.collection(DB_ENDPOINTS.research).get()

if (!howtos.empty) {
// Get howto updates
howtos.forEach((ht) => {
if (!library.empty) {
// Get library updates
library.forEach((ht) => {
const data = ht.data()
howtoUpdates.push({
libraryUpdates.push({
id: ht.id,
_contentModifiedTimestamp: data._modified,
})
})
await batchGeneration(howtoUpdates, 'howtos', dryRun, logPrefix)
await batchGeneration(libraryUpdates, 'library', dryRun, logPrefix)
}

if (!research.empty) {
@@ -68,8 +68,8 @@ export const contentModifiedTimestamp = functions
_dryRun: dryRun,
operations,
meta: {
howtoUpdates: howtoUpdates,
researchUpdates: researchUpdates,
libraryUpdates,
researchUpdates,
},
}
} catch (error) {
@@ -83,7 +83,7 @@ export const contentModifiedTimestamp = functions

async function batchGeneration(
updateData: Record<string, any>[],
collection: 'howtos' | 'research',
collection: 'library' | 'research',
dryRun: boolean,
logPrefix: string,
) {
61 changes: 33 additions & 28 deletions functions/src/emailNotifications/createModerationEmails.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { FirebaseEmulatedTest } from '../test/Firebase/emulator'
import { IModerationStatus } from 'oa-shared'

import { getMockLibraryItem } from '../emulator/seed/content-generate'
import { DB_ENDPOINTS } from '../models'
import { FirebaseEmulatedTest } from '../test/Firebase/emulator'
import { PP_SIGNOFF } from './constants'
import {
createHowtoModerationEmail,
createMapPinModerationEmail,
handleModerationUpdate,
} from './createModerationEmails'
import {
HOW_TO_APPROVAL_SUBJECT,
HOW_TO_NEEDS_IMPROVEMENTS_SUBJECT,
@@ -10,15 +19,8 @@ import {
MAP_PIN_REJECTED_SUBJECT,
MAP_PIN_SUBMISSION_SUBJECT,
} from './templateHelpers'
import { getMockHowto } from '../emulator/seed/content-generate'
import {
createHowtoModerationEmail,
createMapPinModerationEmail,
handleModerationUpdate,
} from './createModerationEmails'
import { PP_SIGNOFF } from './constants'
import { IUserDB } from 'oa-shared/models/user'
import { IModerationStatus } from 'oa-shared'

import type { IUserDB } from 'oa-shared/models/user'

jest.mock('../Firebase/auth', () => ({
firebaseAuth: {
@@ -43,7 +45,7 @@ const userFactory = (_id: string, user: Partial<IUserDB> = {}): IUserDB =>
...user,
}) as IUserDB

describe('Create howto moderation emails', () => {
describe('Create library moderation emails', () => {
const db = FirebaseEmulatedTest.admin.firestore()

beforeEach(async () => {
@@ -62,17 +64,17 @@ describe('Create howto moderation emails', () => {
await FirebaseEmulatedTest.clearFirestoreDB()
})

it('Creates an email for an accepted howto', async () => {
const howtoApproved = getMockHowto('user_1')
const howtoAwaitingModeration = {
...howtoApproved,
it('Creates an email for an accepted library items', async () => {
const projectApproved = getMockLibraryItem('user_1')
const projectAwaitingModeration = {
...projectApproved,
moderation: IModerationStatus.AWAITING_MODERATION,
}
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoAwaitingModeration,
howtoApproved,
'howtos',
howtoApproved._id,
projectAwaitingModeration,
projectApproved,
'library',
projectApproved._id,
)

await handleModerationUpdate(change, createHowtoModerationEmail)
@@ -103,15 +105,18 @@ describe('Create howto moderation emails', () => {
})

it('Creates an email for a howto awaiting moderation', async () => {
const howtoRejected = getMockHowto('user_1', IModerationStatus.REJECTED)
const howtoRejected = getMockLibraryItem(
'user_1',
IModerationStatus.REJECTED,
)
const howtoAwaitingModeration = {
...howtoRejected,
moderation: IModerationStatus.AWAITING_MODERATION,
}
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoRejected,
howtoAwaitingModeration,
'howtos',
'library',
howtoAwaitingModeration._id,
)

@@ -143,7 +148,7 @@ describe('Create howto moderation emails', () => {
})

it('Creates an email for a rejected howto', async () => {
const howtoAwaitingModeration = getMockHowto(
const howtoAwaitingModeration = getMockLibraryItem(
'user_1',
IModerationStatus.AWAITING_MODERATION,
)
@@ -154,7 +159,7 @@ describe('Create howto moderation emails', () => {
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoAwaitingModeration,
howtoRejected,
'howtos',
'library',
howtoAwaitingModeration._id,
)

@@ -189,7 +194,7 @@ describe('Create howto moderation emails', () => {
})

it('Creates an email for a howto that needs improvements', async () => {
const howtoAwaitingModeration = getMockHowto(
const howtoAwaitingModeration = getMockLibraryItem(
'user_1',
IModerationStatus.AWAITING_MODERATION,
)
@@ -202,7 +207,7 @@ describe('Create howto moderation emails', () => {
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoAwaitingModeration,
howtoNeedsImprovements,
'howtos',
'library',
howtoAwaitingModeration._id,
)

@@ -238,16 +243,16 @@ describe('Create howto moderation emails', () => {
})
})

it('Does not create an email for non-approved howtos', async () => {
const howtoApproved = getMockHowto('user_1')
it('Does not create an email for non-approved library items', async () => {
const howtoApproved = getMockLibraryItem('user_1')
const howtoDraft = {
...howtoApproved,
moderation: IModerationStatus.DRAFT,
}
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoApproved,
howtoDraft,
'howtos',
'library',
howtoApproved._id,
)

2 changes: 1 addition & 1 deletion functions/src/emailNotifications/createModerationEmails.ts
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ export async function createMapPinModerationEmail(mapPin: IMapPin) {

export const handleHowToModerationUpdate = functions
.runWith({ memory: MEMORY_LIMIT_512_MB })
.firestore.document(`${DB_ENDPOINTS.howtos}/{id}`)
.firestore.document(`${DB_ENDPOINTS.library}/{id}`)
.onUpdate((change, context) =>
withErrorAlerting(context, handleModerationUpdate, [
change,
Loading

0 comments on commit 94c7264

Please sign in to comment.