'use strict' ; import { Config } from '@Base/config' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , accountFromAuthToken , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { Domains } from '@Entities/Domains' ; import { Places } from '@Entities/Places' ; import { buildDomainInfo , buildPlaceInfo } from '@Route-Tools/Util' ; import { uniqueNamesGenerator , adjectives , colors , animals } from 'unique-names-generator' ; import { Config as UniqueNamesConfig } from 'unique-names-generator' ; import { GenUUID } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; const procPostDomainsTemporary = async ( req , resp , next ) => { if ( Config [ 'metaverse-server' ] [ 'allow-temp-domain-creation' ] ) { const customConfig = { dictionaries : [ colors , animals ] , separator : '-' , length : 2 } ; const generatedPlacename = uniqueNamesGenerator ( customConfig ) ; const generatedAPIkey = GenUUID ( ) ; const newDomain = Domains . createDomain ( ) ; newDomain . name = generatedPlacename ; newDomain . apiKey = generatedAPIkey ; if ( req . vSenderKey ) { newDomain . iPAddrOfFirstContact = req . vSenderKey ; } ; if ( req . vAuthToken ) { Logger . debug ( `procPostDomainsTemporary: associating account ${ req . vAuthToken . accountId } with new domain ${ newDomain . id }` ) ; newDomain . sponsorAccountId = req . vAuthToken . accountId ; } ; const newPlacename = await Places . uniqifyPlaceName ( newDomain . name ) ; const newPlace = await Places . createPlace ( newDomain . sponsorAccountId ) ; newPlace . domainId = newDomain . id ; newPlace . name = newPlacename ; newPlace . description = 'A place in ' + newDomain . name ; newPlace . maturity = newDomain . maturity ; newPlace . iPAddrOfFirstContact = req . vSenderKey ; await Domains . addDomain ( newDomain ) ; await Places . addPlace ( newPlace ) ; Logger . info ( `procPostDomainsTemporary: creating temporary domain "${ newDomain . name }" and place "${ newPlace . name }"` ) ; req . vRestResp . Data = { 'domain' : await buildDomainInfo ( newDomain ) , 'place' : await buildPlaceInfo ( newPlace , newDomain ) } ; req . vRestResp . Data . domain . api_key = newDomain . apiKey ; } else { req . vRestResp . respondFailure ( 'domain temporary names not available' ) ; } next ( ) ; } ; export const name = '/api/v1/domains/temporary' ; export const router = Router ( ) ; router . post ( '/api/v1/domains/temporary' , [ setupMetaverseAPI , accountFromAuthToken , procPostDomainsTemporary , finishMetaverseAPI ] ) ;