'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI , domainAPIkeyFromBody , verifyDomainAccess } from '@Route-Tools/middleware' ; import { HTTPStatusCode } from '@Route-Tools/RESTResponse' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import { domainFromParams } from '@Route-Tools/middleware' ; import { Perm } from '@Route-Tools/Perm' ; import { checkAccessToEntity } from '@Route-Tools/Permissions' ; import { buildDomainInfoV1 } from '@Route-Tools/Util' ; import { Domains } from '@Entities/Domains' ; import { Accounts } from '@Entities/Accounts' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; import Config from '@Base/config' ; import { IsNullOrEmpty } from '@Tools/Misc' ; import { updateObjectFields } from '@Tools/Db' ; const procGetDomainsDomainid = async ( req , resp , next ) => { if ( req . vDomain ) { const aDomain = req . vDomain ; const domainInfo = await buildDomainInfoV1 ( aDomain ) ; domainInfo . id = domainInfo . domainId ; req . vRestResp . Data = { 'domain' : domainInfo } ; req . vRestResp . addAdditionalField ( 'domain' , domainInfo ) ; } else { req . vRestResp . respondFailure ( req . vDomainError ?? 'Domain not found' ) ; req . vRestResp . HTTPStatus = HTTPStatusCode . Unauthorized ; } ; next ( ) ; } ; const procPutDomains = async ( req , resp , next ) => { if ( req . vDomain ) { if ( await checkAccessToEntity ( req . vAuthToken , req . vDomain , [ Perm . DOMAIN , Perm . SPONSOR , Perm . ADMIN ] ) ) { const updated = { } ; let valuesToSet = req . body . domain ; if ( valuesToSet ) { for ( const field of [ 'version' , 'protocol' , 'network_address' , 'network_port' , 'automatic_networking' , 'restricted' , 'capacity' , 'description' , 'maturity' , 'restriction' , 'managers' , 'tags' ] ) { if ( valuesToSet . hasOwnProperty ( field ) ) { await Domains . setField ( req . vAuthToken , req . vDomain , field , valuesToSet [ field ] , req . vAuthAccount , updated ) ; } ; } ; if ( valuesToSet . hasOwnProperty ( 'heartbeat' ) ) { await Domains . setField ( req . vAuthToken , req . vDomain , 'num_users' , valuesToSet . heartbeat . num_users , req . vAuthAccount , updated ) ; await Domains . setField ( req . vAuthToken , req . vDomain , 'num_anon_users' , valuesToSet . heartbeat . num_anon_users , req . vAuthAccount , updated ) ; } ; if ( valuesToSet . meta ) { valuesToSet = valuesToSet . meta ; for ( const field of [ 'capacity' , 'contact_info' , 'description' , 'managers' , 'tags' , 'images' , 'maturity' , 'restriction' , 'thumbnail' , 'world_name' ] ) { if ( valuesToSet . hasOwnProperty ( field ) ) { await Domains . setField ( req . vAuthToken , req . vDomain , field , valuesToSet [ field ] , req . vAuthAccount , updated ) ; } ; } ; } ; updated . timeOfLastHeartbeat = new Date ( ) ; updated . active = true ; Logger . debug ( 'procPutDomains. updating=' + JSON . stringify ( updated ) ) ; Domains . updateEntityFields ( req . vDomain , updated ) ; } else { req . vRestResp . respondFailure ( 'badly formed data' ) ; } ; } else { req . vRestResp . respondFailure ( 'Unauthorized' ) ; } ; } else { req . vRestResp . respondFailure ( req . vDomainError ?? 'Domain not found' ) ; req . vRestResp . HTTPStatus = HTTPStatusCode . Unauthorized ; } ; next ( ) ; } ; const procDeleteDomains = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . vDomain ) { if ( await checkAccessToEntity ( req . vAuthToken , req . vDomain , [ Perm . SPONSOR , Perm . ADMIN ] ) ) { await Domains . removeDomain ( req . vDomain ) ; await Domains . removeDomainContext ( req . vDomain ) ; } else { req . vRestResp . respondFailure ( 'Not authorized' ) ; } ; } else { req . vRestResp . respondFailure ( req . vDomainError ?? 'Domain not found' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/domains/:domainId' ; export const router = Router ( ) ; router . get ( '/api/v1/domains/:domainId' , [ setupMetaverseAPI , domainFromParams , procGetDomainsDomainid , finishMetaverseAPI ] ) ; router . put ( '/api/v1/domains/:domainId' , [ setupMetaverseAPI , domainFromParams , domainAPIkeyFromBody , verifyDomainAccess , procPutDomains , finishMetaverseAPI ] ) ; router . delete ( '/api/v1/domains/:domainId' , [ setupMetaverseAPI , domainFromParams , accountFromAuthToken , procDeleteDomains , finishMetaverseAPI ] ) ;