Add get user cache for comments
authorChocobozzz <me@florianbigard.com>
Mon, 6 Jan 2020 15:13:18 +0000 (16:13 +0100)
committerChocobozzz <me@florianbigard.com>
Mon, 6 Jan 2020 15:13:26 +0000 (16:13 +0100)
client/src/app/shared/users/user.service.ts
client/src/app/videos/+video-watch/comment/video-comment.component.html
client/src/app/videos/+video-watch/comment/video-comment.component.ts

index 051e0bfa446a7f3120ec0c0016107979d879d6ff..e24d91df3fcac9b814bc5135034d3acd81e483ee 100644 (file)
@@ -1,5 +1,5 @@
-import { from, Observable } from 'rxjs'
-import { catchError, concatMap, map, toArray } from 'rxjs/operators'
+import { from, Observable, of } from 'rxjs'
+import { catchError, concatMap, map, share, shareReplay, tap, toArray } from 'rxjs/operators'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
@@ -17,6 +17,8 @@ export class UserService {
 
   private bytesPipe = new BytesPipe()
 
+  private userCache: { [ id: number ]: Observable<User> } = {}
+
   constructor (
     private authHttp: HttpClient,
     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) {
     return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
                .pipe(catchError(err => this.restExtractor.handleError(err)))
index 4753641bd9da6247f43660f22ec4dc70015564b6..246a0843533da0046f0e87e4ac2deb5e662f39c9 100644 (file)
@@ -36,8 +36,9 @@
         <div class="comment-actions">
           <div *ngIf="isUserLoggedIn()" (click)="onWantToReply()" class="comment-action-reply" i18n>Reply</div>
           <div *ngIf="isRemovableByUser()" (click)="onWantToDelete()" class="comment-action-delete" i18n>Delete</div>
+
           <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>
         </div>
       </ng-container>
@@ -80,8 +81,8 @@
           ></my-video-comment>
         </div>
       </div>
-    </div>
 
-    <ng-content></ng-content>
+      <ng-content></ng-content>
+    </div>
   </div>
 </div>
index 8c376d654b87f11f1a905618994db66d456859a5..0c18b6e5d5b872a030d34d37a327c4c78dae57c1 100644 (file)
@@ -107,7 +107,7 @@ export class VideoCommentComponent implements OnInit, OnChanges {
 
     const user = this.authService.getUser()
     if (user.hasRight(UserRight.MANAGE_USERS)) {
-      this.userService.getUser(account.userId)
+      this.userService.getUserWithCache(account.userId)
           .subscribe(
             user => this.commentUser = user,