]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
f9c1b32b036b53e9c90ddb3d5225c201033e3649
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-videos / my-videos.component.ts
1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ComponentPagination, ConfirmService, Notifier, RouteFilter, ScreenService, ServerService, User } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { 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'
11 import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
12 import { VideoSortField } from '@shared/models'
13 import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
14
15 @Component({
16 templateUrl: './my-videos.component.html',
17 styleUrls: [ './my-videos.component.scss' ]
18 })
19 export class MyVideosComponent extends RouteFilter implements OnInit, AfterViewInit, DisableForReuseHook {
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
22 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
23
24 titlePage: string
25 selection: SelectionType = {}
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10,
29 totalItems: null
30 }
31 miniatureDisplayOptions: MiniatureDisplayOptions = {
32 date: true,
33 views: true,
34 by: true,
35 privacyLabel: false,
36 privacyText: true,
37 state: true,
38 blacklistInfo: true
39 }
40
41 videoActions: DropdownAction<{ video: Video }>[] = []
42
43 videos: Video[] = []
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
45 sort: VideoSortField = '-publishedAt'
46
47 user: User
48
49 inputFilters: AdvancedInputFilter[] = [
50 {
51 queryParams: { 'search': 'isLive:true' },
52 label: $localize`Only live videos`
53 }
54 ]
55
56 constructor (
57 protected router: Router,
58 protected serverService: ServerService,
59 protected route: ActivatedRoute,
60 protected authService: AuthService,
61 protected notifier: Notifier,
62 protected screenService: ScreenService,
63 private confirmService: ConfirmService,
64 private videoService: VideoService
65 ) {
66 super()
67
68 this.titlePage = $localize`My videos`
69 }
70
71 ngOnInit () {
72 this.buildActions()
73
74 this.user = this.authService.getUser()
75
76 this.initSearch()
77 this.listenToSearchChange()
78 }
79
80 ngAfterViewInit () {
81 if (this.search) this.setTableFilter(this.search, false)
82 }
83
84 loadData () {
85 this.videosSelection.reloadVideos()
86 }
87
88 onChangeSortColumn () {
89 this.videosSelection.reloadVideos()
90 }
91
92 disableForReuse () {
93 this.videosSelection.disableForReuse()
94 }
95
96 enabledForReuse () {
97 this.videosSelection.enabledForReuse()
98 }
99
100 getVideosObservable (page: number) {
101 const newPagination = immutableAssign(this.pagination, { currentPage: page })
102
103 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
104 .pipe(
105 tap(res => this.pagination.totalItems = res.total)
106 )
107 }
108
109 async deleteSelectedVideos () {
110 const toDeleteVideosIds = Object.keys(this.selection)
111 .filter(k => this.selection[ k ] === true)
112 .map(k => parseInt(k, 10))
113
114 const res = await this.confirmService.confirm(
115 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
116 $localize`Delete`
117 )
118 if (res === false) return
119
120 const observables: Observable<any>[] = []
121 for (const videoId of toDeleteVideosIds) {
122 const o = this.videoService.removeVideo(videoId)
123 .pipe(tap(() => this.removeVideoFromArray(videoId)))
124
125 observables.push(o)
126 }
127
128 concat(...observables)
129 .pipe(toArray())
130 .subscribe(
131 () => {
132 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
133 this.selection = {}
134 },
135
136 err => this.notifier.error(err.message)
137 )
138 }
139
140 async deleteVideo (video: Video) {
141 const res = await this.confirmService.confirm(
142 $localize`Do you really want to delete ${video.name}?`,
143 $localize`Delete`
144 )
145 if (res === false) return
146
147 this.videoService.removeVideo(video.id)
148 .subscribe(
149 () => {
150 this.notifier.success($localize`Video ${video.name} deleted.`)
151 this.removeVideoFromArray(video.id)
152 },
153
154 error => this.notifier.error(error.message)
155 )
156 }
157
158 changeOwnership (video: Video) {
159 this.videoChangeOwnershipModal.show(video)
160 }
161
162 displayLiveInformation (video: Video) {
163 this.liveStreamInformationModal.show(video)
164 }
165
166 private removeVideoFromArray (id: number) {
167 this.videos = this.videos.filter(v => v.id !== id)
168 }
169
170 private buildActions () {
171 this.videoActions = [
172 {
173 label: $localize`Display live information`,
174 handler: ({ video }) => this.displayLiveInformation(video),
175 isDisplayed: ({ video }) => video.isLive,
176 iconName: 'live'
177 },
178 {
179 label: $localize`Change ownership`,
180 handler: ({ video }) => this.changeOwnership(video),
181 iconName: 'ownership-change'
182 },
183 {
184 label: $localize`Delete`,
185 handler: ({ video }) => this.deleteVideo(video),
186 iconName: 'delete'
187 }
188 ]
189 }
190 }