'use strict' ; import { Request } from 'express' ; import { Accounts } from '@Entities/Accounts' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { Domains } from '@Entities/Domains' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { IsNullOrEmpty } from '@Tools/Misc' ; import { SArray } from '@Tools/vTypes' ; import { Logger } from '@Tools/Logging' ; export class AccountFilterInfo extends CriteriaFilter { private _requestingAccount : AccountEntity ; private _filter : string ; private _findFriends : boolean = false ; private _friendsList : string [ ] ; private _findConnections : boolean = false ; private _connectionsList : string [ ] ; private _findDomain : boolean = false ; private _targetDomain : string ; private _status : string ; private _findOnline : boolean = false ; private _search : string ; private _findMatch : string ; private _doingQuery : boolean = false ; public constructor ( ) { super ( ) ; return ; } public parametersFromRequest ( pRequest ) { this . _requestingAccount = pRequest . vAuthAccount ; try { if ( typeof ( pRequest . query . filter ) === 'string' ) { this . _filter = pRequest . query . filter ; const filterPieces = this . _filter . split ( ',' ) ; filterPieces . forEach ( filterClass => { switch ( filterClass ) { case 'all' : this . _findFriends = false ; this . _findConnections = false ; break ; case 'friends' : this . _findFriends = true ; break ; case 'connections' : this . _findConnections = true ; break ; default : break ; } ; } ) ; if ( this . _findFriends && this . _requestingAccount ) { this . _friendsList = this . _requestingAccount . friends ; if ( IsNullOrEmpty ( this . _friendsList ) ) { this . _findFriends = false ; } ; } ; if ( this . _findConnections && this . _requestingAccount ) { this . _connectionsList = this . _requestingAccount . connections ; if ( IsNullOrEmpty ( this . _connectionsList ) ) { this . _findConnections = false ; } ; } ; } ; if ( typeof ( pRequest . query . status ) === 'string' ) { this . _status = pRequest . query . status ; const statusPieces = this . _status . split ( ',' ) ; statusPieces . forEach ( statusClass => { switch ( statusClass ) { case 'online' : this . _findOnline = true ; break ; default : if ( Domains . couldBeDomainId ( statusClass ) ) { this . _findDomain = true ; this . _findOnline = true ; this . _targetDomain = statusClass ; } ; break ; } ; } ) ; } ; this . _search = String ( pRequest . query . search ) ; Logger . cdebug ( 'query-detail' , `AccountFilterInfo.parametersFromRequest: findFriends=${ this . _findFriends }, findConn=${ this . _findConnections }, findOnline=${ this . _findOnline }, findDomain=${ this . _findDomain }` ) ; } catch ( e ) { Logger . error ( 'AccountFilterInfo: parameters from request: exception: ' + e ) ; } } ; public addResponseFields ( pRequest ) { return ; } ; public criteriaTest ( pThingy ) { let ret = false ; if ( this . _doingQuery ) { ret = true ; } else { let filterSelect = false ; if ( this . _findFriends ) { if ( pThingy . hasOwnProperty ( 'friends' ) ) { for ( const friend of this . _friendsList ) { if ( SArray . has ( pThingy . friends , friend ) ) { filterSelect = true ; break ; } ; } ; } ; } ; if ( this . _findConnections ) { if ( pThingy . hasOwnProperty ( 'connections' ) ) { for ( const connection of this . _connectionsList ) { if ( SArray . has ( pThingy . connections , connection ) ) { filterSelect = true ; break ; } ; } ; } ; } ; const statusSelect = false ; if ( this . _findOnline ) { if ( pThingy . hasOwnProperty ( 'timeOfLastHeartbeat' ) ) { ret = Accounts . isOnline ( pThingy as AccountEntity ) ; } ; } ; ret = filterSelect && statusSelect ; } return ret ; } ; public async criteriaTestAsync ( pThingy ) { return this . criteriaTest ( pThingy ) ; } ; public criteriaParameters ( ) { this . _doingQuery = true ; const criteria = { } ; if ( this . _findFriends ) { criteria . friends = { '$in' : this . _friendsList } ; } ; if ( this . _findConnections ) { criteria . connections = { '$in' : this . _connectionsList } ; } ; if ( this . _findOnline ) { criteria . timeOfLastHeartbeat = { '$gte' : Accounts . dateWhenNotOnline ( ) } ; } ; if ( this . _findDomain ) { criteria . locationDomainId = this . _targetDomain ; } ; return criteria ; } ; public sortCriteriaParameters ( ) { return null ; } ; } ;