1 import { concat, Observable } from 'rxjs'
2 import { 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 { 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 implements OnInit, 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)
46 sort: VideoSortField = '-publishedAt'
50 inputFilters: AdvancedInputFilter[] = [
52 queryParams: { search: 'isLive:true' },
53 label: $localize`Only live videos`
59 private search: string
62 protected router: Router,
63 protected serverService: ServerService,
64 protected route: ActivatedRoute,
65 protected authService: AuthService,
66 protected notifier: Notifier,
67 protected screenService: ScreenService,
68 private confirmService: ConfirmService,
69 private videoService: VideoService
71 this.titlePage = $localize`My videos`
77 this.user = this.authService.getUser()
79 if (this.route.snapshot.queryParams['search']) {
80 this.search = this.route.snapshot.queryParams['search']
84 onSearch (search: string) {
90 this.videosSelection.reloadVideos()
93 onChangeSortColumn () {
94 this.videosSelection.reloadVideos()
102 this.disabled = false
105 getVideosObservable (page: number) {
106 const newPagination = immutableAssign(this.pagination, { currentPage: page })
108 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
110 tap(res => this.pagination.totalItems = res.total)
114 async deleteSelectedVideos () {
115 const toDeleteVideosIds = Object.keys(this.selection)
116 .filter(k => this.selection[k] === true)
117 .map(k => parseInt(k, 10))
119 const res = await this.confirmService.confirm(
120 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
123 if (res === false) return
125 const observables: Observable<any>[] = []
126 for (const videoId of toDeleteVideosIds) {
127 const o = this.videoService.removeVideo(videoId)
128 .pipe(tap(() => this.removeVideoFromArray(videoId)))
133 concat(...observables)
137 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
141 error: err => this.notifier.error(err.message)
145 async deleteVideo (video: Video) {
146 const res = await this.confirmService.confirm(
147 $localize`Do you really want to delete ${video.name}?`,
150 if (res === false) return
152 this.videoService.removeVideo(video.id)
155 this.notifier.success($localize`Video ${video.name} deleted.`)
156 this.removeVideoFromArray(video.id)
159 error: err => this.notifier.error(err.message)
163 changeOwnership (video: Video) {
164 this.videoChangeOwnershipModal.show(video)
167 displayLiveInformation (video: Video) {
168 this.liveStreamInformationModal.show(video)
171 private removeVideoFromArray (id: number) {
172 this.videos = this.videos.filter(v => v.id !== id)
175 private buildActions () {
176 this.videoActions = [
178 label: $localize`Display live information`,
179 handler: ({ video }) => this.displayLiveInformation(video),
180 isDisplayed: ({ video }) => video.isLive,
184 label: $localize`Change ownership`,
185 handler: ({ video }) => this.changeOwnership(video),
186 iconName: 'ownership-change'
189 label: $localize`Delete`,
190 handler: ({ video }) => this.deleteVideo(video),