Skip to content

Commit

Permalink
fix(contribute command): listen to user doc changes instead of fetchi…
Browse files Browse the repository at this point in the history
…ng doc to check if exists

There can be some lag after running the auth command before the user can be found in firebase. In
which case trying to fetch the user will fail with `FirebaseError: Unable to retrieve the
authenticated user.`. Instead we can listen and wait for user doc changes.

privacy-scaling-explorations#220
  • Loading branch information
sripwoud committed Oct 30, 2023
1 parent 8bb9489 commit 4fd942f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
21 changes: 21 additions & 0 deletions packages/actions/src/helpers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Firestore,
getDoc,
getDocs,
onSnapshot,
query,
QueryConstraint,
QueryDocumentSnapshot,
Expand Down Expand Up @@ -123,6 +124,26 @@ export const getDocumentById = async (
return getDoc(docRef)
}

export const waitForUserDocumentToExist = (firestoreDatabase: Firestore, collection: string, documentId: string) => {
return new Promise<void>((resolve, reject) => {
const docRef = doc(firestoreDatabase, collection, documentId)

const unsubscribe = onSnapshot(
docRef,
(docSnapshot) => {
if (docSnapshot.exists()) {
unsubscribe()
resolve()
}
},
(error) => {
unsubscribe()
reject(error)
}
)
})
}

/**
* Query for opened ceremonies.
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export {
getContributionsCollectionPath,
getTimeoutsCollectionPath,
getOpenedCeremonies,
getCeremonyCircuits
getCeremonyCircuits,
waitForUserDocumentToExist
} from "./helpers/database"
export {
compareCeremonyArtifacts,
Expand Down
13 changes: 3 additions & 10 deletions packages/phase2cli/src/commands/contribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
FirebaseDocumentInfo,
generateValidContributionsAttestation,
commonTerms,
convertToDoubleDigits
convertToDoubleDigits,
waitForUserDocumentToExist
} from "@p0tion/actions"
import { DocumentSnapshot, DocumentData, Firestore, onSnapshot, Timestamp } from "firebase/firestore"
import { Functions } from "firebase/functions"
Expand Down Expand Up @@ -947,15 +948,7 @@ const contribute = async (opt: any) => {
const spinner = customSpinner(`Verifying your participant status...`, `clock`)
spinner.start()

// Check that the user's document is created
const userDoc = await getDocumentById(firestoreDatabase, commonTerms.collections.users.name, user.uid)
const userData = userDoc.data()
if (!userData) {
spinner.fail(
`Unfortunately we could not find a user document with your information. This likely means that you did not pass the GitHub reputation checks and therefore are not elegible to contribute to any ceremony. If you believe you pass the requirements, it might be possible that your profile is private and we were not able to fetch your real statistics, in this case please consider making your profile public for the duration of the contribution. Please contact the coordinator if you believe this to be an error.`
)
process.exit(0)
}
await waitForUserDocumentToExist(firestoreDatabase, commonTerms.collections.users.name, user.uid)

// Check the user's current participant readiness for contribution status (eligible, already contributed, timed out).
const canParticipantContributeToCeremony = await checkParticipantForCeremony(firebaseFunctions, selectedCeremony.id)
Expand Down

0 comments on commit 4fd942f

Please sign in to comment.