Skip to content

Commit

Permalink
feat(sharing): Updated functions for editors
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbenton committed Apr 25, 2024
1 parent 7016ae8 commit 0d60e40
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
6 changes: 5 additions & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ service cloud.firestore {
match /collections/{homebrewId} {
allow read: if request.auth.uid != null;
allow create: if request.auth.uid != null;
allow write: if request.auth.uid in resource.data.editors;
allow write: if request.auth.uid in resource.data.editors && !request.resource.data.diff(resource.data).affectedKeys().hasAny(["creator", "editors"]);

}

match /stats/{statId} {
Expand Down Expand Up @@ -179,6 +180,9 @@ service cloud.firestore {
allow create: if request.auth.uid != null;
allow write: if checkIsHomebrewOwner(resource.data.collectionId);
}
match /editorInviteKeys/{inviteKeyId} {
allow read, write: if false;
}
}
match /users/{userId} {
allow read: if true;
Expand Down
10 changes: 7 additions & 3 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ export const getHomebrewEditorInviteKey = onCall<

if (linkDocuments.length === 0) {
logger.info("No link documents found, creating one.");
getFirestore().collection("/homebrew/homebrew/editorInviteKeys").add({
collectionId: homebrewCollectionId,
});
const doc = await getFirestore()
.collection("/homebrew/homebrew/editorInviteKeys")
.add({
collectionId: homebrewCollectionId,
});

return doc.id;
}

return linkDocuments[0].id;
Expand Down
23 changes: 23 additions & 0 deletions src/api-calls/homebrew/editorFunction/acceptEditorInvite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { functions } from "config/firebase.config";
import { httpsCallable } from "firebase/functions";

export function acceptEditorInvite(
homebrewCollectionId: string,
inviteKey: string
): Promise<boolean> {
return new Promise((resolve, reject) => {
const acceptInvite = httpsCallable(
functions,
"addCurrentUserAsHomebrewCampaignEditor"
);

acceptInvite({ homebrewCollectionId, inviteKey })
.then((wasSuccessful) => {
resolve(wasSuccessful.data as boolean);
})
.catch((e) => {
console.error(e);
reject(e);
});
});
}
24 changes: 24 additions & 0 deletions src/api-calls/homebrew/editorFunction/getEditorInviteUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { functions } from "config/firebase.config";
import { httpsCallable } from "firebase/functions";

export function getEditorInviteUrl(
homebrewCollectionId: string
): Promise<string | null> {
return new Promise((resolve, reject) => {
const getInviteKey = httpsCallable(functions, "getHomebrewEditorInviteKey");

getInviteKey({ homebrewCollectionId })
.then((inviteKey) => {
if (inviteKey.data) {
resolve(new URL(`/homebrew/invite/${inviteKey.data}`).toString());
} else {
console.error("NO INVITE KEY RETURNED");
reject(new Error("No invite key was returned"));
}
})
.catch((e) => {
console.error(e);
reject(e);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { functions } from "config/firebase.config";
import { httpsCallable } from "firebase/functions";

export function getHomebrewCollectionFromInviteUrl(): Promise<string | null> {
return new Promise((resolve, reject) => {
const inviteKey = location.pathname.substring(
location.pathname.lastIndexOf("/") + 1
);

const getHomebrewId = httpsCallable(
functions,
"getHomebrewIdFromInviteKey"
);

getHomebrewId({ inviteKey })
.then((homebrewId) => {
return homebrewId.data;
})
.catch((e) => {
console.error(e);
reject(e);
});
});
}
3 changes: 3 additions & 0 deletions src/config/firebase.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FirebaseOptions, initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
import { getFunctions } from "firebase/functions";
import { getSystem } from "hooks/useGameSystem";
import { GAME_SYSTEMS, GameSystemChooser } from "types/GameSystems.type";

Expand Down Expand Up @@ -36,3 +37,5 @@ export const firebaseAuth = getAuth(firebaseApp);
export const firestore = getFirestore(firebaseApp);

export const storage = getStorage(firebaseApp);

export const functions = getFunctions(firebaseApp);

0 comments on commit 0d60e40

Please sign in to comment.