'use strict' ; import { Config } from '@Base/config' ; import { RequestEntity } from '@Entities/RequestEntity' ; import { createObject , getObject , getObjects , updateObjectFields , deleteOne , deleteMany } from '@Tools/Db' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { GenericFilter } from '@Entities/EntityFilters/GenericFilter' ; import { GenUUID , SimpleObject , IsNotNullOrEmpty , IsNullOrEmpty } from '@Tools/Misc' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export let requestCollection = 'requests' ; export class RequestType { public static HANDSHAKE = 'handshake' ; public static CONNECTION = 'connection' ; public static FRIEND = 'friend' ; public static FOLLOW = 'follow' ; public static VERIFYEMAIL = 'verifyEmail' ; } ; export function initRequests ( ) { setInterval ( async ( ) => { const nowtime = new Date ( ) ; const numDeleted = await deleteMany ( requestCollection , new GenericFilter ( { 'expirationTime' : { $lt : nowtime } } ) ) ; if ( numDeleted > 0 ) { Logger . debug ( `Requests.Expiration: expired ${ numDeleted } requests` ) ; } ; } , 1000 * 60 * 2 ) ; } ; export const Requests = { async getWithId ( pRequestId ) { return IsNullOrEmpty ( pRequestId ) ? null : getObject ( requestCollection , new GenericFilter ( { 'id' : pRequestId } ) ) ; } , async getWithRequesterOrTarget ( pRequestId , pType , pRequesterField = 'requestingAccountId' , pTargetField = 'targetAccountId' ) { return IsNullOrEmpty ( pRequestId ) ? null : getObject ( requestCollection , new GenericFilter ( { '$or' : [ SimpleObject ( pRequesterField , pRequestId ) , SimpleObject ( pTargetField , pRequestId ) ] , 'requestType' : pType } ) ) ; } , async getWithRequestBetween ( pRequestId , pTargetId , pType , pRequesterField = 'requestingAccountId' , pTargetField = 'targetAccountId' ) { const wayone = { } ; wayone [ pRequesterField ] = pRequestId ; wayone [ pTargetField ] = pTargetId ; const waytwo = { } ; waytwo [ pRequesterField ] = pTargetId ; waytwo [ pTargetField ] = pRequestId ; return IsNullOrEmpty ( pRequestId ) ? null : getObject ( requestCollection , new GenericFilter ( { '$or' : [ wayone , waytwo ] , 'requestType' : pType } ) ) ; } , create ( ) { const aRequest = new RequestEntity ( ) ; aRequest . id = GenUUID ( ) ; aRequest . expirationTime = new Date ( Date . now ( ) + 1000 * 60 ) ; aRequest . whenCreated = new Date ( ) ; return aRequest ; } , createHandshakeRequest ( pRequesterNodeId , pTargetNodeId ) { const newRequest = Requests . create ( ) ; newRequest . requestType = RequestType . HANDSHAKE ; newRequest . requesterNodeId = pRequesterNodeId ; newRequest . requesterAccepted = false ; newRequest . targetNodeId = pTargetNodeId ; newRequest . targetAccepted = false ; const expirationMinutes = Config [ "metaverse-server" ] [ "handshake-request-expiration-minutes" ] ; newRequest . expirationTime = new Date ( Date . now ( ) + 1000 * 60 * expirationMinutes ) ; return newRequest ; } , createEmailVerificationRequest ( pAccountId , pVerificationCode ) { const newRequest = Requests . create ( ) ; newRequest . requestType = RequestType . VERIFYEMAIL ; newRequest . requestingAccountId = pAccountId ; newRequest . verificationCode = pVerificationCode ; const expirationMinutes = Config [ "metaverse-server" ] [ 'email-verification-timeout-minutes' ] ; newRequest . expirationTime = new Date ( Date . now ( ) + 1000 * 60 * expirationMinutes ) ; return newRequest ; } , add ( pRequestEntity ) { return createObject ( requestCollection , pRequestEntity ) ; } , async update ( pEntity , pFields ) { return updateObjectFields ( requestCollection , new GenericFilter ( { 'id' : pEntity . id } ) , pFields ) ; } , async remove ( pRequestEntity ) { return deleteOne ( requestCollection , new GenericFilter ( { 'id' : pRequestEntity . id } ) ) ; } , async removeAllMyRequests ( pAccountId , pRequestType ? ) { const criteria = { 'requestingAccount' : pAccountId } ; if ( IsNotNullOrEmpty ( pRequestType ) ) { criteria . requestType = pRequestType ; } ; return deleteMany ( requestCollection , new GenericFilter ( criteria ) ) ; } , async * enumerateAsync ( pPager , pInfoer ? , pScoper ? ) { for await ( const acct of getObjects ( requestCollection , pPager , pInfoer , pScoper ) ) { yield acct ; } ; } } ;