From 1c26558bf4a7d3cab6b01d8796af913cd1df37d5 Mon Sep 17 00:00:00 2001
From: mohammad <mohammad.alayoub@mni.thm.de>
Date: Fri, 14 May 2021 19:13:55 +0200
Subject: [PATCH] replace bad words with stars

---
 .../topic-cloud-administration.component.ts   |  3 +-
 .../topic-dialog-comment.component.ts         | 35 +++++++++++++------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
index 48536b11e..62f743558 100644
--- a/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
+++ b/src/app/components/shared/_dialogs/topic-cloud-administration/topic-cloud-administration.component.ts
@@ -78,7 +78,7 @@ export class TopicCloudAdministrationComponent implements OnInit {
       questions: [
         'Das ist eine Testfrage fuer den Profanity Filter, du Arschloch',
         'Fuck you!',
-        'fuck Fuck ficken cunt',
+        'fuck Fuck hiii cunt',
         'fuck'
       ]
     },
@@ -228,7 +228,6 @@ export class TopicCloudAdministrationComponent implements OnInit {
     }
     return undefined;
   }
-
 }
 
 interface Keyword {
diff --git a/src/app/components/shared/dialog/topic-dialog-comment/topic-dialog-comment.component.ts b/src/app/components/shared/dialog/topic-dialog-comment/topic-dialog-comment.component.ts
index 874dac5e4..0994d89a2 100644
--- a/src/app/components/shared/dialog/topic-dialog-comment/topic-dialog-comment.component.ts
+++ b/src/app/components/shared/dialog/topic-dialog-comment/topic-dialog-comment.component.ts
@@ -14,27 +14,42 @@ export class TopicDialogCommentComponent implements OnInit {
   @Input() isCollapsed = false;
   @Input() profanityFilter = true;
   public badWords = [];
+  questionWithProfinity: string = undefined;
 
   public shortQuestion: string;
 
   constructor() { }
 
-  get partsOfQuestion() {
-    // return this.question.slice(0,this.isCollapsed? this.question.length: this.maxShowedCharachters).split(new RegExp(this.keyword,'i'));
-
-    const q = this.question.slice(0,this.isCollapsed? this.question.length: this.maxShowedCharachters);
-    // let q2 = q.split(new RegExp('(' + this.keyword + ')', 'i'));
+  get partsOfQuestion() {    
+    const q = this.profanityFilter ? this.questionWithProfinity.slice(0,this.isCollapsed? this.question.length: this.maxShowedCharachters) :
+              this.question.slice(0,this.isCollapsed? this.question.length: this.maxShowedCharachters);
     const q2 = q.split(' ');
     return q2;
   }
 
   ngOnInit(): void {
     this.badWords = BadWordsList.array;
+    this.filterProfanityWords();
+  }
+  
+  filterProfanityWords(){
+    this.questionWithProfinity = this.question;
+      this.badWords.map(word =>{
+        this.questionWithProfinity = this.questionWithProfinity.toLowerCase().includes(word) ?
+        this.replaceString(this.questionWithProfinity.toLowerCase(), word, this.generateXWord(word.length))
+        : this.questionWithProfinity;
+      });
+  }
+
+  replaceString(str: string, search: string, replace: string){
+    return str.split(search).join(replace);
+  }
 
-    // if (this.question.length > this.maxShowedCharachters) {
-    //   this.shortQuestion = this.question.slice(0, this.maxShowedCharachters).concat('...');
-    // } else {
-    //   this.shortQuestion = this.question;
-    // }
+  generateXWord(count: number){
+    let res = '';
+    for (let i = 0; i < count; i++){
+      res += '*';
+    }
+    return res;
   }
 }
-- 
GitLab