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, User, UserService } 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)
51 protected router: Router,
52 protected serverService: ServerService,
53 protected route: ActivatedRoute,
54 protected authService: AuthService,
55 protected notifier: Notifier,
56 protected screenService: ScreenService,
57 private confirmService: ConfirmService,
58 private videoService: VideoService
60 this.titlePage = $localize`My videos`
66 this.user = this.authService.getUser()
68 this.videosSearchChanged
69 .pipe(debounceTime(500))
71 this.videosSelection.reloadVideos()
76 this.videosSearch = ''
77 this.onVideosSearchChanged()
80 onVideosSearchChanged () {
81 this.videosSearchChanged.next()
85 this.videosSelection.disableForReuse()
89 this.videosSelection.enabledForReuse()
92 getVideosObservable (page: number, sort: VideoSortField) {
93 const newPagination = immutableAssign(this.pagination, { currentPage: page })
95 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
97 tap(res => this.pagination.totalItems = res.total)
101 async deleteSelectedVideos () {
102 const toDeleteVideosIds = Object.keys(this.selection)
103 .filter(k => this.selection[ k ] === true)
104 .map(k => parseInt(k, 10))
106 const res = await this.confirmService.confirm(
107 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
110 if (res === false) return
112 const observables: Observable<any>[] = []
113 for (const videoId of toDeleteVideosIds) {
114 const o = this.videoService.removeVideo(videoId)
115 .pipe(tap(() => this.removeVideoFromArray(videoId)))
120 concat(...observables)
124 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
128 err => this.notifier.error(err.message)
132 async deleteVideo (video: Video) {
133 const res = await this.confirmService.confirm(
134 $localize`Do you really want to delete ${video.name}?`,
137 if (res === false) return
139 this.videoService.removeVideo(video.id)
142 this.notifier.success($localize`Video ${video.name} deleted.`)
143 this.removeVideoFromArray(video.id)
146 error => this.notifier.error(error.message)
150 changeOwnership (video: Video) {
151 this.videoChangeOwnershipModal.show(video)
154 displayLiveInformation (video: Video) {
155 this.liveStreamInformationModal.show(video)
158 private removeVideoFromArray (id: number) {
159 this.videos = this.videos.filter(v => v.id !== id)
162 private buildActions () {
163 this.videoActions = [
165 label: $localize`Display live information`,
166 handler: ({ video }) => this.displayLiveInformation(video),
167 isDisplayed: ({ video }) => video.isLive,
171 label: $localize`Change ownership`,
172 handler: ({ video }) => this.changeOwnership(video),
173 iconName: 'ownership-change'
176 label: $localize`Delete`,
177 handler: ({ video }) => this.deleteVideo(video),