Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename all library functions #4073

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"globby",
"gsutil",
"hakkens",
"howto",
"howtos",
"indexeddb",
"kamp",
"mappins",
Expand Down
2 changes: 1 addition & 1 deletion .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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': '' },
Expand Down
2 changes: 1 addition & 1 deletion BOUNTIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

---
Expand Down
4 changes: 2 additions & 2 deletions docs/supabase.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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
Expand All @@ -61,7 +61,7 @@ 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

Expand Down
2 changes: 1 addition & 1 deletion functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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)
Expand Down
2 changes: 1 addition & 1 deletion functions/src/Integrations/firebase-discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
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

Expand Down
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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
20 changes: 10 additions & 10 deletions functions/src/database/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -68,8 +68,8 @@ export const contentModifiedTimestamp = functions
_dryRun: dryRun,
operations,
meta: {
howtoUpdates: howtoUpdates,
researchUpdates: researchUpdates,
libraryUpdates,
researchUpdates,
},
}
} catch (error) {
Expand All @@ -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,
) {
Expand Down
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,
Expand All @@ -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: {
Expand All @@ -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 () => {
Expand All @@ -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)
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)
Expand All @@ -154,7 +159,7 @@ describe('Create howto moderation emails', () => {
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoAwaitingModeration,
howtoRejected,
'howtos',
'library',
howtoAwaitingModeration._id,
)

Expand Down Expand Up @@ -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,
)
Expand All @@ -202,7 +207,7 @@ describe('Create howto moderation emails', () => {
const change = FirebaseEmulatedTest.mockFirestoreChangeObject(
howtoAwaitingModeration,
howtoNeedsImprovements,
'howtos',
'library',
howtoAwaitingModeration._id,
)

Expand Down Expand Up @@ -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,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const handleMessageSubmission = functions

export const handleHowToSubmission = functions
.runWith({ memory: '512MB' })
.firestore.document(`${DB_ENDPOINTS.howtos}/{id}`)
.firestore.document(`${DB_ENDPOINTS.library}/{id}`)
.onCreate((snapshot, context) =>
withErrorAlerting(context, createHowtoSubmissionEmail, [snapshot.data()]),
)
Expand Down
Loading
Loading