From 4ebec029ba6bac82893355afedb292636048d6f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= <crazyserver@gmail.com>
Date: Mon, 13 Nov 2023 12:36:04 +0100
Subject: [PATCH] MOBILE-3947 user: Fix user types on user-avatar

---
 .../components/sites-list/sites-list.html     |  2 +-
 .../components/user-avatar/user-avatar.ts     | 27 ++++++++++++++++---
 .../choose-site-modal/choose-site-modal.html  |  2 +-
 .../login/pages/reconnect/reconnect.html      |  2 +-
 .../user-menu-button/user-menu-button.html    |  2 +-
 .../components/user-menu/user-menu.html       |  2 +-
 src/core/services/sites.ts                    |  2 ++
 7 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/src/core/components/sites-list/sites-list.html b/src/core/components/sites-list/sites-list.html
index d4f3b7e57..b00d973dd 100644
--- a/src/core/components/sites-list/sites-list.html
+++ b/src/core/components/sites-list/sites-list.html
@@ -40,7 +40,7 @@
     <ion-item [attr.button]="isSiteClickable(isCurrentSite) ? true : null" (click)="siteClicked($event, site, isCurrentSite)"
         [attr.detail]="isSiteClickable(isCurrentSite) ? 'true' : 'false'" [class.item-current]="isCurrentSite">
 
-        <core-user-avatar [user]="site" slot="start" [linkProfile]="false" [siteId]="site.id"></core-user-avatar>
+        <core-user-avatar [site]="site" slot="start" [linkProfile]="false" [siteId]="site.id"></core-user-avatar>
 
         <ion-label>
             <p class="item-heading">{{site.fullname}}</p>
diff --git a/src/core/components/user-avatar/user-avatar.ts b/src/core/components/user-avatar/user-avatar.ts
index bdb7bee4c..6c3e137e5 100644
--- a/src/core/components/user-avatar/user-avatar.ts
+++ b/src/core/components/user-avatar/user-avatar.ts
@@ -14,7 +14,7 @@
 
 import { Component, Input, OnInit, OnChanges, OnDestroy, SimpleChange } from '@angular/core';
 
-import { CoreSites } from '@services/sites';
+import { CoreSiteBasicInfo, CoreSites } from '@services/sites';
 import { CoreUtils } from '@services/utils/utils';
 import { CoreEventObserver, CoreEvents } from '@singletons/events';
 import { USER_PROFILE_PICTURE_UPDATED, CoreUserBasicData } from '@features/user/services/user';
@@ -22,6 +22,7 @@ import { CoreNavigator } from '@services/navigator';
 import { CoreNetwork } from '@services/network';
 import { CoreUserHelper } from '@features/user/services/user-helper';
 import { CoreUrlUtils } from '@services/utils/url';
+import { CoreSiteInfo } from '@classes/site';
 
 /**
  * Component to display a "user avatar".
@@ -35,7 +36,8 @@ import { CoreUrlUtils } from '@services/utils/url';
 })
 export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy {
 
-    @Input() user?: CoreUserWithAvatar;
+    @Input() user?: CoreUserWithAvatar; // @todo Fix the accepted type and restrict it a bit.
+    @Input() site?: CoreSiteBasicInfo | CoreSiteInfo; // Site info contains user info.
     // The following params will override the ones in user object.
     @Input() profileUrl?: string;
     @Input() linkProfile = true; // Avoid linking to the profile if wanted.
@@ -71,8 +73,23 @@ export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy {
     /**
      * @inheritdoc
      */
