diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-03-08 21:35:43 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-03-08 21:35:43 +0100 |
commit | d38b82810638b9f664c9016fac2684454c273a77 (patch) | |
tree | 9465c367e5033675309efca4d66790c6fdd5230d /client/src/app/videos/video-watch/video-watch.component.ts | |
parent | 8f9064432122cba0f518a24ac4378357dadec589 (diff) | |
download | PeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.gz PeerTube-d38b82810638b9f664c9016fac2684454c273a77.tar.zst PeerTube-d38b82810638b9f664c9016fac2684454c273a77.zip |
Add like/dislike system for videos
Diffstat (limited to 'client/src/app/videos/video-watch/video-watch.component.ts')
-rw-r--r-- | client/src/app/videos/video-watch/video-watch.component.ts | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/client/src/app/videos/video-watch/video-watch.component.ts b/client/src/app/videos/video-watch/video-watch.component.ts index d1abc81bc..ed6b30102 100644 --- a/client/src/app/videos/video-watch/video-watch.component.ts +++ b/client/src/app/videos/video-watch/video-watch.component.ts | |||
@@ -10,7 +10,7 @@ import { AuthService } from '../../core'; | |||
10 | import { VideoMagnetComponent } from './video-magnet.component'; | 10 | import { VideoMagnetComponent } from './video-magnet.component'; |
11 | import { VideoShareComponent } from './video-share.component'; | 11 | import { VideoShareComponent } from './video-share.component'; |
12 | import { VideoReportComponent } from './video-report.component'; | 12 | import { VideoReportComponent } from './video-report.component'; |
13 | import { Video, VideoService } from '../shared'; | 13 | import { RateType, Video, VideoService } from '../shared'; |
14 | import { WebTorrentService } from './webtorrent.service'; | 14 | import { WebTorrentService } from './webtorrent.service'; |
15 | 15 | ||
16 | @Component({ | 16 | @Component({ |
@@ -33,6 +33,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
33 | player: VideoJSPlayer; | 33 | player: VideoJSPlayer; |
34 | playerElement: Element; | 34 | playerElement: Element; |
35 | uploadSpeed: number; | 35 | uploadSpeed: number; |
36 | userRating: RateType = null; | ||
36 | video: Video = null; | 37 | video: Video = null; |
37 | videoNotFound = false; | 38 | videoNotFound = false; |
38 | 39 | ||
@@ -61,6 +62,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
61 | this.video = video; | 62 | this.video = video; |
62 | this.setOpenGraphTags(); | 63 | this.setOpenGraphTags(); |
63 | this.loadVideo(); | 64 | this.loadVideo(); |
65 | this.checkUserRating(); | ||
64 | }, | 66 | }, |
65 | error => { | 67 | error => { |
66 | this.videoNotFound = true; | 68 | this.videoNotFound = true; |
@@ -136,6 +138,40 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
136 | }); | 138 | }); |
137 | } | 139 | } |
138 | 140 | ||
141 | setLike() { | ||
142 | if (this.isUserLoggedIn() === false) return; | ||
143 | // Already liked this video | ||
144 | if (this.userRating === 'like') return; | ||
145 | |||
146 | this.videoService.setVideoLike(this.video.id) | ||
147 | .subscribe( | ||
148 | () => { | ||
149 | // Update the video like attribute | ||
150 | this.updateVideoRating(this.userRating, 'like'); | ||
151 | this.userRating = 'like'; | ||
152 | }, | ||
153 | |||
154 | err => this.notificationsService.error('Error', err.text) | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | setDislike() { | ||
159 | if (this.isUserLoggedIn() === false) return; | ||
160 | // Already disliked this video | ||
161 | if (this.userRating === 'dislike') return; | ||
162 | |||
163 | this.videoService.setVideoDislike(this.video.id) | ||
164 | .subscribe( | ||
165 | () => { | ||
166 | // Update the video dislike attribute | ||
167 | this.updateVideoRating(this.userRating, 'dislike'); | ||
168 | this.userRating = 'dislike'; | ||
169 | }, | ||
170 | |||
171 | err => this.notificationsService.error('Error', err.text) | ||
172 | ); | ||
173 | } | ||
174 | |||
139 | showReportModal(event: Event) { | 175 | showReportModal(event: Event) { |
140 | event.preventDefault(); | 176 | event.preventDefault(); |
141 | this.videoReportModal.show(); | 177 | this.videoReportModal.show(); |
@@ -154,6 +190,38 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
154 | return this.authService.isLoggedIn(); | 190 | return this.authService.isLoggedIn(); |
155 | } | 191 | } |
156 | 192 | ||
193 | private checkUserRating() { | ||
194 | // Unlogged users do not have ratings | ||
195 | if (this.isUserLoggedIn() === false) return; | ||
196 | |||
197 | this.videoService.getUserVideoRating(this.video.id) | ||
198 | .subscribe( | ||
199 | ratingObject => { | ||
200 | if (ratingObject) { | ||
201 | this.userRating = ratingObject.rating; | ||
202 | } | ||
203 | }, | ||
204 | |||
205 | err => this.notificationsService.error('Error', err.text) | ||
206 | ); | ||
207 | } | ||
208 | |||
209 | private updateVideoRating(oldRating: RateType, newRating: RateType) { | ||
210 | let likesToIncrement = 0; | ||
211 | let dislikesToIncrement = 0; | ||
212 | |||
213 | if (oldRating) { | ||
214 | if (oldRating === 'like') likesToIncrement--; | ||
215 | if (oldRating === 'dislike') dislikesToIncrement--; | ||
216 | } | ||
217 | |||
218 | if (newRating === 'like') likesToIncrement++; | ||
219 | if (newRating === 'dislike') dislikesToIncrement++; | ||
220 | |||
221 | this.video.likes += likesToIncrement; | ||
222 | this.video.dislikes += dislikesToIncrement; | ||
223 | } | ||
224 | |||
157 | private loadTooLong() { | 225 | private loadTooLong() { |
158 | this.error = true; | 226 | this.error = true; |
159 | console.error('The video load seems to be abnormally long.'); | 227 | console.error('The video load seems to be abnormally long.'); |