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 { VideoChannel, 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[]
54 private search: string
55 private userChannels: VideoChannel[] = []
58 protected router: Router,
59 protected serverService: ServerService,
60 protected route: ActivatedRoute,
61 protected authService: AuthService,
62 protected notifier: Notifier,
63 protected screenService: ScreenService,
64 private confirmService: ConfirmService,
65 private videoService: VideoService
67 this.titlePage = $localize`My videos`
73 this.user = this.authService.getUser()
75 if (this.route.snapshot.queryParams['search']) {
76 this.search = this.route.snapshot.queryParams['search']
79 this.authService.userInformationLoaded.subscribe(() => {
80 this.user = this.authService.getUser()
81 this.userChannels = this.user.videoChannels
83 const channelFilters = this.userChannels.map(c => {
85 value: 'channel:' + c.name,
92 title: $localize`Advanced filters`,
96 label: $localize`Only live videos`
102 title: $localize`Channel filters`,
103 children: channelFilters
109 onSearch (search: string) {
115 this.videosSelection.reloadVideos()
118 onChangeSortColumn () {
119 this.videosSelection.reloadVideos()
127 this.disabled = false
130 getVideosObservable (page: number) {
131 const newPagination = immutableAssign(this.pagination, { currentPage: page })
133 return this.videoService.getMyVideos({
134 videoPagination: newPagination,
136 userChannels: this.userChannels,
140 tap(res => this.pagination.totalItems = res.total)
144 async deleteSelectedVideos () {
145 const toDeleteVideosIds = Object.keys(this.selection)
146 .filter(k => this.selection[k] === true)
147 .map(k => parseInt(k, 10))
149 const res = await this.confirmService.confirm(
150 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
153 if (res === false) return
155 const observables: Observable<any>[] = []
156 for (const videoId of toDeleteVideosIds) {
157 const o = this.videoService.removeVideo(videoId)
158 .pipe(tap(() => this.removeVideoFromArray(videoId)))
163 concat(...observables)
167 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
171 error: err => this.notifier.error(err.message)
175 async deleteVideo (video: Video) {
176 const res = await this.confirmService.confirm(
177 $localize`Do you really want to delete ${video.name}?`,
180 if (res === false) return
182 this.videoService.removeVideo(video.id)
185 this.notifier.success($localize`Video ${video.name} deleted.`)
186 this.removeVideoFromArray(video.id)
189 error: err => this.notifier.error(err.message)
193 changeOwnership (video: Video) {
194 this.videoChangeOwnershipModal.show(video)
197 displayLiveInformation (video: Video) {
198 this.liveStreamInformationModal.show(video)
201 private removeVideoFromArray (id: number) {
202 this.videos = this.videos.filter(v => v.id !== id)
205 private buildActions () {
206 this.videoActions = [
208 label: $localize`Editor`,
209 linkBuilder: ({ video }) => [ '/video-editor/edit', video.uuid ],
210 isDisplayed: ({ video }) => video.isEditableBy(this.authService.getUser(), this.serverService.getHTMLConfig().videoEditor.enabled),
214 label: $localize`Display live information`,
215 handler: ({ video }) => this.displayLiveInformation(video),
216 isDisplayed: ({ video }) => video.isLive,
220 label: $localize`Change ownership`,
221 handler: ({ video }) => this.changeOwnership(video),
222 iconName: 'ownership-change'
225 label: $localize`Delete`,
226 handler: ({ video }) => this.deleteVideo(video),