A few updates for the watch video view (#181)

* Fixes #156: Filter out the video being watched from the list of other videos of the same author;

* Fixes #167: in the video view, hide the author's domain when it's from the current host;

* Fixes #171: Allow undoing a like/dislike;
This commit is contained in:
Benjamin Bouvier 2018-01-07 14:48:10 +01:00 committed by Chocobozzz
parent cbca00dfc1
commit 57a49263e4
7 changed files with 55 additions and 34 deletions

View File

@ -35,7 +35,10 @@ export class Video implements VideoServerModel {
nsfw: boolean nsfw: boolean
account: Account account: Account
private static createByString (account: string, serverHost: string) { private static createByString (account: string, serverHost: string, apiURL: string) {
const thisHost = new URL(apiURL).host
if (serverHost.trim() === thisHost)
return account
return account + '@' + serverHost return account + '@' + serverHost
} }
@ -78,7 +81,7 @@ export class Video implements VideoServerModel {
this.dislikes = hash.dislikes this.dislikes = hash.dislikes
this.nsfw = hash.nsfw this.nsfw = hash.nsfw
this.by = Video.createByString(hash.accountName, hash.serverHost) this.by = Video.createByString(hash.accountName, hash.serverHost, absoluteAPIUrl)
} }
isVideoNSFWForUser (user: User) { isVideoNSFWForUser (user: User) {

View File

@ -136,6 +136,10 @@ export class VideoService {
return this.setVideoRate(id, 'dislike') return this.setVideoRate(id, 'dislike')
} }
unsetVideoLike (id: number) {
return this.setVideoRate(id, 'none')
}
getUserVideoRating (id: number): Observable<UserVideoRate> { getUserVideoRating (id: number): Observable<UserVideoRate> {
const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating' const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'

View File

@ -165,7 +165,7 @@
Other videos Other videos
</div> </div>
<div *ngFor="let video of otherVideos"> <div *ngFor="let video of otherVideosDisplayed">
<my-video-miniature [video]="video" [user]="user"></my-video-miniature> <my-video-miniature [video]="video" [user]="user"></my-video-miniature>
</div> </div>
</div> </div>

View File

@ -29,6 +29,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
@ViewChild('videoReportModal') videoReportModal: VideoReportComponent @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
otherVideos: Video[] = [] otherVideos: Video[] = []
otherVideosDisplayed: Video[] = []
error = false error = false
loading = false loading = false
@ -69,7 +70,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt') this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
.subscribe( .subscribe(
data => this.otherVideos = data.videos, data => this.otherVideos = data.videos,
err => console.error(err) err => console.error(err)
) )
@ -102,36 +102,22 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
setLike () { setLike () {
if (this.isUserLoggedIn() === false) return if (this.isUserLoggedIn() === false) return
if (this.userRating === 'like') {
// Already liked this video // Already liked this video
if (this.userRating === 'like') return this.setRating('none')
} else {
this.videoService.setVideoLike(this.video.id) this.setRating('like')
.subscribe( }
() => {
// Update the video like attribute
this.updateVideoRating(this.userRating, 'like')
this.userRating = 'like'
},
err => this.notificationsService.error('Error', err.message)
)
} }
setDislike () { setDislike () {
if (this.isUserLoggedIn() === false) return if (this.isUserLoggedIn() === false) return
if (this.userRating === 'dislike') {
// Already disliked this video // Already disliked this video
if (this.userRating === 'dislike') return this.setRating('none')
} else {
this.videoService.setVideoDislike(this.video.id) this.setRating('dislike')
.subscribe( }
() => {
// Update the video dislike attribute
this.updateVideoRating(this.userRating, 'dislike')
this.userRating = 'dislike'
},
err => this.notificationsService.error('Error', err.message)
)
} }
blacklistVideo (event: Event) { blacklistVideo (event: Event) {
@ -303,6 +289,10 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
private onVideoFetched (video: VideoDetails) { private onVideoFetched (video: VideoDetails) {
this.video = video this.video = video
if (this.otherVideos.length > 0) {
this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
}
let observable let observable
if (this.video.isVideoNSFWForUser(this.user)) { if (this.video.isVideoNSFWForUser(this.user)) {
observable = this.confirmService.confirm( observable = this.confirmService.confirm(
@ -366,6 +356,31 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
) )
} }
private setRating (nextRating) {
let method
switch (nextRating) {
case 'like':
method = this.videoService.setVideoLike
break
case 'dislike':
method = this.videoService.setVideoDislike
break
case 'none':
method = this.videoService.unsetVideoLike
break
}
method.call(this.videoService, this.video.id)
.subscribe(
() => {
// Update the video like attribute
this.updateVideoRating(this.userRating, nextRating)
this.userRating = nextRating
},
err => this.notificationsService.error('Error', err.message)
)
}
private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) { private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
let likesToIncrement = 0 let likesToIncrement = 0
let dislikesToIncrement = 0 let dislikesToIncrement = 0

View File

@ -62,7 +62,6 @@ async function rateVideo (req: express.Request, res: express.Response) {
await previousRate.destroy({ transaction: t }) await previousRate.destroy({ transaction: t })
} else { // Update previous rate } else { // Update previous rate
previousRate.type = rateType previousRate.type = rateType
await previousRate.save({ transaction: t }) await previousRate.save({ transaction: t })
} }
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate

View File

@ -65,7 +65,7 @@ function isVideoViewsValid (value: string) {
} }
function isVideoRatingTypeValid (value: string) { function isVideoRatingTypeValid (value: string) {
return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
} }
function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {

View File

@ -1 +1 @@
export type VideoRateType = 'like' | 'dislike' export type VideoRateType = 'like' | 'dislike' | 'none'