'use strict' ; import { Request } from 'express' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { Visibility } from '@Entities/Sets/Visibility' ; import { Accounts } from '@Entities/Accounts' ; import { Domains } from '@Entities/Domains' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { Logger } from '@Tools/Logging' ; import { DomainEntity } from '@Entities/DomainEntity' ; export class VisibilityFilter extends CriteriaFilter { private _asAdmin : boolean = false ; private _accessingAcct : AccountEntity ; private _accessingAcctName : string ; private _friends : string [ ] = [ ] ; private _connections : string [ ] = [ ] ; private _doingQuery : boolean = false ; public constructor ( pRequestorAccount ) { super ( ) ; this . _accessingAcct = pRequestorAccount ; return ; } public parametersFromRequest ( pRequest ) { try { if ( this . _accessingAcct ) { this . _accessingAcctName = this . _accessingAcct . username ; if ( this . _accessingAcct . hasOwnProperty ( 'friends' ) ) { this . _friends = this . _accessingAcct . friends ?? [ ] ; } ; if ( this . _accessingAcct . hasOwnProperty ( 'connections' ) ) { this . _connections = this . _accessingAcct . connections ?? [ ] ; } ; if ( pRequest . query . hasOwnProperty ( 'asAdmin' ) ) { const askingForAdmin = pRequest . query . asAdmin === 'true' ; if ( askingForAdmin ) { if ( Accounts . isAdmin ( this . _accessingAcct ) ) { this . _asAdmin = true ; } ; } ; } ; } ; Logger . cdebug ( 'query-detail' , `VisibilityFilter.parametersFromRequest: asAdmin=${ this . _asAdmin }` ) ; } catch ( e ) { Logger . error ( 'VisibilityFilter: parameters from request: exception: ' + e ) ; } ; } ; public addResponseFields ( pRequest ) { return ; } ; public AsAdmin ( ) { return this . _asAdmin ; } ; public criteriaTest ( pToTest ) { let ret = this . _doingQuery || this . _asAdmin ; if ( ! ret ) { if ( pToTest . hasOwnProperty ( 'visibility' ) ) { ret = pToTest . visibility === Visibility . OPEN ; } else { ret = true ; } ; } return ret ; } ; public async criteriaTestAsync ( pToTest , pDomain ? ) { let ret = this . _doingQuery || this . _asAdmin ; if ( ! ret ) { if ( pToTest . hasOwnProperty ( 'visibility' ) ) { switch ( pToTest . visibility ) { case Visibility . OPEN : ret = true ; break ; case Visibility . PRIVATE : if ( this . _accessingAcct && pToTest . hasOwnProperty ( 'domainId' ) ) { const aDomain = pDomain ?? await Domains . getDomainWithId ( pToTest . domainId ) ; if ( aDomain ) { ret = aDomain . sponsorAccountId === this . _accessingAcct . id ; } ; } ; break ; default : ret = false ; break ; } } else { ret = true ; } ; } return ret ; } ; public criteriaParameters ( ) { this . _doingQuery = true ; const criteria = { } ; if ( ! this . _asAdmin ) { criteria [ '$or' ] = [ { 'visibility' : { '$exists' : false } } , { 'visibility' : Visibility . OPEN } , ] ; } return criteria ; } ; public sortCriteriaParameters ( ) { return null ; } ; } ;