diff --git a/src/app/components/moderator/moderator-comment-list/moderator-comment-list.component.ts b/src/app/components/moderator/moderator-comment-list/moderator-comment-list.component.ts index 89afc96ef053467ae2485be1cfc1279eea0690fb..67b350f749a4c56f28e0e964f20e051c387c1f4c 100644 --- a/src/app/components/moderator/moderator-comment-list/moderator-comment-list.component.ts +++ b/src/app/components/moderator/moderator-comment-list/moderator-comment-list.component.ts @@ -64,7 +64,7 @@ export class ModeratorCommentListComponent implements OnInit, OnDestroy { search = false; searchPlaceholder = ''; periodsList = Object.values(Period); - period: Period = Period.TWOWEEKS; + period: Period = Period.twoWeeks; fromNow: number; headerInterface = null; @@ -191,11 +191,11 @@ export class ModeratorCommentListComponent implements OnInit, OnDestroy { } private getCurrentFilter() { - let filter = new CommentFilter(); + const filter = new CommentFilter(); filter.filterSelected = this.currentFilter; filter.periodSet = this.period; - if (filter.periodSet == Period.FROMNOW) { + if (filter.periodSet === Period.fromNow) { filter.timeStampNow = new Date().getTime(); } @@ -409,32 +409,32 @@ export class ModeratorCommentListComponent implements OnInit, OnDestroy { const currentTime = new Date(); const hourInSeconds = 3600000; let periodInSeconds; - if (this.period !== Period.ALL) { + if (this.period !== Period.all) { switch (this.period) { - case Period.FROMNOW: + case Period.fromNow: if (!this.fromNow) { this.fromNow = new Date().getTime(); } break; - case Period.ONEHOUR: + case Period.oneHour: periodInSeconds = hourInSeconds; break; - case Period.THREEHOURS: + case Period.threeHours: periodInSeconds = hourInSeconds * 2; break; - case Period.ONEDAY: + case Period.oneDay: periodInSeconds = hourInSeconds * 24; break; - case Period.ONEWEEK: + case Period.oneWeek: periodInSeconds = hourInSeconds * 168; break; - case Period.TWOWEEKS: + case Period.twoWeeks: periodInSeconds = hourInSeconds * 336; break; } this.commentsFilteredByTime = this.comments .filter(c => new Date(c.timestamp).getTime() >= - (this.period === Period.FROMNOW ? this.fromNow : (currentTime.getTime() - periodInSeconds))); + (this.period === Period.fromNow ? this.fromNow : (currentTime.getTime() - periodInSeconds))); } else { this.commentsFilteredByTime = this.comments; } 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 db5ac69e40be8cd074326da35d0c39ff5456f08f..692bdea3a8d308bd2601d5f728e46c79587ab30b 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 @@ -98,7 +98,7 @@ export class TopicCloudAdministrationComponent implements OnInit, OnDestroy { } updateKeywords(){ - this.commentService.getFilteredComments(localStorage.getItem('roomId')).subscribe(comments => { + this.commentService.getAckComments(localStorage.getItem('roomId')).subscribe(comments => { this.keywords = []; comments.forEach(comment => { let keywords: string[]; diff --git a/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.html b/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.html index aca94ca9f9645153ea046c0973328fea0cc8ae4e..8838a59c151580b2f44d30605c35a1a93f4b7c81 100644 --- a/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.html +++ b/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.html @@ -7,12 +7,12 @@ <mat-radio-button value="continueWithAll"> <div class="elementRow"> <div class="elementText"> - {{'content.continue-with-all-questions' | translate}} + {{'content.continue-with-all-questions' | translate}} </div> <div class="elementIcons"> - <mat-icon [inline]="true">comment</mat-icon> {{allCommentsCount}} - <mat-icon [inline]="true">person</mat-icon> {{allCommentsUsers}} - <mat-icon svgIcon="hashtag" class="comment_tag-icon"></mat-icon> {{allCommentsKeywords}} + <mat-icon [inline]="true">comment</mat-icon> {{allComments.comments}} + <mat-icon [inline]="true">person</mat-icon> {{allComments.users}} + <mat-icon svgIcon="hashtag" class="comment_tag-icon"></mat-icon> {{allComments.keywords}} </div> </div> </mat-radio-button> @@ -22,9 +22,9 @@ {{'content.continue-with-current-questions' | translate}} </div> <div class="elementIcons"> - <mat-icon [inline]="true">comment</mat-icon> {{filteredCommentsCount}} - <mat-icon [inline]="true">person</mat-icon> {{filteredCommentsUsers}} - <mat-icon svgIcon="hashtag" class="comment_tag-icon"></mat-icon> {{filteredCommentsKeywords}} + <mat-icon [inline]="true">comment</mat-icon> {{filteredComments.comments}} + <mat-icon [inline]="true">person</mat-icon> {{filteredComments.users}} + <mat-icon svgIcon="hashtag" class="comment_tag-icon"></mat-icon> {{filteredComments.keywords}} </div> </div> </mat-radio-button> diff --git a/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.ts b/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.ts index 26d3c422be906c8d9eb8a76f9053c0a5544863cf..f86b23334a449a41725206123391f314cabfeffd 100644 --- a/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.ts +++ b/src/app/components/shared/_dialogs/topic-cloud-filter/topic-cloud-filter.component.ts @@ -7,13 +7,12 @@ import { LanguageService } from '../../../../services/util/language.service'; import { EventService } from '../../../../services/util/event.service'; import { Router } from '@angular/router'; import { CommentFilter } from '../../../../utils/filter-options'; -import { CommentService } from '../../../../services/http/comment.service'; import { RoomService } from '../../../../services/http/room.service'; import { Comment } from '../../../../models/comment'; - +import { CommentListData } from '../../comment-list/comment-list.component'; class CommentsCount { - comments : number; + comments: number; users: number; keywords: number; } @@ -24,23 +23,14 @@ class CommentsCount { styleUrls: ['./topic-cloud-filter.component.scss'] }) export class TopicCloudFilterComponent implements OnInit { - @Input() filteredComments: any; - @Input() commentsFilteredByTime: any; @Input() shortId: string; continueFilter = 'continueWithCurr'; - - tmpFilter : CommentFilter; - allCommentsCount : number; - allCommentsUsers : number; - allCommentsKeywords : number; - - filteredCommentsCount : number; - filteredCommentsUsers : number; - filteredCommentsKeywords : number; - - disableCurrentFiltersOptions : boolean = false; - commentsLoaded : boolean = false; + comments: Comment[]; + tmpFilter: CommentFilter; + allComments: CommentsCount; + filteredComments: CommentsCount; + disableCurrentFiltersOptions = false; constructor(public dialogRef: MatDialogRef<RoomCreatorPageComponent>, public dialog: MatDialog, @@ -49,7 +39,6 @@ export class TopicCloudFilterComponent implements OnInit { protected langService: LanguageService, private router: Router, protected roomService: RoomService, - private commentService: CommentService, @Inject(MAT_DIALOG_DATA) public data: any, public eventService: EventService) { langService.langEmitter.subscribe(lang => translationService.use(lang)); @@ -57,49 +46,32 @@ export class TopicCloudFilterComponent implements OnInit { ngOnInit() { this.translationService.use(localStorage.getItem('currentLang')); - this.tmpFilter = CommentFilter.currentFilter; - localStorage.setItem("filtertmp", JSON.stringify(this.tmpFilter)); - - this.roomService.getRoomByShortId(this.shortId).subscribe(room => { - this.commentService.getAckComments(room.id).subscribe(comments => { - const counts = this.getCommentCounts(comments); - this.allCommentsCount = counts.comments; - this.allCommentsUsers = counts.users; - this.allCommentsKeywords = counts.keywords; - this.commentsLoadedCallback(); - }); - this.commentService.getFilteredComments(room.id).subscribe(comments => { - const counts = this.getCommentCounts(comments); - this.filteredCommentsCount = counts.comments; - this.filteredCommentsUsers = counts.users; - this.filteredCommentsKeywords = counts.keywords; - this.commentsLoadedCallback(); - }); + const subscriptionEventService = this.eventService.on<CommentListData>('currentRoomData').subscribe(data => { + subscriptionEventService.unsubscribe(); + this.comments = data.comments; + this.tmpFilter = data.currentFilter; + this.allComments = this.getCommentCounts(this.comments); + this.filteredComments = this.getCommentCounts(this.comments.filter(comment => this.tmpFilter.checkComment(comment))); + this.commentsLoadedCallback(); }); + this.eventService.broadcast('pushCurrentRoomData'); } commentsLoadedCallback() { - if (!this.commentsLoaded) { - this.commentsLoaded = true; - } else { - this.disableCurrentFiltersOptions = ((this.allCommentsCount == this.filteredCommentsCount) && - (this.allCommentsUsers == this.filteredCommentsUsers) && - (this.allCommentsKeywords == this.filteredCommentsKeywords)); - - if (this.disableCurrentFiltersOptions) { - this.continueFilter = 'continueWithAll'; - } + this.disableCurrentFiltersOptions = ((this.allComments.comments === this.filteredComments.comments) && + (this.allComments.users === this.filteredComments.users) && + (this.allComments.keywords === this.filteredComments.keywords)); + + if (this.disableCurrentFiltersOptions) { + this.continueFilter = 'continueWithAll'; } } - closeDialog(): void { - } + getCommentCounts(comments: Comment[]): CommentsCount { + const counts = new CommentsCount(); + const userSet = new Set<number>(); + const keywordSet = new Set<string>(); - getCommentCounts(comments : Comment[]) : CommentsCount { - let counts = new CommentsCount(); - let userSet = new Set<number>(); - let keywordSet = new Set<string>(); - comments.forEach(c => { if (c.userNumber) { userSet.add(c.userNumber); @@ -122,28 +94,28 @@ export class TopicCloudFilterComponent implements OnInit { } confirmButtonActionCallback() { - return () => { - let filter : CommentFilter; - + return () => { + let filter: CommentFilter; + switch (this.continueFilter) { case 'continueWithAll': filter = new CommentFilter(); // all questions allowed break; - + case 'continueWithAllFromNow': filter = CommentFilter.generateFilterNow(this.tmpFilter.filterSelected); break; - + case 'continueWithCurr': - filter = JSON.parse(localStorage.getItem("filtertmp")) as CommentFilter; + filter = this.tmpFilter; break; - + default: return; } - + CommentFilter.currentFilter = filter; this.dialogRef.close(this.router.navigateByUrl('/participant/room/' + this.shortId + '/comments/tagcloud')); - } + }; } -} \ No newline at end of file +} 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 969f0c49b712726a76213890aa01c86c14275ba8..9ff00fb051840ba38827816fc80312cd068d18ee 100644 --- a/src/app/components/shared/comment-list/comment-list.component.ts +++ b/src/app/components/shared/comment-list/comment-list.component.ts @@ -27,17 +27,19 @@ import { DeleteCommentsComponent } from '../../creator/_dialogs/delete-comments/ import { Export } from '../../../models/export'; import { BonusTokenService } from '../../../services/http/bonus-token.service'; import { ModeratorService } from '../../../services/http/moderator.service'; -import { TopicCloudFilterComponent } from '../_dialogs/topic-cloud-filter/topic-cloud-filter.component'; import { CommentFilter, Period } from '../../../utils/filter-options'; -import { isObjectBindingPattern } from 'typescript'; import { CreateCommentWrapper } from '../../../utils/CreateCommentWrapper'; +export interface CommentListData { + comments: Comment[]; + currentFilter: CommentFilter; +} + @Component({ selector: 'app-comment-list', templateUrl: './comment-list.component.html', styleUrls: ['./comment-list.component.scss'], }) - export class CommentListComponent implements OnInit, OnDestroy { @ViewChild('searchBox') searchField: ElementRef; @Input() user: User; @@ -89,11 +91,13 @@ export class CommentListComponent implements OnInit, OnDestroy { commentStream: Subscription; periodsList = Object.values(Period); headerInterface = null; - period: Period = Period.TWOWEEKS; + period: Period = Period.twoWeeks; fromNow: number; moderatorIds: string[]; commentsEnabled: boolean; createCommentWrapper: CreateCommentWrapper = null; + private _subscriptionEventServiceTagConfig = null; + private _subscriptionEventServiceRoomData = null; constructor( private commentService: CommentService, @@ -118,6 +122,15 @@ export class CommentListComponent implements OnInit, OnDestroy { } initNavigation() { + this._subscriptionEventServiceTagConfig = this.eventService.on<string>('setTagConfig').subscribe(tag => { + this.clickedOnKeyword(tag); + }); + this._subscriptionEventServiceRoomData = this.eventService.on<string>('pushCurrentRoomData').subscribe(_ => { + this.eventService.broadcast('currentRoomData', { + currentFilter: this.getCurrentFilter(), + comments: this.comments + } as CommentListData); + }); const navigation = {}; const nav = (b, c) => navigation[b] = c; nav('createQuestion', () => this.createCommentWrapper.openCreateDialog(this.user)); @@ -127,9 +140,6 @@ export class CommentListComponent implements OnInit, OnDestroy { }); dialogRef.componentInstance.roomId = this.room.id; }); - this.eventService.on<string>('setTagConfig').subscribe(tag => { - this.clickedOnKeyword(tag); - }); nav('tags', () => { const updRoom = JSON.parse(JSON.stringify(this.room)); const dialogRef = this.dialog.open(TagsComponent, { @@ -252,23 +262,6 @@ export class CommentListComponent implements OnInit, OnDestroy { this.translateService.get('comment-list.search').subscribe(msg => { this.searchPlaceholder = msg; }); - - CommentFilter.writeStdFilter(); - } - - private setCurrentFilter() { - const filter = new CommentFilter(); - filter.filterSelected = this.currentFilter; - filter.paused = this.freeze; - filter.periodSet = this.period; - filter.keywordSelected = this.selectedKeyword; - filter.tagSelected = this.selectedTag; - - if (filter.periodSet == Period.FROMNOW) { - filter.timeStampNow = new Date().getTime(); - } - - CommentFilter.currentFilter = filter; } ngOnDestroy() { @@ -279,6 +272,12 @@ export class CommentListComponent implements OnInit, OnDestroy { if (this.headerInterface) { this.headerInterface.unsubscribe(); } + if (this._subscriptionEventServiceRoomData) { + this._subscriptionEventServiceRoomData.unsubscribe(); + } + if (this._subscriptionEventServiceTagConfig) { + this._subscriptionEventServiceTagConfig.unsubscribe(); + } } checkScroll(): void { @@ -298,7 +297,7 @@ export class CommentListComponent implements OnInit, OnDestroy { this.hideCommentsList = true; this.filteredComments = this.comments .filter(c => this.checkIfIncludesKeyWord(c.body, this.searchInput) - || (!!c.answer ? this.checkIfIncludesKeyWord(c.answer, this.searchInput) : false)); + || (!!c.answer ? this.checkIfIncludesKeyWord(c.answer, this.searchInput) : false)); } } else if (this.searchInput.length === 0 && this.currentFilter === '') { this.hideCommentsList = false; @@ -432,6 +431,7 @@ export class CommentListComponent implements OnInit, OnDestroy { this.searchComments(); } } + closeDialog() { this.dialog.closeAll(); } @@ -577,7 +577,8 @@ export class CommentListComponent implements OnInit, OnDestroy { // current live announcer content must be cleared before next read this.liveAnnouncer.clear(); - this.liveAnnouncer.announce(newCommentText).catch(err => { /* TODO error handling */ }); + this.liveAnnouncer.announce(newCommentText).catch(err => { /* TODO error handling */ + }); }, 450); } @@ -589,32 +590,32 @@ export class CommentListComponent implements OnInit, OnDestroy { const currentTime = new Date(); const hourInSeconds = 3600000; let periodInSeconds; - if (this.period !== Period.ALL) { + if (this.period !== Period.all) { switch (this.period) { - case Period.FROMNOW: + case Period.fromNow: if (!this.fromNow) { this.fromNow = new Date().getTime(); } break; - case Period.ONEHOUR: + case Period.oneHour: periodInSeconds = hourInSeconds; break; - case Period.THREEHOURS: + case Period.threeHours: periodInSeconds = hourInSeconds * 2; break; - case Period.ONEDAY: + case Period.oneDay: periodInSeconds = hourInSeconds * 24; break; - case Period.ONEWEEK: + case Period.oneWeek: periodInSeconds = hourInSeconds * 168; break; - case Period.TWOWEEKS: + case Period.twoWeeks: periodInSeconds = hourInSeconds * 336; break; } this.commentsFilteredByTime = this.comments .filter(c => new Date(c.timestamp).getTime() >= - (this.period === Period.FROMNOW ? this.fromNow : (currentTime.getTime() - periodInSeconds))); + (this.period === Period.fromNow ? this.fromNow : (currentTime.getTime() - periodInSeconds))); } else { this.commentsFilteredByTime = this.comments; } @@ -622,4 +623,19 @@ export class CommentListComponent implements OnInit, OnDestroy { this.filterComments(this.currentFilter); this.titleService.attachTitle('(' + this.commentsFilteredByTime.length + ')'); } + + private getCurrentFilter(): CommentFilter { + const filter = new CommentFilter(); + filter.filterSelected = this.currentFilter; + filter.paused = this.freeze; + filter.periodSet = this.period; + filter.keywordSelected = this.selectedKeyword; + filter.tagSelected = this.selectedTag; + + if (filter.periodSet === Period.fromNow) { + filter.timeStampNow = new Date().getTime(); + } + + return filter; + } } diff --git a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts index d238dce2d3f2ef7eea696b4b36893c0875d2e4d3..d00502cedc212c2d425990c460504ebcc9517583 100644 --- a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts +++ b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts @@ -12,7 +12,6 @@ import { LanguageService } from '../../../../services/util/language.service'; import { TranslateService } from '@ngx-translate/core'; import { Rescale } from '../../../../models/rescale'; import { QuestionWallKeyEventSupport } from '../QuestionWallKeyEventSupport'; -import { CorrectWrong } from '../../../../models/correct-wrong.enum'; import { MatSliderChange } from '@angular/material/slider'; import { Period } from '../../../../utils/filter-options'; @@ -49,7 +48,7 @@ export class QuestionWallComponent implements OnInit, AfterViewInit, OnDestroy { tags; fontSize = 180; periodsList = Object.values(Period); - period: Period = Period.ALL; + period: Period = Period.all; public wrap<E>(e: E, action: (e: E) => void) { action(e); @@ -355,21 +354,21 @@ export class QuestionWallComponent implements OnInit, AfterViewInit, OnDestroy { const currentTime = new Date(); const hourInSeconds = 3600000; let periodInSeconds; - if (period !== Period.ALL) { + if (period !== Period.all) { switch (period) { - case Period.ONEHOUR: + case Period.oneHour: periodInSeconds = hourInSeconds; break; - case Period.THREEHOURS: + case Period.threeHours: periodInSeconds = hourInSeconds * 2; break; - case Period.ONEDAY: + case Period.oneDay: periodInSeconds = hourInSeconds * 24; break; - case Period.ONEWEEK: + case Period.oneWeek: periodInSeconds = hourInSeconds * 168; break; - case Period.TWOWEEKS: + case Period.twoWeeks: periodInSeconds = hourInSeconds * 336; break; } diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts index a5a1a0aeff10c0a40c0323305f19d6fc504c6689..a7bc393f0c6ded44e664bd607881d64d9dadc7cf 100644 --- a/src/app/services/http/comment.service.ts +++ b/src/app/services/http/comment.service.ts @@ -6,9 +6,10 @@ import { catchError, tap, map } from 'rxjs/operators'; import { BaseHttpService } from './base-http.service'; import { TSMap } from 'typescript-map'; import { Vote } from '../../models/vote'; -import { CommentFilterUtils } from '../../utils/filter-comments'; +import { CommentFilter } from '../../utils/filter-options'; const httpOptions = { + // eslint-disable-next-line @typescript-eslint/naming-convention headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @@ -100,10 +101,6 @@ export class CommentService extends BaseHttpService { ); } - getFilteredComments(roomId: string) : Observable<Comment[]> { - return this.getAckComments(roomId).pipe(map(commentList => commentList.filter(comment => CommentFilterUtils.checkComment(comment)))); - } - getAckComments(roomId: string): Observable<Comment[]> { const connectionUrl = this.apiUrl.base + this.apiUrl.comment + this.apiUrl.find; return this.http.post<Comment[]>(connectionUrl, { diff --git a/src/app/services/util/tag-cloud-data.service.ts b/src/app/services/util/tag-cloud-data.service.ts index 42530b1b95fa295f4dd9193bc852ebef3894f9cc..9647b619c832fbc30d0d87d441f65d38c6c2beca 100644 --- a/src/app/services/util/tag-cloud-data.service.ts +++ b/src/app/services/util/tag-cloud-data.service.ts @@ -8,7 +8,6 @@ import { CommentFilter } from '../../utils/filter-options'; import { TranslateService } from '@ngx-translate/core'; import { CloudParameters } from '../../components/shared/tag-cloud/tag-cloud.interface'; import { Comment } from '../../models/comment'; -import { CommentFilterUtils } from '../../utils/filter-comments'; import { Message } from '@stomp/stompjs'; export interface TagCloudDataTagEntry { @@ -82,6 +81,7 @@ export class TagCloudDataService { private _demoData: TagCloudData = null; private _adminData: TopicCloudAdminData = null; private _subscriptionAdminData: Subscription; + private _currentFilter: CommentFilter; constructor(private _wsCommentService: WsCommentServiceService, private _commentService: CommentService, @@ -106,14 +106,15 @@ export class TagCloudDataService { } bindToRoom(roomId: string): void { + this._currentFilter = CommentFilter.currentFilter; this._roomId = roomId; this.onReceiveAdminData(this._tagCloudAdmin.getDefaultAdminData); this._subscriptionAdminData = this._tagCloudAdmin.getAdminData.subscribe(adminData => { - this.onReceiveAdminData(adminData,true); + this.onReceiveAdminData(adminData, true); }); this.fetchData(); - if (!CommentFilter.currentFilter.paused) { + if (!this._currentFilter.paused) { this._wsCommentSubscription = this._wsCommentService .getCommentStream(this._roomId).subscribe(e => this.onMessage(e)); } @@ -248,7 +249,7 @@ export class TagCloudDataService { this._adminData = data; this._calcWeightType = this._adminData.considerVotes ? TagCloudCalcWeightType.byLengthAndVotes : TagCloudCalcWeightType.byLength; this._supplyType = this._adminData.keywordORfulltext as unknown as TagCloudDataSupplyType; - if(update) { + if (update) { this.rebuildTagData(); } } @@ -261,7 +262,7 @@ export class TagCloudDataService { } private fetchData(): void { - this._commentService.getFilteredComments(this._roomId).subscribe((comments: Comment[]) => { + this._commentService.getAckComments(this._roomId).subscribe((comments: Comment[]) => { this._lastFetchedComments = comments; if (this._isDemoActive) { this._lastMetaData.commentCount = comments.length; @@ -290,7 +291,8 @@ export class TagCloudDataService { const currentMeta = this._isDemoActive ? this._lastMetaData : this._currentMetaData; const data: TagCloudData = new Map<string, TagCloudDataTagEntry>(); const users = new Set<number>(); - for (const comment of this._lastFetchedComments) { + const filteredComments = this._lastFetchedComments.filter(comment => this._currentFilter.checkComment(comment)); + for (const comment of filteredComments) { let keywords = comment.keywordsFromQuestioner; if (this._supplyType === TagCloudDataSupplyType.keywordsAndFullText) { if (!keywords || !keywords.length) { @@ -376,10 +378,8 @@ export class TagCloudDataService { switch (msg.type) { case 'CommentCreated': this._commentService.getComment(payload.id).subscribe(c => { - if (CommentFilterUtils.checkComment(c)) { - this._lastFetchedComments.push(c); - this.rebuildTagData(); - } + this._lastFetchedComments.push(c); + this.rebuildTagData(); }); break; case 'CommentPatched': diff --git a/src/app/utils/filter-comments.ts b/src/app/utils/filter-comments.ts deleted file mode 100644 index 83943523692972143366b764831dec1df7f5a1e2..0000000000000000000000000000000000000000 --- a/src/app/utils/filter-comments.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { Comment } from '../models/comment'; -import { CorrectWrong } from '../models/correct-wrong.enum'; -import { CommentFilter, FilterNames, Period } from './filter-options'; - -export class CommentFilterUtils { - - private static checkPeriod(com : Comment, filter : CommentFilter) : boolean { - /* Filter by Period */ - const currentTime = new Date(); - const hourInSeconds = 3600000; - let periodInSeconds; - - if(filter.periodSet === Period.ALL) { - return true; - } - - switch (filter.periodSet) { - case Period.FROMNOW: - break; - case Period.ONEHOUR: - periodInSeconds = hourInSeconds; - break; - case Period.THREEHOURS: - periodInSeconds = hourInSeconds * 3; - break; - case Period.ONEDAY: - periodInSeconds = hourInSeconds * 24; - break; - case Period.ONEWEEK: - periodInSeconds = hourInSeconds * 168; - break; - case Period.TWOWEEKS: - periodInSeconds = hourInSeconds * 336; - break; - } - - const commentTime = new Date(com.timestamp).getTime(); - - if (filter.periodSet === Period.FROMNOW) { - return commentTime > filter.timeStampNow; - } - - if (filter.paused) { - return commentTime < filter.timeStampUntil; - } - - return commentTime > (currentTime.getTime() - periodInSeconds); - } - - private static checkFilters(com : Comment, filter : CommentFilter) : boolean { - if (filter.filterSelected) { // no filters => return true - switch (filter.filterSelected) { - case FilterNames.correct: - return com.correct === CorrectWrong.CORRECT ? true : false; - case FilterNames.wrong: - return com.correct === CorrectWrong.WRONG ? true : false; - case FilterNames.favorite: - return com.favorite; - case FilterNames.bookmark: - return com.bookmark; - case FilterNames.read: - return com.read; - case FilterNames.unread: - return !com.read; - case FilterNames.answer: - return com.answer != ""; - case FilterNames.unanswered: - return !com.answer; - } - } - - if (filter.keywordSelected != '') { - return com.keywordsFromQuestioner.includes(filter.keywordSelected); - } - if (filter.tagSelected != ''){ - return com.tag === filter.tagSelected; - } - - return true; - } - - public static checkComment(com : Comment, filter : CommentFilter = CommentFilter.currentFilter) : boolean { - return (this.checkPeriod(com, filter) && this.checkFilters(com, filter)); - } -} \ No newline at end of file diff --git a/src/app/utils/filter-options.ts b/src/app/utils/filter-options.ts index c0203f093a26d345fc8ed3e8dc334a86bb715652..7669a7d697a59d6d442d135815612e893562e45a 100644 --- a/src/app/utils/filter-options.ts +++ b/src/app/utils/filter-options.ts @@ -1,87 +1,166 @@ +import { Comment } from '../models/comment'; +import { CorrectWrong } from '../models/correct-wrong.enum'; + export const enum FilterNames { - read = 'read', - unread = 'unread', - favorite = 'favorite', - correct = 'correct', - wrong = 'wrong', - bookmark = 'bookmark', - answer = 'answer', - unanswered = 'unanswered' -}; + read = 'read', + unread = 'unread', + favorite = 'favorite', + correct = 'correct', + wrong = 'wrong', + bookmark = 'bookmark', + answer = 'answer', + unanswered = 'unanswered' +} export enum Period { - FROMNOW = 'from-now', - ONEHOUR = 'time-1h', - THREEHOURS = 'time-3h', - ONEDAY = 'time-1d', - ONEWEEK = 'time-1w', - TWOWEEKS = 'time-2w', - ALL = 'time-all' + fromNow = 'from-now', + oneHour = 'time-1h', + threeHours = 'time-3h', + oneDay = 'time-1d', + oneWeek = 'time-1w', + twoWeeks = 'time-2w', + all = 'time-all' } export class CommentFilter { - filterSelected : string = ''; - keywordSelected : string = ''; - tagSelected : string = ''; - - paused: boolean = false; - timeStampUntil : number = 0; - - periodSet : Period = Period.TWOWEEKS; - timeStampNow : number = 0; - - constructor(obj?: any) { - if (obj) { - this.filterSelected = obj.filterSelected; - this.keywordSelected = obj.keywordSelected; - this.tagSelected = obj.tagSelected; - this.paused = obj.paused; - this.timeStampUntil = obj.timeStampUntil; - this.periodSet = obj.periodSet; - this.timeStampNow = obj.timeStampNow; - } - } + filterSelected = ''; + keywordSelected = ''; + tagSelected = ''; - public static set currentFilter(filter : CommentFilter) { - localStorage.setItem("filter", JSON.stringify(filter)); + paused = false; + timeStampUntil = 0; + + periodSet: Period = Period.twoWeeks; + timeStampNow = 0; + + constructor(obj?: any) { + if (obj) { + this.filterSelected = obj.filterSelected; + this.keywordSelected = obj.keywordSelected; + this.tagSelected = obj.tagSelected; + this.paused = obj.paused; + this.timeStampUntil = obj.timeStampUntil; + this.periodSet = obj.periodSet; + this.timeStampNow = obj.timeStampNow; } + } + + public static set currentFilter(filter: CommentFilter) { + localStorage.setItem('filter', JSON.stringify(filter)); + } + + public static get currentFilter(): CommentFilter { + return new CommentFilter(JSON.parse(localStorage.getItem('filter'))); + } + + public static generateFilterNow(filterSelected: string): CommentFilter { + const filter = new CommentFilter(); + + filter.filterSelected = filterSelected; + filter.paused = false; + + filter.tagSelected = ''; + filter.keywordSelected = ''; + + filter.periodSet = Period.fromNow; + filter.timeStampNow = new Date().getTime(); + + return filter; + } - public static get currentFilter() : CommentFilter { - return new CommentFilter(JSON.parse(localStorage.getItem("filter"))); + public static generateFilterUntil(filterSelected: string, periodSelected: Period, untilTime: number, + tagSelected: string, keywordSelected: string): CommentFilter { + const filter = new CommentFilter(); + + filter.filterSelected = filterSelected; + + filter.paused = true; + filter.timeStampUntil = untilTime; + + filter.tagSelected = tagSelected; + filter.keywordSelected = keywordSelected; + + filter.periodSet = periodSelected; + + return filter; + } + + public checkComment(com: Comment): boolean { + return (this.checkPeriod(com) && this.checkFilters(com)); + } + + private checkPeriod(com: Comment): boolean { + /* Filter by Period */ + const currentTime = new Date(); + const hourInSeconds = 3600000; + let periodInSeconds; + + if (this.periodSet === Period.all) { + return true; } - public static writeStdFilter() { - this.currentFilter = new CommentFilter(); + switch (this.periodSet) { + case Period.fromNow: + break; + case Period.oneHour: + periodInSeconds = hourInSeconds; + break; + case Period.threeHours: + periodInSeconds = hourInSeconds * 3; + break; + case Period.oneDay: + periodInSeconds = hourInSeconds * 24; + break; + case Period.oneWeek: + periodInSeconds = hourInSeconds * 168; + break; + case Period.twoWeeks: + periodInSeconds = hourInSeconds * 336; + break; } - - public static generateFilterNow(filterSelected : string) : CommentFilter { - let filter = new CommentFilter(); - - filter.filterSelected = filterSelected; - filter.paused = false; - filter.tagSelected = ''; - filter.keywordSelected = ''; + const commentTime = new Date(com.timestamp).getTime(); - filter.periodSet = Period.FROMNOW; - filter.timeStampNow = new Date().getTime(); + if (this.periodSet === Period.fromNow) { + return commentTime > this.timeStampNow; + } - return filter; + if (this.paused) { + return commentTime < this.timeStampUntil; } - public static generateFilterUntil(filterSelected : string, periodSelected : Period, untilTime : number, tagSelected : string, keywordSelected : string) : CommentFilter { - let filter = new CommentFilter(); - - filter.filterSelected = filterSelected; + return commentTime > (currentTime.getTime() - periodInSeconds); + } - filter.paused = true; - filter.timeStampUntil = untilTime; + private checkFilters(com: Comment): boolean { + if (this.filterSelected) { // no filters => return true + switch (this.filterSelected) { + case FilterNames.correct: + return com.correct === CorrectWrong.CORRECT; + case FilterNames.wrong: + return com.correct === CorrectWrong.WRONG; + case FilterNames.favorite: + return com.favorite; + case FilterNames.bookmark: + return com.bookmark; + case FilterNames.read: + return com.read; + case FilterNames.unread: + return !com.read; + case FilterNames.answer: + return com.answer !== ''; + case FilterNames.unanswered: + return !com.answer; + } + } - filter.tagSelected = tagSelected; - filter.keywordSelected = keywordSelected; - - filter.periodSet = periodSelected; + if (this.keywordSelected !== '') { + return com.keywordsFromQuestioner.includes(this.keywordSelected); + } - return filter; + if (this.tagSelected !== '') { + return com.tag === this.tagSelected; } -} \ No newline at end of file + return true; + } +}