]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/auth/auth.service.ts
Client: add ability for user to change nsfw settings
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth.service.ts
index 2e73281976c2e6626289701f08fe26913373b04d..00a4216ef202b8f066ac4d5bcfcc57e289ff3e5b 100644 (file)
@@ -125,7 +125,7 @@ export class AuthService {
                       res.username = username;
                       return res;
                     })
-                    .flatMap(res => this.fetchUserInformations(res))
+                    .flatMap(res => this.mergeUserInformations(res))
                     .map(res => this.handleLogin(res))
                     .catch((res) => this.restExtractor.handleError(res));
   }
@@ -178,7 +178,23 @@ export class AuthService {
                     });
   }
 
-  private fetchUserInformations (obj: any) {
+  refreshUserInformations() {
+    const obj = {
+      access_token: this.user.getAccessToken()
+    };
+
+    this.mergeUserInformations(obj)
+        .subscribe(
+          res => {
+            this.user.displayNSFW = res.displayNSFW;
+            this.user.role = res.role;
+
+            this.user.save();
+          }
+        );
+  }
+
+  private mergeUserInformations(obj: { access_token: string }) {
     // Do not call authHttp here to avoid circular dependencies headaches
 
     const headers = new Headers();
@@ -187,9 +203,13 @@ export class AuthService {
     return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers })
              .map(res => res.json())
              .map(res => {
-               obj.id = res.id;
-               obj.role = res.role;
-               return obj;
+               const newProperties = {
+                 id: res.id,
+                 role: res.role,
+                 displayNSFW: res.displayNSFW
+               };
+
+               return Object.assign(obj, newProperties);
              }
     );
   }
@@ -198,13 +218,14 @@ export class AuthService {
     const id = obj.id;
     const username = obj.username;
     const role = obj.role;
+    const displayNSFW = obj.displayNSFW;
     const hashTokens = {
       access_token: obj.access_token,
       token_type: obj.token_type,
       refresh_token: obj.refresh_token
     };
 
-    this.user = new AuthUser({ id, username, role }, hashTokens);
+    this.user = new AuthUser({ id, username, role, displayNSFW }, hashTokens);
     this.user.save();
 
     this.setStatus(AuthStatus.LoggedIn);