@@ -17,7 +17,7 @@ import { CoreNavigator } from '@services/navigator';
17
17
import { CoreSites } from '@services/sites' ;
18
18
19
19
import { makeSingleton , Translate } from '@singletons' ;
20
- import { CoreUser , CoreUserProfile , CoreUserRole } from './user' ;
20
+ import { CoreUser , CoreUserBasicData , CoreUserProfile , CoreUserRole } from './user' ;
21
21
import { CoreTime } from '@singletons/time' ;
22
22
23
23
/**
@@ -90,24 +90,8 @@ export class CoreUserHelperProvider {
90
90
*
91
91
* @param user User object.
92
92
* @returns User initials.
93
- * @deprecated since 4.4. Use getUserInitialsFromParts instead.
94
93
*/
95
94
getUserInitials ( user : Partial < CoreUserProfile > ) : string {
96
- if ( ! user . firstname && ! user . lastname ) {
97
- // @TODO : Use local info or check WS to get initials from.
98
- return '' ;
99
- }
100
-
101
- return ( user . firstname ?. charAt ( 0 ) || '' ) + ( user . lastname ?. charAt ( 0 ) || '' ) ;
102
- }
103
-
104
- /**
105
- * Get the user initials.
106
- *
107
- * @param parts User name parts. Containing firstname, lastname, fullname and userId.
108
- * @returns User initials.
109
- */
110
- async getUserInitialsFromParts ( parts : CoreUserNameParts ) : Promise < string > {
111
95
const nameFields = [ 'firstname' , 'lastname' ] ;
112
96
const dummyUser = {
113
97
firstname : 'firstname' ,
@@ -118,28 +102,30 @@ export class CoreUserHelperProvider {
118
102
. filter ( ( field ) => nameFormat . indexOf ( field ) >= 0 )
119
103
. sort ( ( a , b ) => nameFormat . indexOf ( a ) - nameFormat . indexOf ( b ) ) ;
120
104
121
- if ( ! parts . firstname && ! parts . lastname ) {
122
- if ( ! parts . fullname && parts . userId ) {
123
- const user = await CoreUser . getProfile ( parts . userId , undefined , true ) ;
124
- parts . fullname = user . fullname || '' ;
125
- }
126
-
127
- if ( parts . fullname ) {
128
- // It's a complete workaround.
129
- const split = parts . fullname . split ( ' ' ) ;
130
- parts . firstname = split [ 0 ] ;
131
- if ( split . length > 1 ) {
132
- parts . lastname = split [ split . length - 1 ] ;
133
- }
134
- }
135
- }
136
-
137
105
const initials = availableFieldsSorted . reduce ( ( initials , fieldName ) =>
138
- initials + ( parts [ fieldName ] ?. charAt ( 0 ) ?? '' ) , '' ) ;
106
+ initials + ( user [ fieldName ] ?. charAt ( 0 ) ?? '' ) , '' ) ;
139
107
140
108
return initials || 'UNK' ;
141
109
}
142
110
111
+ /**
112
+ * Get the user initials.
113
+ *
114
+ * @param parts User name parts. Containing firstname, lastname, fullname and userId.
115
+ * @returns User initials.
116
+ */
117
+ async getUserInitialsFromParts ( parts : CoreUserNameParts ) : Promise < string > {
118
+ const initials = this . getUserInitials ( parts ) ;
119
+ if ( initials !== 'UNK' || ! parts . userId ) {
120
+ return initials ;
121
+ }
122
+ const user = await CoreUser . getProfile ( parts . userId , undefined , false ) ;
123
+ console . error ( user , parts . userId ) ;
124
+
125
+ return user . initials || 'UNK' ;
126
+
127
+ }
128
+
143
129
/**
144
130
* Translates legacy timezone names.
145
131
*
@@ -151,8 +137,52 @@ export class CoreUserHelperProvider {
151
137
return CoreTime . translateLegacyTimezone ( tz ) ;
152
138
}
153
139
140
+ normalizeBasicFields < T extends CoreUserBasicData = CoreUserBasicData > ( profile : CoreUserDenormalized ) : T {
141
+ let normalized = {
142
+ id : profile . id ?? profile . userid ?? 0 ,
143
+ fullname : profile . fullname ?? profile . userfullname ?? '' ,
144
+ profileimageurl : profile . profileimageurl ?? profile . userprofileimageurl ??
145
+ profile . userpictureurl ?? profile . profileimageurlsmall ?? profile . urls ?. profileimage ?? '' ,
146
+ } as T ;
147
+
148
+ delete profile . userid ;
149
+ delete profile . userfullname ;
150
+ delete profile . userpictureurl ;
151
+ delete profile . userprofileimageurl ;
152
+ delete profile . profileimageurlsmall ;
153
+ delete profile . urls ;
154
+
155
+ normalized = { ...profile , ...normalized } ;
156
+
157
+ if ( normalized . id === 0 ) {
158
+ throw new Error ( 'Invalid user ID' ) ;
159
+ }
160
+
161
+ normalized . initials = CoreUserHelper . getUserInitials ( profile ) ;
162
+
163
+ return normalized ;
164
+ }
165
+
154
166
}
155
167
156
168
export const CoreUserHelper = makeSingleton ( CoreUserHelperProvider ) ;
157
169
158
170
type CoreUserNameParts = { firstname ?: string ; lastname ?: string ; fullname ?: string ; userId ?: number } ;
171
+
172
+ type CoreUserDenormalized = CoreUserBasicData & {
173
+ id ?: number ;
174
+ userid ?: number ;
175
+
176
+ initials ?: string ; // Initials.
177
+
178
+ fullname ?: string ;
179
+ userfullname ?: string ;
180
+
181
+ profileimageurl ?: string ;
182
+ userpictureurl ?: string ;
183
+ userprofileimageurl ?: string ;
184
+ profileimageurlsmall ?: string ;
185
+ urls ?: {
186
+ profileimage ?: string ;
187
+ } ;
188
+ } ;
0 commit comments