@@ -17,7 +17,7 @@ import { CoreNavigator } from '@services/navigator';
1717import { CoreSites } from '@services/sites' ;
1818
1919import { makeSingleton , Translate } from '@singletons' ;
20- import { CoreUser , CoreUserProfile , CoreUserRole } from './user' ;
20+ import { CoreUser , CoreUserBasicData , CoreUserProfile , CoreUserRole } from './user' ;
2121import { CoreTime } from '@singletons/time' ;
2222
2323/**
@@ -90,24 +90,8 @@ export class CoreUserHelperProvider {
9090 *
9191 * @param user User object.
9292 * @returns User initials.
93- * @deprecated since 4.4. Use getUserInitialsFromParts instead.
9493 */
9594 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 > {
11195 const nameFields = [ 'firstname' , 'lastname' ] ;
11296 const dummyUser = {
11397 firstname : 'firstname' ,
@@ -118,28 +102,30 @@ export class CoreUserHelperProvider {
118102 . filter ( ( field ) => nameFormat . indexOf ( field ) >= 0 )
119103 . sort ( ( a , b ) => nameFormat . indexOf ( a ) - nameFormat . indexOf ( b ) ) ;
120104
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-
137105 const initials = availableFieldsSorted . reduce ( ( initials , fieldName ) =>
138- initials + ( parts [ fieldName ] ?. charAt ( 0 ) ?? '' ) , '' ) ;
106+ initials + ( user [ fieldName ] ?. charAt ( 0 ) ?? '' ) , '' ) ;
139107
140108 return initials || 'UNK' ;
141109 }
142110
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+
143129 /**
144130 * Translates legacy timezone names.
145131 *
@@ -151,8 +137,52 @@ export class CoreUserHelperProvider {
151137 return CoreTime . translateLegacyTimezone ( tz ) ;
152138 }
153139
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+
154166}
155167
156168export const CoreUserHelper = makeSingleton ( CoreUserHelperProvider ) ;
157169
158170type 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