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 { MiniatureDisplayOptions, OwnerDisplayType, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
10 import { VideoSortField } from '@shared/models'
11 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
12 import { LiveStreamInformationComponent } from './modals/live-stream-information.component'
15 selector: 'my-account-videos',
16 templateUrl: './my-account-videos.component.html',
17 styleUrls: [ './my-account-videos.component.scss' ]
19 export class MyAccountVideosComponent 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 = {
40 ownerDisplayType: OwnerDisplayType = 'videoChannel'
42 videoActions: DropdownAction<{ video: Video }>[] = []
46 videosSearchChanged = new Subject<string>()
47 getVideosObservableFunction = this.getVideosObservable.bind(this)
50 protected router: Router,
51 protected serverService: ServerService,
52 protected route: ActivatedRoute,
53 protected authService: AuthService,
54 protected notifier: Notifier,
55 protected screenService: ScreenService,
56 private confirmService: ConfirmService,
57 private videoService: VideoService
59 this.titlePage = $localize`My videos`
65 this.videosSearchChanged
66 .pipe(debounceTime(500))
68 this.videosSelection.reloadVideos()
73 this.videosSearch = ''
74 this.onVideosSearchChanged()
77 onVideosSearchChanged () {
78 this.videosSearchChanged.next()
82 this.videosSelection.disableForReuse()
86 this.videosSelection.enabledForReuse()
89 getVideosObservable (page: number, sort: VideoSortField) {
90 const newPagination = immutableAssign(this.pagination, { currentPage: page })
92 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
94 tap(res => this.pagination.totalItems = res.total)
98 async deleteSelectedVideos () {
99 const toDeleteVideosIds = Object.keys(this.selection)
100 .filter(k => this.selection[ k ] === true)
101 .map(k => parseInt(k, 10))
103 const res = await this.confirmService.confirm(
104 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
107 if (res === false) return
109 const observables: Observable<any>[] = []
110 for (const videoId of toDeleteVideosIds) {
111 const o = this.videoService.removeVideo(videoId)
112 .pipe(tap(() => this.removeVideoFromArray(videoId)))
117 concat(...observables)
121 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
125 err => this.notifier.error(err.message)
129 async deleteVideo (video: Video) {
130 const res = await this.confirmService.confirm(
131 $localize`Do you really want to delete ${video.name}?`,
134 if (res === false) return
136 this.videoService.removeVideo(video.id)
139 this.notifier.success($localize`Video ${video.name} deleted.`)
140 this.removeVideoFromArray(video.id)
143 error => this.notifier.error(error.message)
147 changeOwnership (video: Video) {
148 this.videoChangeOwnershipModal.show(video)
151 displayLiveInformation (video: Video) {
152 this.liveStreamInformationModal.show(video)
155 private removeVideoFromArray (id: number) {
156 this.videos = this.videos.filter(v => v.id !== id)
159 private buildActions () {
160 this.videoActions = [
162 label: $localize`Display live information`,
163 handler: ({ video }) => this.displayLiveInformation(video),
164 isDisplayed: ({ video }) => video.isLive,
168 label: $localize`Change ownership`,
169 handler: ({ video }) => this.changeOwnership(video),
170 iconName: 'ownership-change'
173 label: $localize`Delete`,
174 handler: ({ video }) => this.deleteVideo(video),