From b7819090de8ced71e5f9c5773c575ab627a148e4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 3 Jan 2020 15:01:17 +0100 Subject: Move watch later logic in miniature --- .../app/shared/video/video-miniature.component.ts | 98 ++++++++++++++++++++-- 1 file changed, 90 insertions(+), 8 deletions(-) (limited to 'client/src/app/shared/video/video-miniature.component.ts') diff --git a/client/src/app/shared/video/video-miniature.component.ts b/client/src/app/shared/video/video-miniature.component.ts index ba65d33b6..a603f87e5 100644 --- a/client/src/app/shared/video/video-miniature.component.ts +++ b/client/src/app/shared/video/video-miniature.component.ts @@ -1,12 +1,24 @@ -import { ChangeDetectionStrategy, Component, EventEmitter, Inject, Input, LOCALE_ID, OnInit, Output, ViewChild } from '@angular/core' +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + EventEmitter, + Inject, + Input, + LOCALE_ID, + OnInit, + Output +} from '@angular/core' import { User } from '../users' import { Video } from './video.model' -import { ServerService } from '@app/core' -import { ServerConfig, VideoPrivacy, VideoState } from '../../../../../shared' +import { AuthService, ServerService } from '@app/core' +import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared' import { I18n } from '@ngx-translate/i18n-polyfill' import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component' import { ScreenService } from '@app/shared/misc/screen.service' -import { VideoThumbnailComponent } from './video-thumbnail.component' +import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' +import { forkJoin } from 'rxjs' +import { first } from 'rxjs/operators' export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto' export type MiniatureDisplayOptions = { @@ -47,8 +59,6 @@ export class VideoMiniatureComponent implements OnInit { @Output() videoUnblacklisted = new EventEmitter() @Output() videoRemoved = new EventEmitter() - @ViewChild('thumbnail', { static: true }) thumbnail: VideoThumbnailComponent - videoActionsDisplayOptions: VideoActionsDisplayType = { playlist: true, download: false, @@ -60,14 +70,28 @@ export class VideoMiniatureComponent implements OnInit { showActions = false serverConfig: ServerConfig + addToWatchLaterText: string + addedToWatchLaterText: string + inWatchLaterPlaylist: boolean + + watchLaterPlaylist: { + id: number + playlistElementId?: number + } + private ownerDisplayTypeChosen: 'account' | 'videoChannel' constructor ( private screenService: ScreenService, private serverService: ServerService, private i18n: I18n, + private authService: AuthService, + private videoPlaylistService: VideoPlaylistService, + private cd: ChangeDetectorRef, @Inject(LOCALE_ID) private localeId: string - ) { } + ) { + + } get isVideoBlur () { return this.video.isVideoNSFWForUser(this.user, this.serverConfig) @@ -131,7 +155,8 @@ export class VideoMiniatureComponent implements OnInit { loadActions () { if (this.displayVideoActions) this.showActions = true - this.thumbnail.load() + + this.loadWatchLater() } onVideoBlacklisted () { @@ -146,6 +171,38 @@ export class VideoMiniatureComponent implements OnInit { this.videoRemoved.emit() } + isUserLoggedIn () { + return this.authService.isLoggedIn() + } + + onWatchLaterClick (currentState: boolean) { + if (currentState === true) this.removeFromWatchLater() + else this.addToWatchLater() + + this.inWatchLaterPlaylist = !currentState + } + + addToWatchLater () { + const body = { videoId: this.video.id } + + this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe( + res => { + this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id + } + ) + } + + removeFromWatchLater () { + this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId) + .subscribe( + _ => { /* empty */ } + ) + } + + isWatchLaterPlaylistDisplayed () { + return this.inWatchLaterPlaylist !== undefined + } + private setUpBy () { if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') { this.ownerDisplayTypeChosen = this.ownerDisplayType @@ -163,4 +220,29 @@ export class VideoMiniatureComponent implements OnInit { this.ownerDisplayTypeChosen = 'videoChannel' } } + + private loadWatchLater () { + if (!this.isUserLoggedIn()) return + + forkJoin([ + this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id), + this.authService.userInformationLoaded.pipe(first()) + ]).subscribe( + ([ existResult ]) => { + const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER) + const existsInWatchLater = existResult[ this.video.id ].find(r => r.playlistId === watchLaterPlaylist.id) + this.inWatchLaterPlaylist = false + + this.watchLaterPlaylist = { + id: watchLaterPlaylist.id + } + + if (existsInWatchLater) { + this.inWatchLaterPlaylist = true + this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId + } + + this.cd.markForCheck() + }) + } } -- cgit v1.2.3