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 { prepareIcu, 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 = {
44 forceChannelInBy: true
46 videoDropdownDisplayOptions: VideoActionsDisplayType = {
62 moreVideoActions: DropdownAction<{ video: Video }>[][] = []
65 getVideosObservableFunction = this.getVideosObservable.bind(this)
67 sort: VideoSortField = '-publishedAt'
71 inputFilters: AdvancedInputFilter[] = []
75 private search: string
76 private userChannels: VideoChannel[] = []
79 protected router: Router,
80 protected serverService: ServerService,
81 protected route: ActivatedRoute,
82 protected authService: AuthService,
83 protected notifier: Notifier,
84 protected screenService: ScreenService,
85 private confirmService: ConfirmService,
86 private videoService: VideoService
88 this.titlePage = $localize`My videos`
94 this.user = this.authService.getUser()
96 if (this.route.snapshot.queryParams['search']) {
97 this.search = this.route.snapshot.queryParams['search']
100 this.authService.userInformationLoaded.subscribe(() => {
101 this.user = this.authService.getUser()
102 this.userChannels = this.user.videoChannels
104 const channelFilters = this.userChannels.map(c => {
106 value: 'channel:' + c.name,
111 this.inputFilters = [
113 title: $localize`Advanced filters`,
116 value: 'isLive:true',
117 label: $localize`Only live videos`
123 title: $localize`Channel filters`,
124 children: channelFilters
130 onSearch (search: string) {
136 this.videosSelection.reloadVideos()
139 onChangeSortColumn () {
140 this.videosSelection.reloadVideos()
148 this.disabled = false
151 getVideosObservable (page: number) {
152 const newPagination = immutableAssign(this.pagination, { currentPage: page })
154 return this.videoService.getMyVideos({
155 videoPagination: newPagination,
157 userChannels: this.userChannels,
161 tap(res => this.pagination.totalItems = res.total)
165 async deleteSelectedVideos () {
166 const toDeleteVideosIds = Object.keys(this.selection)
167 .filter(k => this.selection[k] === true)
168 .map(k => parseInt(k, 10))
170 const res = await this.confirmService.confirm(
171 prepareIcu($localize`Do you really want to delete {length, plural, =1 {this video} other {{length} videos}}?`)(
172 { length: toDeleteVideosIds.length },
173 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`
177 if (res === false) return
179 const observables: Observable<any>[] = []
180 for (const videoId of toDeleteVideosIds) {
181 const o = this.videoService.removeVideo(videoId)
182 .pipe(tap(() => this.removeVideoFromArray(videoId)))
187 concat(...observables)
191 this.notifier.success(
192 prepareIcu($localize`{length, plural, =1 {Video has been deleted} other {{length} videos have been deleted}}`)(
193 { length: toDeleteVideosIds.length },
194 $localize`${toDeleteVideosIds.length} have been deleted.`
201 error: err => this.notifier.error(err.message)
205 onVideoRemoved (video: Video) {
206 this.removeVideoFromArray(video.id)
209 changeOwnership (video: Video) {
210 this.videoChangeOwnershipModal.show(video)
213 private removeVideoFromArray (id: number) {
214 this.videos = this.videos.filter(v => v.id !== id)
217 private buildActions () {
218 this.moreVideoActions = [
221 label: $localize`Change ownership`,
222 handler: ({ video }) => this.changeOwnership(video),
223 iconName: 'ownership-change'