Skip to content

Commit

Permalink
Added image deletion functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin-Parvulescu committed Dec 1, 2023
1 parent 1921b92 commit 9278166
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions platform/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface CreateInnerContextOptions
*/
export async function createContextInner(
opts: CreateInnerContextOptions & {
waitUntil?: (promise: Promise<unknown>) => void
waitUntil: (promise: Promise<unknown>) => void
}
) {
const traceSpan = generateTraceSpan(opts.req?.headers)
Expand All @@ -97,7 +97,7 @@ export async function createContextInner(
*/
export async function createContext(
opts: FetchCreateContextFnOptions & {
waitUntil?: (promise: Promise<unknown>) => void
waitUntil: (promise: Promise<unknown>) => void
},
env: Environment
) {
Expand Down
33 changes: 33 additions & 0 deletions platform/identity/src/jsonrpc/methods/patchProfileFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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))
}
}
16 changes: 16 additions & 0 deletions platform/identity/src/jsonrpc/methods/resetProfileFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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))
}
}

0 comments on commit 9278166

Please sign in to comment.