1 import { uniqBy } from 'lodash-es'
2 import { concat, Observable } from 'rxjs'
3 import { tap, toArray } from 'rxjs/operators'
4 import { Component, OnInit, ViewChild } from '@angular/core'
5 import { ActivatedRoute, Router } from '@angular/router'
6 import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
7 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
8 import { immutableAssign, prepareIcu } from '@app/helpers'
9 import { AdvancedInputFilter } from '@app/shared/shared-forms'
10 import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
11 import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
13 MiniatureDisplayOptions,
15 VideoActionsDisplayType,
16 VideosSelectionComponent
17 } from '@app/shared/shared-video-miniature'
18 import { VideoPlaylistService } from '@app/shared/shared-video-playlist'
19 import { VideoChannel, VideoExistInPlaylist, VideosExistInPlaylists, VideoSortField } from '@shared/models'
20 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
23 templateUrl: './my-videos.component.html',
24 styleUrls: [ './my-videos.component.scss' ]
26 export class MyVideosComponent implements OnInit, DisableForReuseHook {
27 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
28 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
29 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
31 videosContainedInPlaylists: VideosExistInPlaylists = {}
33 selection: SelectionType = {}
34 pagination: ComponentPagination = {
39 miniatureDisplayOptions: MiniatureDisplayOptions = {
47 forceChannelInBy: true
49 videoDropdownDisplayOptions: VideoActionsDisplayType = {
65 moreVideoActions: DropdownAction<{ video: Video }>[][] = []
68 getVideosObservableFunction = this.getVideosObservable.bind(this)
70 sort: VideoSortField = '-publishedAt'
74 inputFilters: AdvancedInputFilter[] = []
78 private search: string
79 private userChannels: VideoChannel[] = []
82 protected router: Router,
83 protected serverService: ServerService,
84 protected route: ActivatedRoute,
85 protected authService: AuthService,
86 protected notifier: Notifier,
87 protected screenService: ScreenService,
88 private confirmService: ConfirmService,
89 private videoService: VideoService,
90 private playlistService: VideoPlaylistService
92 this.titlePage = $localize`My videos`
98 this.user = this.authService.getUser()
100 if (this.route.snapshot.queryParams['search']) {
101 this.search = this.route.snapshot.queryParams['search']
104 this.authService.userInformationLoaded.subscribe(() => {
105 this.user = this.authService.getUser()
106 this.userChannels = this.user.videoChannels
108 const channelFilters = [ ...this.userChannels ]
109 .sort((a, b) => a.displayName.localeCompare(b.displayName))
112 value: 'channel:' + c.name,
117 this.inputFilters = [
119 title: $localize`Advanced filters`,
122 value: 'isLive:true',
123 label: $localize`Only live videos`
129 title: $localize`Channel filters`,
130 children: channelFilters
136 onSearch (search: string) {
142 this.videosSelection.reloadVideos()
145 onChangeSortColumn () {
146 this.videosSelection.reloadVideos()
154 this.disabled = false
157 getVideosObservable (page: number) {
158 const newPagination = immutableAssign(this.pagination, { currentPage: page })
160 return this.videoService.getMyVideos({
161 videoPagination: newPagination,
163 userChannels: this.userChannels,
166 tap(res => this.pagination.totalItems = res.total),
167 tap(({ data }) => this.fetchVideosContainedInPlaylists(data))
171 private fetchVideosContainedInPlaylists (videos: Video[]) {
172 this.playlistService.doVideosExistInPlaylist(videos.map(v => v.id))
173 .subscribe(result => {
174 this.videosContainedInPlaylists = Object.keys(result).reduce((acc, videoId) => ({
176 [videoId]: uniqBy(result[+videoId], (p: VideoExistInPlaylist) => p.playlistId)
177 }), this.videosContainedInPlaylists)
181 async deleteSelectedVideos () {
182 const toDeleteVideosIds = Object.entries(this.selection)
183 .filter(([ _k, v ]) => v === true)
184 .map(([ k, _v ]) => parseInt(k, 10))
186 const res = await this.confirmService.confirm(
187 prepareIcu($localize`Do you really want to delete {length, plural, =1 {this video} other {{length} videos}}?`)(
188 { length: toDeleteVideosIds.length },
189 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`
193 if (res === false) return
195 const observables: Observable<any>[] = []
196 for (const videoId of toDeleteVideosIds) {
197 const o = this.videoService.removeVideo(videoId)
198 .pipe(tap(() => this.removeVideoFromArray(videoId)))
203 concat(...observables)
207 this.notifier.success(
208 prepareIcu($localize`{length, plural, =1 {Video has been deleted} other {{length} videos have been deleted}}`)(
209 { length: toDeleteVideosIds.length },
210 $localize`${toDeleteVideosIds.length} have been deleted.`
217 error: err => this.notifier.error(err.message)
221 onVideoRemoved (video: Video) {
222 this.removeVideoFromArray(video.id)
225 changeOwnership (video: Video) {
226 this.videoChangeOwnershipModal.show(video)
229 private removeVideoFromArray (id: number) {
230 this.videos = this.videos.filter(v => v.id !== id)
233 private buildActions () {
234 this.moreVideoActions = [
237 label: $localize`Change ownership`,
238 handler: ({ video }) => this.changeOwnership(video),
239 iconName: 'ownership-change'