-    ngOnInit(): void {
-        this.siteId = this.siteId || CoreSites.getCurrentSiteId();
+    async ngOnInit(): Promise<void> {
+        this.siteId = this.siteId ?? (this.site && 'id' in this.site
+            ? this.site.id
+            : CoreSites.getCurrentSiteId());
+
+        if (this.site && !this.user) {
+            this.user = {
+                id: ('userid' in this.site
+                    ? this.site.userid
+                    : this.site.userId)
+                    ?? (await CoreSites.getSite(this.siteId)).getUserId(),
+                fullname: this.site.fullname ?? '',
+                firstname: this.site.firstname ?? '',
+                lastname: this.site.lastname ?? '',
+                userpictureurl: this.site.userpictureurl,
+            };
+        }
 
         this.setFields();
     }
@@ -189,4 +206,6 @@ export type CoreUserWithAvatar = CoreUserBasicData & {
     isonline?: boolean;
     courseid?: number;
     lastaccess?: number;
+    firstname?: string; // The first name(s) of the user.
+    lastname?: string; // The family name of the user.
 };
diff --git a/src/core/features/contentlinks/components/choose-site-modal/choose-site-modal.html b/src/core/features/contentlinks/components/choose-site-modal/choose-site-modal.html
index 25ceee8f7..a78a68793 100644
--- a/src/core/features/contentlinks/components/choose-site-modal/choose-site-modal.html
+++ b/src/core/features/contentlinks/components/choose-site-modal/choose-site-modal.html
@@ -20,7 +20,7 @@
                 </ion-label>
             </ion-item>
             <ion-item *ngFor="let site of sites" (click)="siteClicked(site.id)" [detail]="false" button>
-                <core-user-avatar [user]="site" slot="start" [linkProfile]="false"></core-user-avatar>
+                <core-user-avatar [site]="site" slot="start" [linkProfile]="false"></core-user-avatar>
 
                 <ion-label>
                     <p class="item-heading">{{site.fullname}}</p>
diff --git a/src/core/features/login/pages/reconnect/reconnect.html b/src/core/features/login/pages/reconnect/reconnect.html
index af0a6e681..dcde5f15f 100644
--- a/src/core/features/login/pages/reconnect/reconnect.html
+++ b/src/core/features/login/pages/reconnect/reconnect.html
@@ -45,7 +45,7 @@
 
                 <div class="core-login-user">
                     <!-- Show user avatar. -->
-                    <core-user-avatar class="large-avatar" *ngIf="showUserAvatar" [user]="siteInfo" [linkProfile]="false"
+                    <core-user-avatar class="large-avatar" *ngIf="showUserAvatar" [site]="siteInfo" [linkProfile]="false"
                         [siteId]="siteId"></core-user-avatar>
                     <p *ngIf="siteInfo?.fullname" class="core-login-fullname">
                         <core-format-text [text]="siteInfo?.fullname" [filter]="false"></core-format-text>
diff --git a/src/core/features/mainmenu/components/user-menu-button/user-menu-button.html b/src/core/features/mainmenu/components/user-menu-button/user-menu-button.html
index 5e8829b0b..b23e54da8 100644
--- a/src/core/features/mainmenu/components/user-menu-button/user-menu-button.html
+++ b/src/core/features/mainmenu/components/user-menu-button/user-menu-button.html
@@ -1,4 +1,4 @@
-<core-user-avatar *ngIf="(alwaysShow || isMainScreen) && siteInfo" [user]="siteInfo" class="core-bar-button-image clickable"
+<core-user-avatar *ngIf="(alwaysShow || isMainScreen) && siteInfo" [site]="siteInfo" class="core-bar-button-image clickable"
     [linkProfile]="false" (ariaButtonClick)="openUserMenu($event)" [userTour]="userTour"
     [attr.aria-label]="'core.user.useraccount' | translate">
 </core-user-avatar>
diff --git a/src/core/features/mainmenu/components/user-menu/user-menu.html b/src/core/features/mainmenu/components/user-menu/user-menu.html
index 8de97c149..896d075c2 100644
--- a/src/core/features/mainmenu/components/user-menu/user-menu.html
+++ b/src/core/features/mainmenu/components/user-menu/user-menu.html
@@ -44,7 +44,7 @@
 
             <ion-item button class="core-usermenu-handler ion-text-wrap" *ngIf="siteInfo" lines="full" (click)="openUserProfile($event)"
                 [detail]="true" [attr.aria-label]="'core.user.profile' | translate">
-                <core-user-avatar [user]="siteInfo" [userId]="siteInfo.userid" [linkProfile]="false" slot="start"></core-user-avatar>
+                <core-user-avatar [site]="siteInfo" [userId]="siteInfo.userid" [linkProfile]="false" slot="start"></core-user-avatar>
                 <ion-label>
                     <p class="item-heading">{{ siteInfo.fullname }}</p>
                 </ion-label>
diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts
index 48239c18b..f3ec0137b 100644
--- a/src/core/services/sites.ts
+++ b/src/core/services/sites.ts
@@ -1253,6 +1253,7 @@ export class CoreSitesProvider {
 
                 const basicInfo: CoreSiteBasicInfo = {
                     id: site.id,
+                    userId: siteInfo?.userid,
                     siteUrl: site.siteUrl,
                     siteUrlWithoutProtocol: site.siteUrl.replace(/^https?:\/\//, '').toLowerCase(),
                     fullname: siteInfo?.fullname,
@@ -2148,6 +2149,7 @@ export type CoreSiteUserTokenResponse = {
  */
 export type CoreSiteBasicInfo = {
     id: string; // Site ID.
+    userId?: number; // User ID.
     siteUrl: string; // Site URL.
     siteUrlWithoutProtocol: string; // Site URL without protocol.
     fullname?: string; // User's full name.