Simple Managment webapp [LLM]
This commit is contained in:
@@ -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<UserWithExtra>(`${this.baseUrl}/users/${userId}`);
|
||||
}
|
||||
|
||||
getUserPreview(userId: string): Observable<UserPreview> {
|
||||
return this.http.get<UserPreview>(`${this.baseUrl}/preview/users/${userId}`);
|
||||
}
|
||||
|
||||
updateUser(userId: string, data: { username?: string; pro_token?: string }): Observable<User> {
|
||||
return this.http.patch<User>(`${this.baseUrl}/users/${userId}`, data);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './auth.service';
|
||||
export * from './api.service';
|
||||
export * from './notification.service';
|
||||
export * from './user-cache.service';
|
||||
|
||||
55
webapp/src/app/core/services/user-cache.service.ts
Normal file
55
webapp/src/app/core/services/user-cache.service.ts
Normal file
@@ -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<string, Observable<UserPreview | null>>();
|
||||
|
||||
resolveUser(userId: string): Observable<ResolvedUser> {
|
||||
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<UserPreview | null> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user