1
- const EcPerson = require ( " cassproject/src/org/schema/EcPerson" ) ;
1
+ const EcPerson = require ( ' cassproject/src/org/schema/EcPerson' ) ;
2
2
3
3
/** Will NOT return null, but may throw errors. */
4
- let anythingToPem = global . anythingToPem = async ( subject ) => {
5
- if ( typeof subject === " string" && subject . startsWith ( " -----BEGIN" ) ) {
4
+ let anythingToPem = global . anythingToPem = async ( subject ) => {
5
+ if ( typeof subject === ' string' && subject . startsWith ( ' -----BEGIN' ) ) {
6
6
return subject ;
7
7
}
8
8
9
- return anythingToPerson ( subject ) . then ( person => {
10
- if ( person == null ) throw new Error ( " No Person found for " + subject ) ;
11
- if ( EcArray . isArray ( person ) )
9
+ return anythingToPerson ( subject ) . then ( ( person ) => {
10
+ if ( person == null ) throw new Error ( ' No Person found for ' + subject ) ;
11
+ if ( EcArray . isArray ( person ) ) {
12
12
return person . map ( ( p ) => p . owner [ 0 ] ) ;
13
+ }
13
14
return person . owner [ 0 ] ;
14
- } ) . catch ( e => {
15
+ } ) . catch ( ( e ) => {
15
16
throw e ;
16
17
} ) ;
17
18
} ;
18
19
19
- let anythingToPerson = global . anythingToPerson = async ( subject ) => {
20
+ let unknownPerson = new EcPerson ( ) ;
21
+ unknownPerson . generateId ( global . repo . selectedServer ) ;
22
+ unknownPerson . name = 'Unknown Person' ;
23
+ let anythingToPersonIsTolerant = true ;
24
+ let anythingToPerson = global . anythingToPerson = async ( subject ) => {
20
25
// No/Invalid subject?
21
- if ( subject == null ) throw new Error ( " Parse Error" ) ;
26
+ if ( subject == null ) throw new Error ( ' Parse Error' ) ;
22
27
23
28
let result ;
24
- if ( EcArray . isArray ( subject ) )
29
+ if ( EcArray . isArray ( subject ) ) {
25
30
result = await Promise . all ( subject . map ( anythingToPerson ) ) ;
31
+ }
26
32
// Subject is URL?
27
- else if ( typeof subject === " string" && subject . startsWith ( " http" ) ) {
33
+ else if ( typeof subject === ' string' && subject . startsWith ( ' http' ) ) {
28
34
const url = subject ;
29
35
let person ;
30
36
try {
31
37
person = await EcPerson . get ( url ) ;
32
38
} catch ( e ) {
33
- error ( e , 500 ) ;
39
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
40
+ error ( e , 500 ) ;
34
41
}
35
42
36
- if ( person == null )
37
- error ( "Person not found." , 400 ) ;
43
+ if ( person == null ) {
44
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
45
+ error ( 'Person not found.' , 400 ) ;
46
+ }
38
47
39
48
result = person ;
40
- } else if ( typeof subject === " string" && subject . startsWith ( " -----BEGIN" ) ) {
49
+ } else if ( typeof subject === ' string' && subject . startsWith ( ' -----BEGIN' ) ) {
41
50
// Subject is PEM
42
51
subject = EcPk . fromPem ( subject ) . toPem ( ) ;
43
52
let person ;
44
53
try {
45
54
person = await EcPerson . getByPk ( repo , EcPk . fromPem ( subject ) ) . catch ( ( ) => { } ) ;
46
55
if ( person == null ) {
47
- people = await EcPerson . search ( repo , `owner:"${ subject } "` ) ;
56
+ people = await EcPerson . search ( repo , `owner:"${ subject } "` ) ;
48
57
people = people . filter ( ( p ) => p . owner [ 0 ] == subject ) ;
49
58
if ( people . length > 0 ) person = people [ 0 ] ;
50
- if ( people . length > 1 ) global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . INFO , " AnythingToPerson" , `Looking for person, found people! ${ people . length } ` ) ;
59
+ if ( people . length > 1 ) global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . INFO , ' AnythingToPerson' , `Looking for person, found people! ${ people . length } ` ) ;
51
60
}
52
61
} catch ( e ) {
53
- global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . ERROR , " AnythingToPerson" , e ) ;
62
+ global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . ERROR , ' AnythingToPerson' , e ) ;
54
63
if ( e instanceof TypeError ) {
55
- throw new Error ( "Parse Error" ) ;
56
- } else throw new Error ( e . message ) ;
64
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
65
+ throw new Error ( 'Parse Error' ) ;
66
+ } else {
67
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
68
+ throw new Error ( e . message ) ;
69
+ }
57
70
}
58
71
59
- if ( person == null ) throw new Error ( "Could not find " + subject ) ;
72
+ if ( person == null ) {
73
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
74
+ throw new Error ( 'Could not find ' + subject ) ;
75
+ }
60
76
61
77
result = person ;
62
- } else if ( typeof subject === " string" ) {
78
+ } else if ( typeof subject === ' string' ) {
63
79
// Subject is an Email or Username
64
80
let people ;
65
81
try {
66
- people = await EcPerson . search ( repo , `email:"${ subject } " OR username:"${ subject } "` ) ;
82
+ people = await EcPerson . search ( repo , `email:"${ subject } " OR username:"${ subject } "` ) ;
67
83
} catch ( e ) {
68
- global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . ERROR , "AnythingToPerson" , e ) ;
84
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
85
+ global . auditLogger . report ( global . auditLogger . LogCategory . PROFILE , global . auditLogger . Severity . ERROR , 'AnythingToPerson' , e ) ;
69
86
throw new Error ( e . message ) ;
70
87
}
71
- if ( people == null || people . length === 0 ) Error ( "No Person found for " + subject ) ;
88
+ if ( people == null || people . length === 0 ) {
89
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
90
+ throw new Error ( 'No Person found for ' + subject ) ;
91
+ }
72
92
73
93
result = people [ 0 ] ;
74
94
}
75
95
76
96
if ( result != null ) {
77
- if ( result . personType != null && result . personType !== "Person" )
78
- throw Error ( "No Person found for " + subject ) ;
97
+ if ( result . personType != null && result . personType !== 'Person' ) {
98
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
99
+ throw Error ( 'No Person found for ' + subject ) ;
100
+ }
79
101
return result ;
80
102
}
81
103
82
- throw new Error ( "Parse Error" ) ;
83
- }
104
+ if ( anythingToPersonIsTolerant ) return unknownPerson ;
105
+ throw new Error ( 'Parse Error' ) ;
106
+ } ;
84
107
module . exports = {
85
108
/** Will NOT return null, but may throw errors. */
86
- anythingToPem : anythingToPem ,
87
- anythingToPerson : anythingToPerson
88
- }
109
+ anythingToPem : anythingToPem ,
110
+ anythingToPerson : anythingToPerson ,
111
+ } ;
0 commit comments