Skip to content

Commit

Permalink
Ensured new users do not get avatars missing resources (#9407)
Browse files Browse the repository at this point in the history
Added logic to createNewUser to check that the avatar randomly
selected for a new user has a valid model and thumbnail resource,
and remove that avatar from the list to choose from if either
is missing.

Co-authored-by: HexaField <joshfield999@gmail.com>
  • Loading branch information
barankyle and HexaField committed Dec 11, 2023
1 parent 2ee2f58 commit 6b7cb60
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import appConfig from '../../appconfig'

import { isDev } from '@etherealengine/common/src/config'
import { checkScope } from '@etherealengine/engine/src/common/functions/checkScope'
import { staticResourcePath } from '@etherealengine/engine/src/schemas/media/static-resource.schema'
import { scopeTypePath } from '@etherealengine/engine/src/schemas/scope/scope-type.schema'
import { ScopeType, scopePath } from '@etherealengine/engine/src/schemas/scope/scope.schema'
import { avatarPath } from '@etherealengine/engine/src/schemas/user/avatar.schema'
Expand Down Expand Up @@ -151,9 +152,26 @@ async function createNewUser(context: HookContext<IdentityProviderService>) {
.service(avatarPath)
.find({ isInternal: true, query: { isPublic: true, skipUser: true, $limit: 1000 } })

let selectedAvatarId
while (selectedAvatarId == null) {
const randomId = random(avatars.data.length - 1)
const selectedAvatar = avatars.data[randomId]
try {
await Promise.all([
context.app.service(staticResourcePath).get(selectedAvatar.modelResourceId),
context.app.service(staticResourcePath).get(selectedAvatar.thumbnailResourceId)
])
selectedAvatarId = selectedAvatar.id
} catch (err) {
console.log('error in getting resources')
avatars.data.splice(randomId, 1)
if (avatars.data.length < 1) throw new Error('All avatars are missing static resources')
}
}

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

Expand Down

0 comments on commit 6b7cb60

Please sign in to comment.