1 import { concat, Observable, Subject } from 'rxjs'
2 import { debounceTime, 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 } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { immutableAssign } from '@app/helpers'
8 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
9 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
10 import { MiniatureDisplayOptions, OwnerDisplayType, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
11 import { VideoSortField } from '@shared/models'
12 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
15 templateUrl: './my-videos.component.html',
16 styleUrls: [ './my-videos.component.scss' ]
18 export class MyVideosComponent implements OnInit, DisableForReuseHook {
19 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
20 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
21 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
24 selection: SelectionType = {}
25 pagination: ComponentPagination = {
30 miniatureDisplayOptions: MiniatureDisplayOptions = {
39 ownerDisplayType: OwnerDisplayType = 'videoChannel'
41 videoActions: DropdownAction<{ video: Video }>[] = []
45 videosSearchChanged = new Subject<string>()
46 getVideosObservableFunction = this.getVideosObservable.bind(this)
49 protected router: Router,
50 protected serverService: ServerService,
51 protected route: ActivatedRoute,
52 protected authService: AuthService,
53 protected notifier: Notifier,
54 protected screenService: ScreenService,
55 private confirmService: ConfirmService,
56 private videoService: VideoService
58 this.titlePage = $localize`My videos`
64 this.videosSearchChanged
65 .pipe(debounceTime(500))
67 this.videosSelection.reloadVideos()
72 this.videosSearch = ''
73 this.onVideosSearchChanged()
76 onVideosSearchChanged () {
77 this.videosSearchChanged.next()
81 this.videosSelection.disableForReuse()
85 this.videosSelection.enabledForReuse()
88 getVideosObservable (page: number, sort: VideoSortField) {
89 const newPagination = immutableAssign(this.pagination, { currentPage: page })
91 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
93 tap(res => this.pagination.totalItems = res.total)
97 async deleteSelectedVideos () {
98 const toDeleteVideosIds = Object.keys(this.selection)
99 .filter(k => this.selection[ k ] === true)
100 .map(k => parseInt(k, 10))
102 const res = await this.confirmService.confirm(
103 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
106 if (res === false) return
108 const observables: Observable<any>[] = []
109 for (const videoId of toDeleteVideosIds) {
110 const o = this.videoService.removeVideo(videoId)
111 .pipe(tap(() => this.removeVideoFromArray(videoId)))
116 concat(...observables)
120 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
124 err => this.notifier.error(err.message)
128 async deleteVideo (video: Video) {
129 const res = await this.confirmService.confirm(
130 $localize`Do you really want to delete ${video.name}?`,
133 if (res === false) return
135 this.videoService.removeVideo(video.id)
138 this.notifier.success($localize`Video ${video.name} deleted.`)
139 this.removeVideoFromArray(video.id)
142 error => this.notifier.error(error.message)
146 changeOwnership (video: Video) {
147 this.videoChangeOwnershipModal.show(video)
150 displayLiveInformation (video: Video) {
151 this.liveStreamInformationModal.show(video)
154 private removeVideoFromArray (id: number) {
155 this.videos = this.videos.filter(v => v.id !== id)
158 private buildActions () {
159 this.videoActions = [
161 label: $localize`Display live information`,
162 handler: ({ video }) => this.displayLiveInformation(video),
163 isDisplayed: ({ video }) => video.isLive,