From 54a27d41f4b7f47adfba4ccd1596b5335e25414e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Sun, 28 Jul 2019 01:45:51 +0200
Subject: [PATCH] Have a flag that indicates moderation of session

Use flag to only show link to comment moderation stream when room is moderated
Fix bugs in settings
---
 .../comment-settings.component.ts             |  2 +-
 .../room-creator-page.component.html          |  4 +--
 .../room-creator-page.component.ts            | 26 +++++++++++++------
 .../room-moderator-page.component.html        |  4 +--
 .../room-moderator-page.component.ts          |  7 +++++
 .../comment-list/comment-list.component.ts    | 16 ++++++++++--
 .../shared/room-page/room-page.component.ts   | 11 ++++++++
 7 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/src/app/components/creator/_dialogs/comment-settings/comment-settings.component.ts b/src/app/components/creator/_dialogs/comment-settings/comment-settings.component.ts
index 3b5f20848..3e4d7a1fa 100644
--- a/src/app/components/creator/_dialogs/comment-settings/comment-settings.component.ts
+++ b/src/app/components/creator/_dialogs/comment-settings/comment-settings.component.ts
@@ -23,7 +23,7 @@ export class CommentSettingsComponent implements OnInit {
 
   roomId: string;
   comments: Comment[];
-  commentThreshold: number;
+  commentThreshold = -10;
   editRoom: Room;
   settingThreshold = false;
   enableCommentModeration = false;
diff --git a/src/app/components/creator/room-creator-page/room-creator-page.component.html b/src/app/components/creator/room-creator-page/room-creator-page.component.html
index 9c5cb8214..7c3a322e1 100644
--- a/src/app/components/creator/room-creator-page/room-creator-page.component.html
+++ b/src/app/components/creator/room-creator-page/room-creator-page.component.html
@@ -42,7 +42,7 @@
           {{ room.description.trim() }}
         </h4>
       </mat-card-content>
-      <mat-grid-list cols="2" rowHeight="2:1">
+      <mat-grid-list cols="{{viewModuleCount}}" rowHeight="2:1">
         <mat-grid-tile>
           <button mat-icon-button
                   routerLink="/creator/room/{{ room.shortId }}/comments">
@@ -51,7 +51,7 @@
             <h3>{{ 'room-page.comments' | translate}}</h3> <!-- *ngIf="deviceType === 'desktop'" -->
           </button>
         </mat-grid-tile>
-        <mat-grid-tile>
+        <mat-grid-tile *ngIf="moderationEnabled">
           <button mat-icon-button routerLink="/moderator/room/{{ room.shortId }}/moderator/comments">
             <mat-icon matBadge="{{moderatorCommentCounter}}" matBadgeColor="primary"
                       [ngClass]="{'desktop' : deviceType === 'desktop'}">gavel
diff --git a/src/app/components/creator/room-creator-page/room-creator-page.component.ts b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
index 8f5a0bc14..0a64a29af 100644
--- a/src/app/components/creator/room-creator-page/room-creator-page.component.ts
+++ b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
@@ -27,6 +27,7 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   commentThreshold: number;
   updCommentThreshold: number;
   deviceType = localStorage.getItem('deviceType');
+  viewModuleCount = 1;
 
   constructor(protected roomService: RoomService,
               protected notification: NotificationService,
@@ -49,6 +50,12 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
     });
   }
 
