diff --git a/webapp/src/app/core/services/api.service.ts b/webapp/src/app/core/services/api.service.ts index 5ffcd8b..dea2fb5 100644 --- a/webapp/src/app/core/services/api.service.ts +++ b/webapp/src/app/core/services/api.service.ts @@ -5,6 +5,7 @@ import { environment } from '../../../environments/environment'; import { User, UserWithExtra, + UserPreview, Message, MessageListParams, MessageListResponse, @@ -41,6 +42,10 @@ export class ApiService { return this.http.get(`${this.baseUrl}/users/${userId}`); } + getUserPreview(userId: string): Observable { + return this.http.get(`${this.baseUrl}/preview/users/${userId}`); + } + updateUser(userId: string, data: { username?: string; pro_token?: string }): Observable { return this.http.patch(`${this.baseUrl}/users/${userId}`, data); } diff --git a/webapp/src/app/core/services/index.ts b/webapp/src/app/core/services/index.ts index 1474f66..8964ce3 100644 --- a/webapp/src/app/core/services/index.ts +++ b/webapp/src/app/core/services/index.ts @@ -1,3 +1,4 @@ export * from './auth.service'; export * from './api.service'; export * from './notification.service'; +export * from './user-cache.service'; diff --git a/webapp/src/app/core/services/user-cache.service.ts b/webapp/src/app/core/services/user-cache.service.ts new file mode 100644 index 0000000..74b13b2 --- /dev/null +++ b/webapp/src/app/core/services/user-cache.service.ts @@ -0,0 +1,55 @@ +import { Injectable, inject, signal } from '@angular/core'; +import { Observable, of, tap, catchError, map, shareReplay } from 'rxjs'; +import { ApiService } from './api.service'; +import { AuthService } from './auth.service'; +import { UserPreview } from '../models'; + +export interface ResolvedUser { + userId: string; + displayName: string; + isCurrentUser: boolean; +} + +@Injectable({ + providedIn: 'root' +}) +export class UserCacheService { + private apiService = inject(ApiService); + private authService = inject(AuthService); + + private cache = new Map>(); + + resolveUser(userId: string): Observable { + const currentUserId = this.authService.getUserId(); + const isCurrentUser = userId === currentUserId; + + return this.getUserPreview(userId).pipe( + map(preview => { + let displayName = preview?.username || userId; + if (isCurrentUser) { + displayName += ' (you)'; + } + return { + userId, + displayName, + isCurrentUser + }; + }) + ); + } + + private getUserPreview(userId: string): Observable { + if (!this.cache.has(userId)) { + const request$ = this.apiService.getUserPreview(userId).pipe( + catchError(() => of(null)), + shareReplay(1) + ); + this.cache.set(userId, request$); + } + return this.cache.get(userId)!; + } + + clearCache(): void { + this.cache.clear(); + } +} diff --git a/webapp/src/app/features/auth/login/login.component.html b/webapp/src/app/features/auth/login/login.component.html index 36b660d..ee0a112 100644 --- a/webapp/src/app/features/auth/login/login.component.html +++ b/webapp/src/app/features/auth/login/login.component.html @@ -2,7 +2,6 @@