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'
12 MiniatureDisplayOptions,
14 VideoActionsDisplayType,
15 VideosSelectionComponent
16 } from '@app/shared/shared-video-miniature'
17 import { VideoChannel, VideoSortField } from '@shared/models'
18 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
21 templateUrl: './my-videos.component.html',
22 styleUrls: [ './my-videos.component.scss' ]
24 export class MyVideosComponent implements OnInit, DisableForReuseHook {
25 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
26 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
27 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
30 selection: SelectionType = {}
31 pagination: ComponentPagination = {
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
45 videoDropdownDisplayOptions: VideoActionsDisplayType = {
61 moreVideoActions: DropdownAction<{ video: Video }>[][] = []
64 getVideosObservableFunction = this.getVideosObservable.bind(this)
66 sort: VideoSortField = '-publishedAt'
70 inputFilters: AdvancedInputFilter[] = []
74 private search: string
75 private userChannels: VideoChannel[] = []
78 protected router: Router,
79 protected serverService: ServerService,
80 protected route: ActivatedRoute,
81 protected authService: AuthService,
82 protected notifier: Notifier,
83 protected screenService: ScreenService,
84 private confirmService: ConfirmService,
85 private videoService: VideoService
87 this.titlePage = $localize`My videos`
93 this.user = this.authService.getUser()
95 if (this.route.snapshot.queryParams['search']) {
96 this.search = this.route.snapshot.queryParams['search']
99 this.authService.userInformationLoaded.subscribe(() => {
100 this.user = this.authService.getUser()
101 this.userChannels = this.user.videoChannels
103 const channelFilters = this.userChannels.map(c => {
105 value: 'channel:' + c.name,
110 this.inputFilters = [
112 title: $localize`Advanced filters`,
115 value: 'isLive:true',
116 label: $localize`Only live videos`
122 title: $localize`Channel filters`,
123 children: channelFilters
129 onSearch (search: string) {
135 this.videosSelection.reloadVideos()
138 onChangeSortColumn () {
139 this.videosSelection.reloadVideos()
147 this.disabled = false
150 getVideosObservable (page: number) {
151 const newPagination = immutableAssign(this.pagination, { currentPage: page })
153 return this.videoService.getMyVideos({
154 videoPagination: newPagination,
156 userChannels: this.userChannels,
160 tap(res => this.pagination.totalItems = res.total)
164 async deleteSelectedVideos () {
165 const toDeleteVideosIds = Object.keys(this.selection)
166 .filter(k => this.selection[k] === true)
167 .map(k => parseInt(k, 10))
169 const res = await this.confirmService.confirm(
170 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
173 if (res === false) return
175 const observables: Observable<any>[] = []
176 for (const videoId of toDeleteVideosIds) {
177 const o = this.videoService.removeVideo(videoId)
178 .pipe(tap(() => this.removeVideoFromArray(videoId)))
183 concat(...observables)
187 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
191 error: err => this.notifier.error(err.message)
195 onVideoRemoved (video: Video) {
196 this.removeVideoFromArray(video.id)
199 changeOwnership (video: Video) {
200 this.videoChangeOwnershipModal.show(video)
203 private removeVideoFromArray (id: number) {
204 this.videos = this.videos.filter(v => v.id !== id)
207 private buildActions () {
208 this.moreVideoActions = [
211 label: $localize`Change ownership`,
212 handler: ({ video }) => this.changeOwnership(video),
213 iconName: 'ownership-change'