Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Make avatar static-resource resolvers try/catches #9400

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/server-core/src/user/avatar/avatar.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,14 @@ const updateUserAvatars = async (context: HookContext<AvatarService>) => {

/**
* Hook used to check if request has any public avatar in data.
* @param context
*/
const isPublicAvatar = () => {
return (context: HookContext) => {
const data: AvatarType[] = Array.isArray(context.data) ? context.data : [context.data]

const hasPublic = data.find((item) => item.isPublic)

return hasPublic ? true : false
return !!hasPublic
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/server-core/src/user/avatar/avatar.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ export const avatarExternalResolver = resolve<AvatarType, HookContext>({
}),
modelResource: virtual(async (avatar, context) => {
if (context.event !== 'removed' && avatar.modelResourceId)
return context.app.service(staticResourcePath).get(avatar.modelResourceId)
try {
return await context.app.service(staticResourcePath).get(avatar.modelResourceId)
} catch (err) {
//Swallow missing resource errors, deal with them elsewhere
}
}),
thumbnailResource: virtual(async (avatar, context) => {
if (context.event !== 'removed' && avatar.thumbnailResourceId)
return context.app.service(staticResourcePath).get(avatar.thumbnailResourceId)
try {
return await context.app.service(staticResourcePath).get(avatar.thumbnailResourceId)
} catch (err) {
//Swallow missing resource errors, deal with them elsewhere
}
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ async function addIdentityProviderType(context: HookContext<IdentityProviderServ

async function createNewUser(context: HookContext<IdentityProviderService>) {
const isGuest = (context.actualData as IdentityProviderType).type === 'guest'
const avatars = await context.app.service(avatarPath).find({ isInternal: true, query: { $limit: 1000 } })
const avatars = await context.app
.service(avatarPath)
.find({ isInternal: true, query: { isPublic: true, skipUser: true, $limit: 1000 } })

const newUser = await context.app.service(userPath).create({
context.existingUser = await context.app.service(userPath).create({
isGuest,
avatarId: avatars.data[random(avatars.data.length - 1)].id
})

context.existingUser = newUser
}

/* (AFTER) CREATE HOOKS */
Expand Down