From e72ca2ad0daaf07f4577376cef1df19a86155c74 Mon Sep 17 00:00:00 2001
From: tekay <tom.kaesler@mni.thm.de>
Date: Tue, 7 Apr 2020 13:35:52 +0200
Subject: [PATCH] Map creatorId to a number

---
 .../comment-list/comment-list.component.ts    |  4 +--
 src/app/rx-stomp.config.ts                    |  4 +--
 src/app/services/http/comment.service.ts      | 30 +++++++++++++++++--
 tslint.json                                   |  2 +-
 4 files changed, 33 insertions(+), 7 deletions(-)

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 36d2a5714..d70d63185 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -196,6 +196,8 @@ export class CommentListComponent implements OnInit, OnDestroy {
         c.id = payload.id;
         c.timestamp = payload.timestamp;
         c.tag = payload.tag;
+        c.creatorId = payload.creatorId;
+        c.userNumber = this.commentService.hashCode(c.creatorId);
 
         this.announceNewComment(c.body);
 
@@ -309,8 +311,6 @@ export class CommentListComponent implements OnInit, OnDestroy {
   }
 
   filterComments(type: string, compare?: any): void {
-    console.log(type);
-    console.log(compare);
     this.currentFilter = type;
     if (type === '') {
       this.filteredComments = this.comments;
diff --git a/src/app/rx-stomp.config.ts b/src/app/rx-stomp.config.ts
index c1ab0b452..f6bdce0ee 100644
--- a/src/app/rx-stomp.config.ts
+++ b/src/app/rx-stomp.config.ts
@@ -3,8 +3,8 @@ import { environment } from './../environments/environment';
 
 export const ARSRxStompConfig: RxStompConfig = {
   // Which server?
-  brokerURL: (window.location.protocol === 'http:' ) ?
-    `ws://${window.location.hostname}:/api/ws/websocket` : `wss://${window.location.hostname}/api/ws/websocket`,
+  brokerURL: ((window.location.protocol === 'http:') ? 'ws' : 'wss')
+  + `://${window.location.host}/api/ws/websocket`,
 
   connectHeaders: {
     login: 'guest',
diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts
index bf5196cc9..1208e34c8 100644
--- a/src/app/services/http/comment.service.ts
+++ b/src/app/services/http/comment.service.ts
@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
 import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { Observable } from 'rxjs';
 import { Comment } from '../../models/comment';
-import { catchError, tap } from 'rxjs/operators';
+import { catchError, tap, map } from 'rxjs/operators';
 import { BaseHttpService } from './base-http.service';
 
 const httpOptions = {
@@ -25,6 +25,7 @@ export class CommentService extends BaseHttpService {
   getComment(commentId: string): Observable<Comment> {
     const connectionUrl = `${this.apiUrl.base}${this.apiUrl.comment}/${commentId}`;
     return this.http.get<Comment>(connectionUrl, httpOptions).pipe(
+      map(comment => this.parseUserNumber(comment)),
       tap(_ => ''),
       catchError(this.handleError<Comment>('getComment'))
     );
@@ -33,7 +34,8 @@ export class CommentService extends BaseHttpService {
   addComment(comment: Comment): Observable<Comment> {
     const connectionUrl = this.apiUrl.base + this.apiUrl.comment + '/';
     return this.http.post<Comment>(connectionUrl,
-      { roomId: comment.roomId, body: comment.body,
+      {
+        roomId: comment.roomId, body: comment.body,
         read: comment.read, creationTimestamp: comment.timestamp
       }, httpOptions).pipe(
         tap(_ => ''),
@@ -55,6 +57,9 @@ export class CommentService extends BaseHttpService {
       properties: { roomId: roomId, ack: true },
       externalFilters: {}
     }, httpOptions).pipe(
+      map(commentList => {
+        return commentList.map(comment => this.parseUserNumber(comment));
+      }),
       tap(_ => ''),
       catchError(this.handleError<Comment[]>('getComments', []))
     );
@@ -66,6 +71,9 @@ export class CommentService extends BaseHttpService {
       properties: { roomId: roomId, ack: false },
       externalFilters: {}
     }, httpOptions).pipe(
+      map(commentList => {
+        return commentList.map(comment => this.parseUserNumber(comment));
+      }),
       tap(_ => ''),
       catchError(this.handleError<Comment[]>('getComments', []))
     );
@@ -77,6 +85,9 @@ export class CommentService extends BaseHttpService {
       properties: { roomId: roomId },
       externalFilters: {}
     }, httpOptions).pipe(
+      map(commentList => {
+        return commentList.map(comment => this.parseUserNumber(comment));
+      }),
       tap(_ => ''),
       catchError(this.handleError<Comment[]>('getComments', []))
     );
@@ -108,4 +119,19 @@ export class CommentService extends BaseHttpService {
       catchError(this.handleError<number>('countByRoomId', 0))
     );
   }
+
+  parseUserNumber(comment: Comment): Comment {
+    comment.userNumber = this.hashCode(comment.creatorId);
+    return comment;
+  }
+
+  hashCode(s) {
+    let hash;
+    for (let i = 0, h = 0; i < s.length; i++) {
+      hash = Math.abs(Math.imul(31, hash) + s.charCodeAt(i) | 0);
+    }
+    const userNumberString = String(hash);
+    hash = +userNumberString.substring(userNumberString.length - 4, userNumberString.length);
+    return hash;
+  }
 }
diff --git a/tslint.json b/tslint.json
index 999182156..1ea028f4b 100644
--- a/tslint.json
+++ b/tslint.json
@@ -47,7 +47,7 @@
       }
     ],
     "no-arg": true,
-    "no-bitwise": true,
+    "no-bitwise": false,
     "no-console": [
       true,
       "debug",
-- 
GitLab