1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ComponentPagination, ConfirmService, Notifier, RouteFilter, 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 { AdvancedInputFilter } from '@app/shared/shared-forms'
9 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
10 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
11 import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
12 import { VideoSortField } from '@shared/models'
13 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
16 templateUrl: './my-videos.component.html',
17 styleUrls: [ './my-videos.component.scss' ]
19 export class MyVideosComponent extends RouteFilter implements OnInit, AfterViewInit, DisableForReuseHook {
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
22 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
25 selection: SelectionType = {}
26 pagination: ComponentPagination = {
31 miniatureDisplayOptions: MiniatureDisplayOptions = {
41 videoActions: DropdownAction<{ video: Video }>[] = []
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
45 sort: VideoSortField = '-publishedAt'
49 inputFilters: AdvancedInputFilter[] = [
51 queryParams: { 'search': 'isLive:true' },
52 label: $localize`Only live videos`
57 protected router: Router,
58 protected serverService: ServerService,
59 protected route: ActivatedRoute,
60 protected authService: AuthService,
61 protected notifier: Notifier,
62 protected screenService: ScreenService,
63 private confirmService: ConfirmService,
64 private videoService: VideoService
68 this.titlePage = $localize`My videos`
74 this.user = this.authService.getUser()
77 this.listenToSearchChange()
81 if (this.search) this.setTableFilter(this.search, false)
85 this.videosSelection.reloadVideos()
88 onChangeSortColumn () {
89 this.videosSelection.reloadVideos()
93 this.videosSelection.disableForReuse()
97 this.videosSelection.enabledForReuse()
100 getVideosObservable (page: number) {
101 const newPagination = immutableAssign(this.pagination, { currentPage: page })
103 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
105 tap(res => this.pagination.totalItems = res.total)
109 async deleteSelectedVideos () {
110 const toDeleteVideosIds = Object.keys(this.selection)
111 .filter(k => this.selection[ k ] === true)
112 .map(k => parseInt(k, 10))
114 const res = await this.confirmService.confirm(
115 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
118 if (res === false) return
120 const observables: Observable<any>[] = []
121 for (const videoId of toDeleteVideosIds) {
122 const o = this.videoService.removeVideo(videoId)
123 .pipe(tap(() => this.removeVideoFromArray(videoId)))
128 concat(...observables)
132 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
136 err => this.notifier.error(err.message)
140 async deleteVideo (video: Video) {
141 const res = await this.confirmService.confirm(
142 $localize`Do you really want to delete ${video.name}?`,
145 if (res === false) return
147 this.videoService.removeVideo(video.id)
150 this.notifier.success($localize`Video ${video.name} deleted.`)
151 this.removeVideoFromArray(video.id)
154 error => this.notifier.error(error.message)
158 changeOwnership (video: Video) {
159 this.videoChangeOwnershipModal.show(video)
162 displayLiveInformation (video: Video) {
163 this.liveStreamInformationModal.show(video)
166 private removeVideoFromArray (id: number) {
167 this.videos = this.videos.filter(v => v.id !== id)
170 private buildActions () {
171 this.videoActions = [
173 label: $localize`Display live information`,
174 handler: ({ video }) => this.displayLiveInformation(video),
175 isDisplayed: ({ video }) => video.isLive,
179 label: $localize`Change ownership`,
180 handler: ({ video }) => this.changeOwnership(video),
181 iconName: 'ownership-change'
184 label: $localize`Delete`,
185 handler: ({ video }) => this.deleteVideo(video),