Add get user cache for comments

pull/2386/head
Chocobozzz 2020-01-06 16:13:18 +01:00
parent bc6f886347
commit 218b0874ed
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 17 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import { from, Observable } from 'rxjs' import { from, Observable, of } from 'rxjs'
import { catchError, concatMap, map, toArray } from 'rxjs/operators' import { catchError, concatMap, map, share, shareReplay, tap, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http' import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
@ -17,6 +17,8 @@ export class UserService {
private bytesPipe = new BytesPipe() private bytesPipe = new BytesPipe()
private userCache: { [ id: number ]: Observable<User> } = {}
constructor ( constructor (
private authHttp: HttpClient, private authHttp: HttpClient,
private restExtractor: RestExtractor, private restExtractor: RestExtractor,
@ -194,6 +196,14 @@ export class UserService {
) )
} }
getUserWithCache (userId: number) {
if (!this.userCache[userId]) {
this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
}
return this.userCache[userId]
}
getUser (userId: number) { getUser (userId: number) {
return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
.pipe(catchError(err => this.restExtractor.handleError(err))) .pipe(catchError(err => this.restExtractor.handleError(err)))

View File

@ -36,8 +36,9 @@
<div class="comment-actions"> <div class="comment-actions">
<div *ngIf="isUserLoggedIn()" (click)="onWantToReply()" class="comment-action-reply" i18n>Reply</div> <div *ngIf="isUserLoggedIn()" (click)="onWantToReply()" class="comment-action-reply" i18n>Reply</div>
<div *ngIf="isRemovableByUser()" (click)="onWantToDelete()" class="comment-action-delete" i18n>Delete</div> <div *ngIf="isRemovableByUser()" (click)="onWantToDelete()" class="comment-action-delete" i18n>Delete</div>
<my-user-moderation-dropdown <my-user-moderation-dropdown
buttonSize="small" [account]="commentAccount" [user]="commentUser" label="Options" placement="bottom-left auto" buttonSize="small" [account]="commentAccount" [user]="commentUser" i18n-label label="Options" placement="bottom-left auto"
></my-user-moderation-dropdown> ></my-user-moderation-dropdown>
</div> </div>
</ng-container> </ng-container>
@ -80,8 +81,8 @@
></my-video-comment> ></my-video-comment>
</div> </div>
</div> </div>
</div>
<ng-content></ng-content> <ng-content></ng-content>
</div>
</div> </div>
</div> </div>

View File

@ -107,7 +107,7 @@ export class VideoCommentComponent implements OnInit, OnChanges {
const user = this.authService.getUser() const user = this.authService.getUser()
if (user.hasRight(UserRight.MANAGE_USERS)) { if (user.hasRight(UserRight.MANAGE_USERS)) {
this.userService.getUser(account.userId) this.userService.getUserWithCache(account.userId)
.subscribe( .subscribe(
user => this.commentUser = user, user => this.commentUser = user,