6
6
7
7
import { User } from "@gitpod/public-api/lib/gitpod/v1/user_pb" ;
8
8
import { User as UserProtocol } from "@gitpod/gitpod-protocol/lib/protocol" ;
9
+ import { Timestamp } from "@bufbuild/protobuf" ;
9
10
10
11
/**
11
12
* Returns a primary email address of a user.
@@ -17,37 +18,44 @@ import { User as UserProtocol } from "@gitpod/gitpod-protocol/lib/protocol";
17
18
* @param user
18
19
* @returns A primaryEmail, or undefined.
19
20
*/
20
- export function getPrimaryEmail ( user : User ) : string | undefined {
21
+ export function getPrimaryEmail ( user : User | UserProtocol ) : string | undefined {
21
22
// If the accounts is owned by an organization, use the email of the most recently
22
23
// used SSO identity.
23
24
if ( isOrganizationOwned ( user ) ) {
24
- const compareTime = ( a ?: string , b ?: string ) => ( a || "" ) . localeCompare ( b || "" ) ;
25
+ const timestampToString = ( a ?: string | Timestamp ) =>
26
+ a instanceof Timestamp ? a ?. toDate ( ) ?. toISOString ( ) : a || "" ;
27
+ const compareTime = ( a ?: string | Timestamp , b ?: string | Timestamp ) => {
28
+ return timestampToString ( a ) . localeCompare ( timestampToString ( b ) ) ;
29
+ } ;
25
30
const recentlyUsedSSOIdentity = user . identities
26
- . sort ( ( a , b ) =>
27
- compareTime ( a . lastSigninTime ?. toDate ( ) ?. toISOString ( ) , b . lastSigninTime ?. toDate ( ) ?. toISOString ( ) ) ,
28
- )
31
+ . sort ( ( a , b ) => compareTime ( a . lastSigninTime , b . lastSigninTime ) )
29
32
// optimistically pick the most recent one
30
33
. reverse ( ) [ 0 ] ;
31
34
return recentlyUsedSSOIdentity ?. primaryEmail ;
32
35
}
33
36
34
37
// In case of a personal account, check for the email stored by the user.
35
- if ( ! isOrganizationOwned ( user ) && user . profile ?. emailAddress ) {
36
- return user . profile ?. emailAddress ;
38
+ if ( ! isOrganizationOwned ( user ) ) {
39
+ const emailAddress = UserProtocol . is ( user )
40
+ ? user . additionalData ?. profile ?. emailAddress
41
+ : user . profile ?. emailAddress ;
42
+ if ( emailAddress ) {
43
+ return emailAddress ;
44
+ }
37
45
}
38
46
39
47
// Otherwise pick any
40
48
// FIXME(at) this is still not correct, as it doesn't distinguish between
41
49
// sign-in providers and additional Git hosters.
42
- const identities = user . identities . filter ( ( i ) => ! ! i . primaryEmail ) ;
43
- if ( identities . length <= 0 ) {
50
+ const primaryEmails : string [ ] = user . identities . map ( ( i ) => i . primaryEmail || "" ) . filter ( ( e ) => ! ! e ) ;
51
+ if ( primaryEmails . length <= 0 ) {
44
52
return undefined ;
45
53
}
46
54
47
- return identities [ 0 ] . primaryEmail || undefined ;
55
+ return primaryEmails [ 0 ] || undefined ;
48
56
}
49
57
50
- export function getName ( user : User ) : string | undefined {
58
+ export function getName ( user : User | UserProtocol ) : string | undefined {
51
59
const name = /* user.fullName ||*/ user . name ;
52
60
if ( name ) {
53
61
return name ;
@@ -61,23 +69,24 @@ export function getName(user: User): string | undefined {
61
69
return undefined ;
62
70
}
63
71
64
- export function isOrganizationOwned ( user : User ) {
72
+ export function isOrganizationOwned ( user : User | UserProtocol ) {
65
73
return ! ! user . organizationId ;
66
74
}
67
75
68
76
// FIXME(at) get rid of this Nth indirection to read attributes of User entity
69
- export function getProfile ( user : User ) : UserProtocol . Profile {
77
+ export function getProfile ( user : User | UserProtocol ) : UserProtocol . Profile {
78
+ const profile = UserProtocol . is ( user ) ? user . additionalData ?. profile : user . profile ;
70
79
return {
71
- name : getName ( user ! ) || "" ,
72
- email : getPrimaryEmail ( user ! ) || "" ,
73
- company : user ?. profile ?. companyName ,
80
+ name : getName ( user ) || "" ,
81
+ email : getPrimaryEmail ( user ) || "" ,
82
+ company : profile ?. companyName ,
74
83
avatarURL : user ?. avatarUrl ,
75
- jobRole : user ?. profile ?. jobRole ,
76
- jobRoleOther : user ?. profile ?. jobRoleOther ,
77
- explorationReasons : user ?. profile ?. explorationReasons ,
78
- signupGoals : user ?. profile ?. signupGoals ,
79
- signupGoalsOther : user ?. profile ?. signupGoalsOther ,
80
- companySize : user ?. profile ?. companySize ,
81
- onboardedTimestamp : user ?. profile ?. onboardedTimestamp ,
84
+ jobRole : profile ?. jobRole ,
85
+ jobRoleOther : profile ?. jobRoleOther ,
86
+ explorationReasons : profile ?. explorationReasons ,
87
+ signupGoals : profile ?. signupGoals ,
88
+ signupGoalsOther : profile ?. signupGoalsOther ,
89
+ companySize : profile ?. companySize ,
90
+ onboardedTimestamp : profile ?. onboardedTimestamp ,
82
91
} ;
83
92
}
0 commit comments