1 import { concat, Observable, Subject } from 'rxjs'
2 import { debounceTime, tap, toArray } from 'rxjs/operators'
3 import { Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { immutableAssign } from '@app/helpers'
8 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
10 import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
11 import { VideoSortField } from '@shared/models'
12 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
15 templateUrl: './my-videos.component.html',
16 styleUrls: [ './my-videos.component.scss' ]
18 export class MyVideosComponent implements OnInit, DisableForReuseHook {
19 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
20 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
21 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
24 selection: SelectionType = {}
25 pagination: ComponentPagination = {
30 miniatureDisplayOptions: MiniatureDisplayOptions = {
40 videoActions: DropdownAction<{ video: Video }>[] = []
44 videosSearchChanged = new Subject<string>()
45 getVideosObservableFunction = this.getVideosObservable.bind(this)
50 protected router: Router,
51 protected serverService: ServerService,
52 protected route: ActivatedRoute,
53 protected authService: AuthService,
54 protected notifier: Notifier,
55 protected screenService: ScreenService,
56 private confirmService: ConfirmService,
57 private videoService: VideoService
59 this.titlePage = $localize`My videos`
65 this.user = this.authService.getUser()
67 this.videosSearchChanged
68 .pipe(debounceTime(500))
70 this.videosSelection.reloadVideos()
75 this.videosSearch = ''
76 this.onVideosSearchChanged()
79 onVideosSearchChanged () {
80 this.videosSearchChanged.next()
84 this.videosSelection.disableForReuse()
88 this.videosSelection.enabledForReuse()
91 getVideosObservable (page: number, sort: VideoSortField) {
92 const newPagination = immutableAssign(this.pagination, { currentPage: page })
94 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
96 tap(res => this.pagination.totalItems = res.total)
100 async deleteSelectedVideos () {
101 const toDeleteVideosIds = Object.keys(this.selection)
102 .filter(k => this.selection[ k ] === true)
103 .map(k => parseInt(k, 10))
105 const res = await this.confirmService.confirm(
106 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
109 if (res === false) return
111 const observables: Observable<any>[] = []
112 for (const videoId of toDeleteVideosIds) {
113 const o = this.videoService.removeVideo(videoId)
114 .pipe(tap(() => this.removeVideoFromArray(videoId)))
119 concat(...observables)
123 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
127 err => this.notifier.error(err.message)
131 async deleteVideo (video: Video) {
132 const res = await this.confirmService.confirm(
133 $localize`Do you really want to delete ${video.name}?`,
136 if (res === false) return
138 this.videoService.removeVideo(video.id)
141 this.notifier.success($localize`Video ${video.name} deleted.`)
142 this.removeVideoFromArray(video.id)
145 error => this.notifier.error(error.message)
149 changeOwnership (video: Video) {
150 this.videoChangeOwnershipModal.show(video)
153 displayLiveInformation (video: Video) {
154 this.liveStreamInformationModal.show(video)
157 private removeVideoFromArray (id: number) {
158 this.videos = this.videos.filter(v => v.id !== id)
161 private buildActions () {
162 this.videoActions = [
164 label: $localize`Display live information`,
165 handler: ({ video }) => this.displayLiveInformation(video),
166 isDisplayed: ({ video }) => video.isLive,
170 label: $localize`Change ownership`,
171 handler: ({ video }) => this.changeOwnership(video),
172 iconName: 'ownership-change'
175 label: $localize`Delete`,
176 handler: ({ video }) => this.deleteVideo(video),