'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { Accounts } from '@Entities/Accounts' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import { buildLocationInfo , buildImageInfo } from '@Route-Tools/Util' ; import { PaginationInfo } from '@Entities/EntityFilters/PaginationInfo' ; import { SArray , VKeyedCollection } from '@Tools/vTypes' ; import { IsNullOrEmpty , IsNotNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; const procGetUsersConnections = async ( req , resp , next ) => { if ( req . vAuthAccount ) { const pager = new PaginationInfo ( ) ; pager . parametersFromRequest ( req ) ; let connections = await Accounts . getField ( req . vAuthToken , req . vAuthAccount , 'connections' , req . vAuthAccount ) ; connections = IsNullOrEmpty ( connections ) ? [ ] : connections ; Logger . debug ( `procGetUsersConnections: user=${ req . vAuthAccount . username }, connections=${ JSON . stringify ( connections ) }` ) ; const connectionInfo = [ ] ; for ( const connectionUsername of connections ) { if ( pager . criteriaTest ( connectionUsername ) ) { const aAccount = await Accounts . getAccountWithUsername ( connectionUsername ) ; if ( aAccount ) { connectionInfo . push ( { 'username' : connectionUsername , 'connection' : SArray . has ( req . vAuthAccount . friends , connectionUsername ) ? 'friend' : 'connection' , 'images' : await buildImageInfo ( aAccount ) , 'location' : await buildLocationInfo ( aAccount ) } ) ; } else { Logger . error ( `procGetUsersConnections: connection name with no account. acct=${ req . vAuthAccount . id }, name=${ connectionUsername }` ) ; connectionInfo . push ( { 'username' : connectionUsername , 'connection' : 'unknown' } ) ; } ; } ; } ; req . vRestResp . Data = { 'users' : connectionInfo } ; pager . addResponseFields ( req ) ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/users/connections' ; export const router = Router ( ) ; router . get ( '/api/v1/users/connections' , [ setupMetaverseAPI , accountFromAuthToken , procGetUsersConnections , finishMetaverseAPI ] ) ;