+ setDislike() {
+ if (this.isUserLoggedIn() === false) return;
+ // Already disliked this video
+ if (this.userRating === 'dislike') return;
+
+ this.videoService.setVideoDislike(this.video.id)
+ .subscribe(
+ () => {
+ // Update the video dislike attribute
+ this.updateVideoRating(this.userRating, 'dislike');
+ this.userRating = 'dislike';
+ },
+
+ err => this.notificationsService.error('Error', err.text)
+ );
+ }
+
+ showReportModal(event: Event) {
+ event.preventDefault();
+ this.videoReportModal.show();
+ }
+
+ showShareModal() {
+ this.videoShareModal.show();
+ }
+
+ showMagnetUriModal(event: Event) {
+ event.preventDefault();
+ this.videoMagnetModal.show();
+ }
+
+ isUserLoggedIn() {
+ return this.authService.isLoggedIn();
+ }
+
+ private checkUserRating() {
+ // Unlogged users do not have ratings
+ if (this.isUserLoggedIn() === false) return;
+
+ this.videoService.getUserVideoRating(this.video.id)
+ .subscribe(
+ ratingObject => {
+ if (ratingObject) {
+ this.userRating = ratingObject.rating;
+ }
+ },
+
+ err => this.notificationsService.error('Error', err.text)
+ );
+ }
+
+ private onVideoFetched(video: Video) {
+ this.video = video;
+
+ let observable;
+ if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
+ observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW');
+ } else {
+ observable = Observable.of(true);
+ }
+
+ observable.subscribe(
+ res => {
+ if (res === false) {
+ return this.router.navigate([ '/videos/list' ]);
+ }
+
+ this.setOpenGraphTags();
+ this.loadVideo();
+ this.checkUserRating();
+ }
+ );
+ }
+
+ private updateVideoRating(oldRating: RateType, newRating: RateType) {
+ let likesToIncrement = 0;
+ let dislikesToIncrement = 0;
+
+ if (oldRating) {
+ if (oldRating === 'like') likesToIncrement--;
+ if (oldRating === 'dislike') dislikesToIncrement--;
+ }
+
+ if (newRating === 'like') likesToIncrement++;
+ if (newRating === 'dislike') dislikesToIncrement++;
+
+ this.video.likes += likesToIncrement;
+ this.video.dislikes += dislikesToIncrement;