+  afterRoomLoadHook() {
+    if (this.moderationEnabled) {
+      this.viewModuleCount = this.viewModuleCount + 1;
+    }
+  }
+
   updateGeneralSettings() {
     this.room.name = this.updRoom.name;
     this.room.description = this.updRoom.description;
@@ -58,13 +65,18 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   updateCommentSettings(settings: CommentSettingsDialog) {
     const commentExtension: TSMap<string, any> = new TSMap();
     this.room.extensions = new TSMap();
+    commentExtension.set('enableThreshold', settings.enableThreshold);
+    commentExtension.set('commentThreshold', settings.threshold);
+    commentExtension.set('enableModeration', settings.enableModeration);
     this.room.extensions.set('comments', commentExtension);
-    if (settings.enableThreshold) {
-      commentExtension.set('commentThreshold', settings.threshold);
-    }
-    if (settings.enableModeration) {
-      commentExtension.set('enableModeration', settings.enableModeration);
+
+    if (this.moderationEnabled && !settings.enableModeration) {
+      this.viewModuleCount = this.viewModuleCount - 1;
+    } else if (!this.moderationEnabled && settings.enableModeration) {
+      this.viewModuleCount = this.viewModuleCount + 1;
     }
+
+    this.moderationEnabled = settings.enableModeration;
   }
 
   resetThreshold(): void {
@@ -116,9 +128,7 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
           return;
         } else {
           if (result instanceof CommentSettingsDialog) {
-            if (!result.enableThreshold) {
-              this.resetThreshold();
-            }
+            console.log(result);
             this.updateCommentSettings(result);
             this.saveChanges();
           }
diff --git a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.html b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.html
index 7e17a0300..a08b44fca 100644
--- a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.html
+++ b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.html
@@ -20,7 +20,7 @@
         <mat-card-content *ngIf="room.description" fxLayoutAlign="center">
           <h4>{{room.description.trim()}}</h4>
         </mat-card-content>
-        <mat-grid-list cols="2" rowHeight="2:1">
+        <mat-grid-list cols="{{viewModuleCount}}" rowHeight="2:1">
             <mat-grid-tile>
               <button mat-icon-button routerLink="/moderator/room/{{ room.shortId }}/comments">
                 <mat-icon matBadge="{{commentCounter}}" matBadgeColor="primary"
@@ -29,7 +29,7 @@
                 <h3>{{ 'room-page.public-stream' | translate}}</h3>         <!-- *ngIf="deviceType === 'desktop'" -->
               </button>
             </mat-grid-tile>
-            <mat-grid-tile>
+            <mat-grid-tile *ngIf="moderationEnabled">
               <button mat-icon-button routerLink="/moderator/room/{{ room.shortId }}/moderator/comments">
                 <mat-icon matBadge="{{moderatorCommentCounter}}" matBadgeColor="primary"
                           [ngClass]="{'desktop' : deviceType === 'desktop'}">gavel
diff --git a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
index 503ef6c5f..1374b6927 100644
--- a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
+++ b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
@@ -21,6 +21,7 @@ export class RoomModeratorPageComponent extends RoomPageComponent implements OnI
   isLoading = true;
   deviceType = localStorage.getItem('deviceType');
   moderatorCommentCounter: number;
+  viewModuleCount = 1;
 
 
   constructor(protected location: Location,
@@ -36,6 +37,12 @@ export class RoomModeratorPageComponent extends RoomPageComponent implements OnI
 
   initializeRoom(id: string): void {
     this.roomService.getRoomByShortId(id).subscribe(room => {
+      if (this.room && this.room.extensions && this.room.extensions['comments']) {
+        if (this.room.extensions['comments'].enableModeration !== null) {
+          this.moderationEnabled = this.room.extensions['comments'].enableModeration;
+          this.viewModuleCount = this.viewModuleCount + 1;
+        }
+      }
       this.room = room;
       this.isLoading = false;
       this.commentService.countByRoomId(this.room.id, true)
diff --git a/src/app/components/shared/comment-list/comment-list.component.ts b/src/app/components/shared/comment-list/comment-list.component.ts
index 94bde1414..c24f03cd9 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -46,6 +46,8 @@ export class CommentListComponent implements OnInit {
   searchInput = '';
   search = false;
   searchPlaceholder = '';
+  moderationEnabled = false;
+  thresholdEnabled = false;
 
   constructor(private commentService: CommentService,
               private translateService: TranslateService,
@@ -62,7 +64,17 @@ export class CommentListComponent implements OnInit {
     this.roomId = localStorage.getItem(`roomId`);
     const userId = this.user.id;
     this.userRole = this.user.role;
-    this.roomService.getRoom(this.roomId).subscribe( room => this.room = room);
+    this.roomService.getRoom(this.roomId).subscribe( room => {
+      this.room = room;
+      if (this.room && this.room.extensions && this.room.extensions['comments']) {
+        if (this.room.extensions['comments'].commentThreshold !== null) {
+          this.thresholdEnabled = true;
+        }
+        if (this.room.extensions['comments'].enableModeration !== null) {
+          this.moderationEnabled = this.room.extensions['comments'].enableModeration;
+        }
+      }
+    });
     this.hideCommentsList = false;
     this.wsCommentService.getCommentStream(this.roomId).subscribe((message: Message) => {
       this.parseIncomingMessage(message);
@@ -115,7 +127,7 @@ export class CommentListComponent implements OnInit {
   getComments(): void {
     this.isLoading = false;
     let commentThreshold = -10;
-    if (this.room && this.room.extensions && this.room.extensions['comments']) {
+    if (this.thresholdEnabled) {
       commentThreshold = this.room.extensions['comments'].commentThreshold;
       if (this.hideCommentsList) {
         this.filteredComments = this.filteredComments.filter( x => x.score >= commentThreshold );
diff --git a/src/app/components/shared/room-page/room-page.component.ts b/src/app/components/shared/room-page/room-page.component.ts
index 90a826576..7e739e637 100644
--- a/src/app/components/shared/room-page/room-page.component.ts
+++ b/src/app/components/shared/room-page/room-page.component.ts
@@ -16,6 +16,7 @@ export class RoomPageComponent implements OnInit {
   room: Room = null;
   isLoading = true;
   commentCounter: number;
+  protected moderationEnabled = false;
 
   constructor(protected roomService: RoomService,
               protected route: ActivatedRoute,
@@ -31,10 +32,19 @@ export class RoomPageComponent implements OnInit {
     });
   }
 
+  protected afterRoomLoadHook() {
+
+  }
+
   initializeRoom(id: string): void {
     this.roomService.getRoomByShortId(id).subscribe(room => {
       this.room = room;
       this.isLoading = false;
+      if (this.room && this.room.extensions && this.room.extensions['comments']) {
+        if (this.room.extensions['comments'].enableModeration !== null) {
+          this.moderationEnabled = this.room.extensions['comments'].enableModeration;
+        }
+      }
       this.commentService.countByRoomId(this.room.id, true)
         .subscribe(commentCounter => {
           this.commentCounter = commentCounter;
@@ -48,6 +58,7 @@ export class RoomPageComponent implements OnInit {
           this.commentCounter = this.commentCounter - 1;
         }
       });
+      this.afterRoomLoadHook();
     });
   }
 
-- 
GitLab