1+ import { createFetchComponent } from '@well-known-components/fetch-component'
12import { CatalystClient , ContentClient , createContentClient } from 'dcl-catalyst-client'
23import { Authenticator , AuthIdentity } from '@dcl/crypto'
34import { Entity , EntityType } from '@dcl/schemas'
4- import { createFetchComponent } from '@well-known-components/ fetch-component '
5+ import cryptoFetch from 'decentraland-crypto- fetch'
56import { getAddress } from 'decentraland-dapps/dist/modules/wallet/selectors'
67import { buildEntity } from 'dcl-catalyst-client/dist/client/utils/DeploymentBuilder'
78import { takeLatest , put , select , call , take , all } from 'redux-saga/effects'
89import { config } from 'config'
910import { BuilderAPI , getEmptySceneUrl , getPreviewUrl } from 'lib/api/builder'
10- import { getData as getDeployments } from 'modules/deployment/selectors'
1111import { Deployment , SceneDefinition , Placement } from 'modules/deployment/types'
1212import { takeScreenshot } from 'modules/editor/actions'
1313import { fetchENSWorldStatusRequest } from 'modules/ens/actions'
@@ -24,6 +24,7 @@ import { Media } from 'modules/media/types'
2424import { getName } from 'modules/profile/selectors'
2525import { createFiles , EXPORT_PATH } from 'modules/project/export'
2626import { getCurrentProject , getData as getProjects } from 'modules/project/selectors'
27+ import { getData as getDeployments } from 'modules/deployment/selectors'
2728import { Project } from 'modules/project/types'
2829import { getSceneByProjectId } from 'modules/scene/utils'
2930import { Scene } from 'modules/scene/types'
@@ -59,9 +60,11 @@ import {
5960 fetchWorldDeploymentsRequest
6061} from './actions'
6162import { makeContentFiles } from './contentUtils'
62- import { getEmptyDeployment , getThumbnail , UNPUBLISHED_PROJECT_ID } from './utils'
63+ import { UNPUBLISHED_PROJECT_ID , getEmptyDeployment , getThumbnail } from './utils'
6364import { ProgressStage } from './types'
6465
66+ const getWorldsContentServerUrl = ( ) => config . get ( 'WORLDS_CONTENT_SERVER' , '' )
67+
6568type UnwrapPromise < T > = T extends PromiseLike < infer U > ? U : T
6669
6770// TODO: Remove this. This is using the store directly which it shouldn't and causes a circular dependency.
@@ -330,7 +333,7 @@ export function* deploymentSaga(builder: BuilderAPI, catalystClient: CatalystCli
330333 function * handleDeployToWorldRequest ( action : DeployToWorldRequestAction ) {
331334 const { world, projectId } = action . payload
332335 const contentClient = createContentClient ( {
333- url : config . get ( 'WORLDS_CONTENT_SERVER' , '' ) ,
336+ url : getWorldsContentServerUrl ( ) ,
334337 fetcher : createFetchComponent ( )
335338 } )
336339 try {
@@ -364,57 +367,60 @@ export function* deploymentSaga(builder: BuilderAPI, catalystClient: CatalystCli
364367 function * handleClearDeploymentRequest ( action : ClearDeploymentRequestAction ) {
365368 const { deploymentId } = action . payload
366369
367- const deployments : ReturnType < typeof getDeployments > = yield select ( getDeployments )
368- const deployment = deployments [ deploymentId ]
369- if ( ! deployment ) {
370- yield put ( deployToLandFailure ( 'Unable to Publish: Invalid deployment' ) )
371- return
372- }
370+ try {
371+ const deployments : ReturnType < typeof getDeployments > = yield select ( getDeployments )
372+ const deployment = deployments [ deploymentId ]
373373
374- let contentClient : ContentClient
375- if ( deployment . world ) {
376- contentClient = createContentClient ( {
377- url : config . get ( 'WORLDS_CONTENT_SERVER' , '' ) ,
378- fetcher : createFetchComponent ( )
379- } )
380- } else {
381- contentClient = yield call ( [ catalystClient , 'getContentClient' ] )
382- }
374+ if ( ! deployment ) {
375+ throw new Error ( 'Unable to clear deployment: Invalid deployment' )
376+ }
383377
384- const identity : AuthIdentity = yield getIdentity ( )
385- if ( ! identity ) {
386- yield put ( deployToLandFailure ( 'Unable to Publish: Invalid identity' ) )
387- return
388- }
378+ const identity : AuthIdentity = yield call ( getIdentity )
379+
380+ if ( ! identity ) {
381+ throw new Error ( 'Unable to clear deployment: Invalid identity' )
382+ }
383+
384+ if ( deployment . world ) {
385+ const response : Response = yield call ( cryptoFetch , `${ getWorldsContentServerUrl ( ) } /entities/${ deployment . world } ` , {
386+ method : 'DELETE' ,
387+ identity
388+ } )
389+
390+ if ( ! response . ok ) {
391+ throw new Error ( `Unable to clear deployment: Response is not ok, status ${ response . status } ` )
392+ }
393+ } else {
394+ const contentClient : ContentClient = yield call ( [ catalystClient , 'getContentClient' ] )
395+ const { placement } = deployment
396+ const [ emptyProject , emptyScene ] = getEmptyDeployment ( deployment . projectId || UNPUBLISHED_PROJECT_ID )
397+ const files : UnwrapPromise < ReturnType < typeof createFiles > > = yield call ( createFiles , {
398+ project : emptyProject ,
399+ scene : emptyScene ,
400+ point : placement . point ,
401+ rotation : placement . rotation ,
402+ thumbnail : getEmptySceneUrl ( ) ,
403+ author : null ,
404+ isDeploy : true ,
405+ isEmpty : true ,
406+ onProgress : handleProgress ( ProgressStage . CREATE_FILES ) ,
407+ world : deployment . world ?? undefined
408+ } )
409+ const contentFiles : Map < string , Buffer > = yield call ( makeContentFiles , files )
410+ const sceneDefinition : SceneDefinition = JSON . parse ( files [ EXPORT_PATH . SCENE_FILE ] )
411+ const { entityId, files : hashedFiles } = yield call ( buildEntity , {
412+ type : EntityType . SCENE ,
413+ pointers : [ ...sceneDefinition . scene . parcels ] ,
414+ metadata : sceneDefinition ,
415+ files : contentFiles
416+ } )
417+ const authChain = Authenticator . signPayload ( identity , entityId )
418+ yield call ( [ contentClient , 'deploy' ] , { entityId, files : hashedFiles , authChain } )
419+ }
389420
390- try {
391- const { placement } = deployment
392- const [ emptyProject , emptyScene ] = getEmptyDeployment ( deployment . projectId || UNPUBLISHED_PROJECT_ID )
393- const files : UnwrapPromise < ReturnType < typeof createFiles > > = yield call ( createFiles , {
394- project : emptyProject ,
395- scene : emptyScene ,
396- point : placement . point ,
397- rotation : placement . rotation ,
398- thumbnail : getEmptySceneUrl ( ) ,
399- author : null ,
400- isDeploy : true ,
401- isEmpty : true ,
402- onProgress : handleProgress ( ProgressStage . CREATE_FILES ) ,
403- world : deployment . world ?? undefined
404- } )
405- const contentFiles : Map < string , Buffer > = yield call ( makeContentFiles , files )
406- const sceneDefinition : SceneDefinition = JSON . parse ( files [ EXPORT_PATH . SCENE_FILE ] )
407- const { entityId, files : hashedFiles } = yield call ( buildEntity , {
408- type : EntityType . SCENE ,
409- pointers : [ ...sceneDefinition . scene . parcels ] ,
410- metadata : sceneDefinition ,
411- files : contentFiles
412- } )
413- const authChain = Authenticator . signPayload ( identity , entityId )
414- yield call ( [ contentClient , 'deploy' ] , { entityId, files : hashedFiles , authChain } )
415421 yield put ( clearDeploymentSuccess ( deploymentId ) )
416- } catch ( error ) {
417- yield put ( clearDeploymentFailure ( deploymentId , error . message ) )
422+ } catch ( e ) {
423+ yield put ( clearDeploymentFailure ( deploymentId , e . message ) )
418424 }
419425 }
420426
@@ -502,7 +508,7 @@ export function* deploymentSaga(builder: BuilderAPI, catalystClient: CatalystCli
502508
503509 function * handleFetchWorldDeploymentsRequest ( action : FetchWorldDeploymentsRequestAction ) {
504510 const { worlds } = action . payload
505- const worldContentClient = createContentClient ( { url : config . get ( 'WORLDS_CONTENT_SERVER' , '' ) , fetcher : createFetchComponent ( ) } )
511+ const worldContentClient = createContentClient ( { url : getWorldsContentServerUrl ( ) , fetcher : createFetchComponent ( ) } )
506512 try {
507513 const entities : Entity [ ] = [ ]
508514
0 commit comments