From 77eaf923e438f86baea6ac77d7e27889707a6eee Mon Sep 17 00:00:00 2001 From: mohammad <mohammad.alayoub@mni.thm.de> Date: Fri, 25 Jun 2021 02:01:52 +0200 Subject: [PATCH] filter profanity after updating the room --- .../comment-answer.component.ts | 2 +- .../shared/comment/comment.component.ts | 12 +-- src/app/services/util/room-data.service.ts | 84 +++++++++++-------- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/src/app/components/shared/comment-answer/comment-answer.component.ts b/src/app/components/shared/comment-answer/comment-answer.component.ts index e9245c318..c4641fc14 100644 --- a/src/app/components/shared/comment-answer/comment-answer.component.ts +++ b/src/app/components/shared/comment-answer/comment-answer.component.ts @@ -43,7 +43,7 @@ export class CommentAnswerComponent implements OnInit { this.isStudent = false; } this.route.params.subscribe(params => { - this.roomDataService.getComment(params['commentId']).subscribe(comment => { + this.commentService.getComment(params['commentId']).subscribe(comment => { this.comment = comment; this.answer = this.comment.answer; this.isLoading = false; diff --git a/src/app/components/shared/comment/comment.component.ts b/src/app/components/shared/comment/comment.component.ts index a6d38e7ed..c8928f42f 100644 --- a/src/app/components/shared/comment/comment.component.ts +++ b/src/app/components/shared/comment/comment.component.ts @@ -98,6 +98,12 @@ export class CommentComponent implements OnInit, AfterViewInit { this.inAnswerView = !this.router.url.includes('comments'); } + checkProfanity(){ + if (!this.router.url.includes('moderator/comments')) { + this.roomDataService.checkProfanity(this.comment); + } + } + ngAfterViewInit(): void { this.isExpandable = this.commentBody.getRenderedHeight() > CommentComponent.COMMENT_MAX_HEIGHT; if (!this.isExpandable) { @@ -108,12 +114,6 @@ export class CommentComponent implements OnInit, AfterViewInit { } } - checkProfanity(){ - if (!this.router.url.includes('moderator/comments')) { - this.roomDataService.checkProfanity(this.comment); - } - } - toggleExpand(evt: MouseEvent) { this.isExpanded = !this.isExpanded; if (this.isExpanded) { diff --git a/src/app/services/util/room-data.service.ts b/src/app/services/util/room-data.service.ts index 98854e0ac..d7b0a9fbf 100644 --- a/src/app/services/util/room-data.service.ts +++ b/src/app/services/util/room-data.service.ts @@ -8,6 +8,7 @@ import { CorrectWrong } from '../../models/correct-wrong.enum'; import { RoomService } from '../http/room.service'; import { TopicCloudAdminService } from './topic-cloud-admin.service'; import { ProfanityFilter, Room } from '../../models/room'; +import { WsRoomService } from '..//websockets/ws-room.service'; export interface UpdateInformation { type: 'CommentCreated' | 'CommentPatched' | 'CommentHighlighted' | 'CommentDeleted'; @@ -93,11 +94,13 @@ export class RoomDataService { private _currentRoomId: string = null; private _savedCommentsBeforeFilter = new Map(); private _savedCommentsAfterFilter = new Map(); + private room: Room; constructor(private wsCommentService: WsCommentService, private commentService: CommentService, private roomService: RoomService, - private topicCloudAdminService: TopicCloudAdminService) { + private topicCloudAdminService: TopicCloudAdminService, + private wsRoomService: WsRoomService) { } get currentRoomData() { @@ -127,38 +130,43 @@ export class RoomDataService { return tempSubject.asObservable(); } - getComment(id: string): Observable<Comment> { - if (!this._fastCommentAccess[id]) { - const comment = new Subject<Comment>(); - this.commentService.getComment(id).subscribe(c => { + public checkProfanity(comment: Comment){ + const finish = new Subject<boolean>(); + const subscription = finish.asObservable().subscribe(_ => { + if (this.room.profanityFilter !== ProfanityFilter.deactived) { + comment.body = this._savedCommentsAfterFilter.get(comment.id); + } else { + comment.body = this._savedCommentsBeforeFilter.get(comment.id); + } + subscription.unsubscribe(); + }); + + if (!this._savedCommentsAfterFilter.get(comment.id) || !this.room) { + if (!this.room) { this.roomService.getRoom(localStorage.getItem('roomId')).subscribe(room => { - if (room.profanityFilter) { - c.body = this.filterCommentOfProfanity(room, c); - } - comment.next(c); + this.room = room; + this.setCommentBody(comment); + finish.next(true); }); - }); - return comment.asObservable(); + } else { + this.setCommentBody(comment); + finish.next(true); + } } else { - this.checkProfanity(this._fastCommentAccess[id]); - return of(this._fastCommentAccess[id]); + finish.next(true); } } - public checkProfanity(comment: Comment) { - this.roomService.getRoom(localStorage.getItem('roomId')).subscribe(room => { - if (room.profanityFilter !== ProfanityFilter.deactived) { - comment.body = this._savedCommentsAfterFilter.get(comment.id); - } else { - comment.body = this._savedCommentsBeforeFilter.get(comment.id); - } - }); + private setCommentBody(comment: Comment) { + this._savedCommentsBeforeFilter.set(comment.id, comment.body); + this._savedCommentsAfterFilter.set(comment.id, this.filterCommentOfProfanity(this.room, comment)); } - private setCommentBodies(comment: Comment) { - this.roomService.getRoom(localStorage.getItem('roomId')).subscribe(room => { - this._savedCommentsBeforeFilter.set(comment.id, comment.body); - this._savedCommentsAfterFilter.set(comment.id, this.filterCommentOfProfanity(room, comment)); + private filterAllCommentsBodies(){ + this._currentComments.forEach(comment => { + comment.body = this._savedCommentsBeforeFilter.get(comment.id); + this.setCommentBody(comment); + this.checkProfanity(comment); }); } @@ -185,14 +193,24 @@ export class RoomDataService { this._wsCommentServiceSubscription.unsubscribe(); } this._wsCommentServiceSubscription = this.wsCommentService.getCommentStream(roomId) - .subscribe(msg => this.onMessageReceive(msg)); - this.commentService.getAckComments(roomId).subscribe(comments => { - this._currentComments = comments; - for (const comment of comments) { - this.setCommentBodies(comment); - this._fastCommentAccess[comment.id] = comment; + .subscribe(msg => this.onMessageReceive(msg)); + this.roomService.getRoom(roomId).subscribe(room => { + this.room = room; + this.commentService.getAckComments(roomId).subscribe(comments => { + this._currentComments = comments; + for (const comment of comments) { + this.setCommentBody(comment); + this._fastCommentAccess[comment.id] = comment; + } + this.triggerUpdate(UpdateType.force, null); + }); + }); + this.wsRoomService.getRoomStream(roomId).subscribe(msg => { + const message = JSON.parse(msg.body); + if (message.type === 'RoomPatched') { + this.room = message.payload.changes; + this.filterAllCommentsBodies(); } - this.triggerUpdate(UpdateType.force, null); }); } @@ -242,7 +260,7 @@ export class RoomDataService { this.commentService.getComment(c.id).subscribe(comment => { this._fastCommentAccess[comment.id] = comment; this._currentComments.push(comment); - this.setCommentBodies(comment); + this.setCommentBody(comment); this.triggerUpdate(UpdateType.commentStream, { type: 'CommentCreated', finished: true, -- GitLab