This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(users): users profile pictures
#77
Open
Blckbrry-Pi
wants to merge
3
commits into
03-29-feat_create_uploads_module
Choose a base branch
from
03-31-feat_users_users_profile_pictures
base: 03-29-feat_create_uploads_module
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
| export interface Config { | ||
| maxProfilePictureBytes: number; | ||
| allowedMimes?: string[]; | ||
| } | ||
|
|
||
| export const DEFAULT_MIME_TYPES = [ | ||
| "image/jpeg", | ||
| "image/png", | ||
| ]; | ||
2 changes: 2 additions & 0 deletions
2
modules/users/db/migrations/20240522003232_initial_setup/migration.sql
This file contains hidden or 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,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "User" ADD COLUMN "avatarUploadId" UUID; |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ datasource db { | |
| } | ||
|
|
||
| model User { | ||
| id String @id @default(uuid()) @db.Uuid | ||
| username String @unique | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
| id String @id @default(uuid()) @db.Uuid | ||
| username String @unique | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
|
|
||
| avatarUploadId String? @db.Uuid | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. profilePictureUploadId |
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| import { ScriptContext, RuntimeError } from "../module.gen.ts"; | ||
| import { DEFAULT_MIME_TYPES } from "../config.ts"; | ||
|
|
||
| export interface Request { | ||
| mime: string; | ||
| contentLength: string; | ||
| userToken: string; | ||
| } | ||
|
|
||
| export interface Response { | ||
| url: string; | ||
| uploadId: string; | ||
| } | ||
|
|
||
| export async function run( | ||
| ctx: ScriptContext, | ||
| req: Request, | ||
| ): Promise<Response> { | ||
| // Authenticate/rate limit because this is a public route | ||
| await ctx.modules.rateLimit.throttlePublic({ period: 60, requests: 5 }); | ||
| const { userId } = await ctx.modules.users.authenticateUser({ userToken: req.userToken }); | ||
|
|
||
| // Ensure at least the MIME type says it is an image | ||
| const allowedMimes = ctx.userConfig.allowedMimes ?? DEFAULT_MIME_TYPES; | ||
| if (!allowedMimes.includes(req.mime)) { | ||
| throw new RuntimeError( | ||
| "invalid_mime_type", | ||
| { cause: `MIME type ${req.mime} is not an allowed image type` }, | ||
| ); | ||
| } | ||
|
|
||
| // Ensure the file is within the maximum configured size for a PFP | ||
| if (BigInt(req.contentLength) > ctx.userConfig.maxProfilePictureBytes) { | ||
| throw new RuntimeError( | ||
| "file_too_large", | ||
| { cause: `File is too large (${req.contentLength} bytes)` }, | ||
| ); | ||
| } | ||
|
|
||
| // Prepare the upload to get the presigned URL | ||
| const { upload: presigned } = await ctx.modules.uploads.prepare({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs a tag: |
||
| files: [ | ||
| { | ||
| path: `profile-picture`, | ||
| contentLength: req.contentLength, | ||
| mime: req.mime, | ||
| multipart: false, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| return { | ||
| url: presigned.files[0].presignedChunks[0].url, | ||
| uploadId: presigned.id, | ||
| } | ||
| } | ||
This file contains hidden or 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,74 @@ | ||
| import { ScriptContext, RuntimeError } from "../module.gen.ts"; | ||
| import { User } from "../utils/types.ts"; | ||
| import { withPfpUrls } from "../utils/pfp.ts"; | ||
|
|
||
| export interface Request { | ||
| uploadId: string; | ||
| userToken: string; | ||
| } | ||
|
|
||
| export interface Response { | ||
| user: User; | ||
| } | ||
|
|
||
| export async function run( | ||
| ctx: ScriptContext, | ||
| req: Request, | ||
| ): Promise<Response> { | ||
| // Authenticate/rate limit because this is a public route | ||
| await ctx.modules.rateLimit.throttlePublic({ period: 60, requests: 5 }); | ||
| const { userId } = await ctx.modules.users.authenticateUser({ userToken: req.userToken }); | ||
|
|
||
| // Complete the upload in the `uploads` module | ||
| await ctx.modules.uploads.complete({ uploadId: req.uploadId }); | ||
|
|
||
| // Delete the old uploaded profile picture and replace it with the new one | ||
| const [user] = await ctx.db.$transaction(async (db) => { | ||
| // If there is an existing profile picture, delete it | ||
| const oldUser = await db.user.findFirst({ | ||
| where: { id: userId }, | ||
| }); | ||
|
|
||
| // (This means that `users.authenticateUser` is broken!) | ||
| if (!oldUser) { | ||
| throw new RuntimeError( | ||
| "internal_error", | ||
| { | ||
| meta: "Existing user not found", | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| if (oldUser.avatarUploadId) { | ||
| await ctx.modules.uploads.delete({ uploadId: oldUser.avatarUploadId }); | ||
| } | ||
|
|
||
| // Update the user upload ID | ||
| const user = await db.user.update({ | ||
| where: { | ||
| id: userId, | ||
| }, | ||
| data: { | ||
| avatarUploadId: req.uploadId, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| username: true, | ||
| avatarUploadId: true, | ||
| createdAt: true, | ||
| updatedAt: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!user) { | ||
| throw new RuntimeError("internal_error", { cause: "User not found" }); | ||
| } | ||
|
|
||
| return await withPfpUrls( | ||
| ctx, | ||
| [user], | ||
| ); | ||
| }); | ||
|
|
||
| return { user }; | ||
| } |
This file contains hidden or 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,45 @@ | ||
| import { test, TestContext } from "../module.gen.ts"; | ||
| import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts"; | ||
| import { assertEquals } from "https://deno.land/std@0.217.0/assert/assert_equals.ts"; | ||
| import { assertExists } from "https://deno.land/std@0.217.0/assert/assert_exists.ts"; | ||
|
|
||
| test("e2e", async (ctx: TestContext) => { | ||
| const imageReq = await fetch("https://picsum.photos/200/300"); | ||
| const imageData = new Uint8Array(await imageReq.arrayBuffer()); | ||
|
|
||
|
|
||
| const { user } = await ctx.modules.users.createUser({ | ||
| username: faker.internet.userName(), | ||
| }); | ||
|
|
||
| const { token } = await ctx.modules.users.createUserToken({ | ||
| userId: user.id, | ||
| }); | ||
|
|
||
| const { url, uploadId } = await ctx.modules.users.prepareProfilePicture({ | ||
| mime: imageReq.headers.get("Content-Type") ?? "image/jpeg", | ||
| contentLength: imageData.length.toString(), | ||
| userToken: token.token, | ||
| }); | ||
|
|
||
| // Upload the profile picture | ||
| await fetch(url, { | ||
| method: "PUT", | ||
| body: imageData, | ||
| }); | ||
|
|
||
| // Set the profile picture | ||
| await ctx.modules.users.setProfilePicture({ | ||
| uploadId, | ||
| userToken: token.token, | ||
| }); | ||
|
|
||
| // Get PFP from URL | ||
| const { users: [{ profilePictureUrl }] } = await ctx.modules.users.getUser({ userIds: [user.id] }); | ||
| assertExists(profilePictureUrl); | ||
|
|
||
| // Get PFP from URL | ||
| const getPfpFromUrl = await fetch(profilePictureUrl); | ||
| const pfp = new Uint8Array(await getPfpFromUrl.arrayBuffer()); | ||
| assertEquals(pfp, imageData); | ||
| }); |
This file contains hidden or 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,34 @@ | ||
| import { ModuleContext } from "../module.gen.ts"; | ||
| import { User } from "./types.ts"; | ||
|
|
||
| const EXPIRY_SECS = 60 * 60 * 24; // 1 day | ||
|
|
||
| type UserWithUploadidInfo = Omit<User, "profilePictureUrl"> & { avatarUploadId: string | null }; | ||
|
|
||
| export async function withPfpUrls<T extends ModuleContext>( | ||
| ctx: T, | ||
| users: UserWithUploadidInfo[], | ||
| ): Promise<User[]> { | ||
| const fileRefs = users | ||
| .filter(user => user.avatarUploadId) | ||
| .map(user => ({ uploadId: user.avatarUploadId!, path: "profile-picture" })); | ||
|
|
||
| const { files } = await ctx.modules.uploads.getPublicFileUrls({ | ||
| files: fileRefs, | ||
| expirySeconds: EXPIRY_SECS, | ||
| }); | ||
|
|
||
| const map = new Map(files.map((file) => [file.uploadId, file.url])); | ||
|
|
||
| const completeUsers: User[] = []; | ||
| for (const user of users) { | ||
| if (user.avatarUploadId && map.has(user.avatarUploadId)) { | ||
| const profilePictureUrl = map.get(user.avatarUploadId)!; | ||
| completeUsers.push({ ...user, profilePictureUrl }); | ||
| } else { | ||
| completeUsers.push({ ...user, profilePictureUrl: null }); | ||
| } | ||
| } | ||
|
|
||
| return completeUsers; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ export interface User { | |
| username: string; | ||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| profilePictureUrl: string | null; | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.