'use strict' ; import Config from '@Base/config' ; import { AuthToken } from '@Entities/AuthToken' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { DomainEntity } from '@Entities/DomainEntity' ; import { DomainFields , CheckDomainFields } from '@Entities/DomainFields' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { GenericFilter } from '@Entities/EntityFilters/GenericFilter' ; import { ValidateResponse } from '@Route-Tools/EntityFieldDefn' ; import { getEntityField , setEntityField , getEntityUpdateForField } from '@Route-Tools/GetterSetter' ; import { Maturity } from '@Entities/Sets/Maturity' ; import { createObject , getObject , getObjects , countObjects , updateObjectFields , deleteOne } from '@Tools/Db' ; import { GenUUID , IsNullOrEmpty , IsNotNullOrEmpty } from '@Tools/Misc' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export let domainCollection = 'domains' ; export function initDomains ( ) { CheckDomainFields ( ) ; setInterval ( async ( ) => { for await ( const aDomain of Domains . enumerateAsync ( new GenericFilter ( { 'timeOfLastHeartbeat' : { '$lt' : Domains . dateWhenNotActive ( ) } , '$or' : [ { 'numUsers' : { '$gt' : 0 } } , { 'anonUsers' : { '$gt' : 0 } } , { 'active' : true } ] } ) ) ) { Logger . info ( `Domains: domain ${ aDomain . name } not heartbeating. Zeroing users.` ) ; aDomain . numUsers = 0 ; aDomain . anonUsers = 0 ; aDomain . active = false ; const updates = { 'numUsers' : 0 , 'anonUsers' : 0 , 'active' : false } ; void Domains . updateEntityFields ( aDomain , updates ) ; } ; } , 1000 * Config [ 'metaverse-server' ] [ 'domain-seconds-check-if-online' ] ) ; } ; export const Domains = { async getDomainWithId ( pDomainId ) { return IsNullOrEmpty ( pDomainId ) ? null : getObject ( domainCollection , new GenericFilter ( { 'id' : pDomainId } ) ) ; } , async getDomainWithAPIKey ( pApiKey ) { return IsNullOrEmpty ( pApiKey ) ? null : getObject ( domainCollection , new GenericFilter ( { 'apiKey' : pApiKey } ) ) ; } , async getDomainWithSenderKey ( pSenderKey ) { return IsNullOrEmpty ( pSenderKey ) ? null : getObject ( domainCollection , new GenericFilter ( { 'lastSenderKey' : pSenderKey } ) ) ; } , async addDomain ( pDomainEntity ) { Logger . info ( `Domains: creating domain ${ pDomainEntity . name }, id=${ pDomainEntity . id }` ) ; return IsNullOrEmpty ( pDomainEntity ) ? null : createObject ( domainCollection , pDomainEntity ) ; } , createDomain ( ) { const newDomain = new DomainEntity ( ) ; newDomain . id = GenUUID ( ) ; newDomain . tags = [ ] ; newDomain . managers = [ ] ; newDomain . images = [ ] ; newDomain . whenCreated = new Date ( ) ; newDomain . maturity = Maturity . UNRATED ; return newDomain ; } , async removeDomain ( pDomainEntity ) { Logger . info ( `Domains: removing domain ${ pDomainEntity . name }, id=${ pDomainEntity . id }` ) ; return deleteOne ( domainCollection , new GenericFilter ( { 'id' : pDomainEntity . id } ) ) ; } , async removeDomainContext ( pDomainEntity ) { Logger . info ( `Domains: removing relationships for domain ${ pDomainEntity . name }, id=${ pDomainEntity . id }` ) ; return ; } , async getField ( pAuthToken , pDomain , pField , pRequestingAccount ? ) { return getEntityField ( DomainFields , pAuthToken , pDomain , pField , pRequestingAccount ) ; } , async setField ( pAuthToken , pDomain , pField , pVal , pRequestingAccount ? , pUpdates ? ) { return setEntityField ( DomainFields , pAuthToken , pDomain , pField , pVal , pRequestingAccount , pUpdates ) ; } , getUpdateForField ( pDomain , pField , pExisting ? ) { return getEntityUpdateForField ( DomainFields , pDomain , pField , pExisting ) ; } , async domainCount ( pCriteria ) { return countObjects ( domainCollection , pCriteria ) ; } , async * enumerateAsync ( pPager , pInfoer ? , pScoper ? ) { for await ( const domain of getObjects ( domainCollection , pPager , pInfoer , pScoper ) ) { yield domain ; } ; } , async updateEntityFields ( pEntity , pFields ) { return updateObjectFields ( domainCollection , new GenericFilter ( { 'id' : pEntity . id } ) , pFields ) ; } , dateWhenNotActive ( ) { const notActiveTime = new Date ( Date . now ( ) - ( 1000 * Config [ "metaverse-server" ] [ "domain-seconds-until-offline" ] ) ) ; return notActiveTime ; } , couldBeDomainId ( pId ) { return pId . length === 36 ; } } ;