From 927816646fe25270e62613e224742ac2ff382225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cosmin=20P=C3=A2rvulescu?= Date: Fri, 1 Dec 2023 13:33:47 +0200 Subject: [PATCH] Added image deletion functionality --- platform/core/src/context.ts | 4 +-- .../src/jsonrpc/methods/patchProfileFields.ts | 33 +++++++++++++++++++ .../src/jsonrpc/methods/resetProfileFields.ts | 16 +++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/platform/core/src/context.ts b/platform/core/src/context.ts index 84090e6bda..e7bab5482f 100644 --- a/platform/core/src/context.ts +++ b/platform/core/src/context.ts @@ -75,7 +75,7 @@ export interface CreateInnerContextOptions */ export async function createContextInner( opts: CreateInnerContextOptions & { - waitUntil?: (promise: Promise) => void + waitUntil: (promise: Promise) => void } ) { const traceSpan = generateTraceSpan(opts.req?.headers) @@ -97,7 +97,7 @@ export async function createContextInner( */ export async function createContext( opts: FetchCreateContextFnOptions & { - waitUntil?: (promise: Promise) => void + waitUntil: (promise: Promise) => void }, env: Environment ) { diff --git a/platform/identity/src/jsonrpc/methods/patchProfileFields.ts b/platform/identity/src/jsonrpc/methods/patchProfileFields.ts index 8d5f653fe6..a169368170 100644 --- a/platform/identity/src/jsonrpc/methods/patchProfileFields.ts +++ b/platform/identity/src/jsonrpc/methods/patchProfileFields.ts @@ -3,6 +3,8 @@ import { Context } from '../../context' import { InternalServerError } from '@proofzero/errors' import { router } from '@proofzero/platform.core' import { IdentityURNSpace } from '@proofzero/urns/identity' +import createImageClient from '@proofzero/platform-clients/image' +import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace' export const PatchProfileFieldsInputSchema = z.object({ displayName: z.string().max(50).optional(), @@ -40,6 +42,25 @@ export const patchProfileFieldsMethod = async ({ }) } + if (profile.primaryAccountURN !== identityGraphNode.qc.primaryAccountURN) { + throw new InternalServerError({ + message: 'Primary account URN mismatch', + }) + } + + const primaryAccountURN = profile.primaryAccountURN + const accountNodeProfiles = await caller.account.getAccountProfileBatch([ + primaryAccountURN, + ]) + if (!accountNodeProfiles || accountNodeProfiles.length === 0) { + throw new InternalServerError({ + message: 'Primary account node not found', + }) + } + + const primaryAccountPicture = accountNodeProfiles[0].icon + const existingProfilePicture = profile.pfp?.image + if (input.displayName) { if (profile.displayName !== identityGraphNode.qc.name) { throw new InternalServerError({ @@ -78,4 +99,16 @@ export const patchProfileFieldsMethod = async ({ ), }), ]) + + if ( + existingProfilePicture && + existingProfilePicture !== profile.pfp?.image && + existingProfilePicture !== primaryAccountPicture + ) { + const imageClient = createImageClient(ctx.env.Images, { + headers: generateTraceContextHeaders(ctx.traceSpan), + }) + + ctx.waitUntil(imageClient.delete.mutate(existingProfilePicture)) + } } diff --git a/platform/identity/src/jsonrpc/methods/resetProfileFields.ts b/platform/identity/src/jsonrpc/methods/resetProfileFields.ts index 4ef88ba36b..931cb99286 100644 --- a/platform/identity/src/jsonrpc/methods/resetProfileFields.ts +++ b/platform/identity/src/jsonrpc/methods/resetProfileFields.ts @@ -2,6 +2,8 @@ import { Context } from '../../context' import { InternalServerError } from '@proofzero/errors' import { router } from '@proofzero/platform.core' import { IdentityURNSpace } from '@proofzero/urns/identity' +import createImageClient from '@proofzero/platform-clients/image' +import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace' export const resetProfileFieldsMethod = async ({ ctx, @@ -49,6 +51,9 @@ export const resetProfileFieldsMethod = async ({ const primaryAccountProfile = accountNodeProfiles[0] + const primaryAccountPicture = primaryAccountProfile.icon + const existingProfilePicture = profile.pfp?.image + profile.displayName = primaryAccountProfile.title identityGraphNode.qc.name = primaryAccountProfile.title @@ -74,4 +79,15 @@ export const resetProfileFieldsMethod = async ({ ), }), ]) + + if ( + existingProfilePicture && + existingProfilePicture !== primaryAccountPicture + ) { + const imageClient = createImageClient(ctx.env.Images, { + headers: generateTraceContextHeaders(ctx.traceSpan), + }) + + ctx.waitUntil(imageClient.delete.mutate(existingProfilePicture)) + } }