-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from fmasa/character-duplication
Character duplication using cloud functions
- Loading branch information
Showing
12 changed files
with
226 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Bucket, CopyResponse, UploadResponse} from "@google-cloud/storage"; | ||
|
||
export const generateAvatarUrl = async (response: UploadResponse|CopyResponse, bucket: Bucket): Promise<string> => { | ||
if ("FIREBASE_STORAGE_EMULATOR_HOST" in process.env) { | ||
const [metadata] = await response[0].getMetadata(); | ||
|
||
return metadata.mediaLink; | ||
} | ||
|
||
return "https://firebasestorage.googleapis.com/v0/b/" | ||
+ bucket.name | ||
+ "/o/" | ||
+ encodeURIComponent(response[0].name) | ||
+ "?alt=media" | ||
+ "&v=" | ||
+ (+new Date()); | ||
} | ||
|
||
export const getAvatarPath = (partyId: string, characterId: string): string => `images/parties/${partyId}/characters/${characterId}.webp`; | ||
|
||
export const METADATA = { | ||
contentType: "image/webp", | ||
cacheControl: `max-age=${365 * 24 * 60 * 60}`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as t from "io-ts"; | ||
import {isLeft} from "fp-ts/Either"; | ||
import * as functions from "firebase-functions"; | ||
import {hasAccessToCharacter} from "./acl"; | ||
import {firestore} from "firebase-admin"; | ||
|
||
const RequiredFields = t.type({ | ||
partyId: t.string, | ||
characterId: t.string, | ||
}) | ||
|
||
type RequiredProps = (typeof RequiredFields)['props']; | ||
type RequestBody<T> = T extends t.TypeC<any> ? (T['props'] extends RequiredProps ? T : never) : never; | ||
|
||
export const characterChange = <T>( | ||
requestBodyCodec: RequestBody<T>, | ||
handler: (body: t.TypeOf<RequestBody<T>>, character: firestore.DocumentReference) => Promise<any>, | ||
) => { | ||
return functions.https.onCall(async (data, context) => { | ||
const body = requestBodyCodec.decode(data); | ||
|
||
if (isLeft(body)) { | ||
return { | ||
status: "error", | ||
error: 400, | ||
message: "Invalid request body", | ||
}; | ||
} | ||
|
||
const userId = context.auth?.uid; | ||
const {characterId, partyId} = body.right; | ||
|
||
if (userId === undefined) { | ||
return { | ||
status: "error", | ||
error: 401, | ||
message: "User is not authorized", | ||
}; | ||
} | ||
|
||
if (!await hasAccessToCharacter(userId, partyId, characterId)) { | ||
return { | ||
status: "error", | ||
error: 403, | ||
message: "User does not have access to given character", | ||
}; | ||
} | ||
|
||
const character = firestore().doc(`parties/${partyId}/characters/${characterId}`); | ||
|
||
if (!(await character.get()).exists) { | ||
return { | ||
status: "error", | ||
error: 404, | ||
message: "Character does not exist", | ||
} | ||
} | ||
|
||
return handler(body.right, character); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.