]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
261e87f996f8881e090f5564057ef1d634baa460
[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 { Component, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, ComponentPagination, ConfirmService, Notifier, 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 { VideoChannel, 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 implements OnInit, 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
46 sort: VideoSortField = '-publishedAt'
47
48 user: User
49
50 inputFilters: AdvancedInputFilter[]
51
52 disabled = false
53
54 private search: string
55 private userChannels: VideoChannel[] = []
56
57 constructor (
58 protected router: Router,
59 protected serverService: ServerService,
60 protected route: ActivatedRoute,
61 protected authService: AuthService,
62 protected notifier: Notifier,
63 protected screenService: ScreenService,
64 private confirmService: ConfirmService,
65 private videoService: VideoService
66 ) {
67 this.titlePage = $localize`My videos`
68 }
69
70 ngOnInit () {
71 this.buildActions()
72
73 this.user = this.authService.getUser()
74
75 if (this.route.snapshot.queryParams['search']) {
76 this.search = this.route.snapshot.queryParams['search']
77 }
78
79 this.authService.userInformationLoaded.subscribe(() => {
80 this.user = this.authService.getUser()
81 this.userChannels = this.user.videoChannels
82
83 const channelFilters = this.userChannels.map(c => {
84 return {
85 value: 'channel:' + c.name,
86 label: c.name
87 }
88 })
89
90 this.inputFilters = [
91 {
92 title: $localize`Advanced filters`,
93 children: [
94 {
95 value: 'isLive:true',
96 label: $localize`Only live videos`
97 }
98 ]
99 },
100
101 {
102 title: $localize`Channel filters`,
103 children: channelFilters
104 }
105 ]
106 })
107 }
108
109 onSearch (search: string) {
110 this.search = search
111 this.reloadData()
112 }
113
114 reloadData () {
115 this.videosSelection.reloadVideos()
116 }
117
118 onChangeSortColumn () {
119 this.videosSelection.reloadVideos()
120 }
121
122 disableForReuse () {
123 this.disabled = true
124 }
125
126 enabledForReuse () {
127 this.disabled = false
128 }
129
130 getVideosObservable (page: number) {
131 const newPagination = immutableAssign(this.pagination, { currentPage: page })
132
133 return this.videoService.getMyVideos({
134 videoPagination: newPagination,
135 sort: this.sort,
136 userChannels: this.userChannels,
137 search: this.search
138 })
139 .pipe(
140 tap(res => this.pagination.totalItems = res.total)
141 )
142 }
143
144 async deleteSelectedVideos () {
145 const toDeleteVideosIds = Object.keys(this.selection)
146 .filter(k => this.selection[k] === true)
147 .map(k => parseInt(k, 10))
148
149 const res = await this.confirmService.confirm(
150 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
151 $localize`Delete`
152 )
153 if (res === false) return
154
155 const observables: Observable<any>[] = []
156 for (const videoId of toDeleteVideosIds) {
157 const o = this.videoService.removeVideo(videoId)
158 .pipe(tap(() => this.removeVideoFromArray(videoId)))
159
160 observables.push(o)
161 }
162
163 concat(...observables)
164 .pipe(toArray())
165 .subscribe({
166 next: () => {
167 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
168 this.selection = {}
169 },
170
171 error: err => this.notifier.error(err.message)
172 })
173 }
174
175 async deleteVideo (video: Video) {
176 const res = await this.confirmService.confirm(
177 $localize`Do you really want to delete ${video.name}?`,
178 $localize`Delete`
179 )
180 if (res === false) return
181
182 this.videoService.removeVideo(video.id)
183 .subscribe({
184 next: () => {
185 this.notifier.success($localize`Video ${video.name} deleted.`)
186 this.removeVideoFromArray(video.id)
187 },
188
189 error: err => this.notifier.error(err.message)
190 })
191 }
192
193 changeOwnership (video: Video) {
194 this.videoChangeOwnershipModal.show(video)
195 }
196
197 displayLiveInformation (video: Video) {
198 this.liveStreamInformationModal.show(video)
199 }
200
201 private removeVideoFromArray (id: number) {
202 this.videos = this.videos.filter(v => v.id !== id)
203 }
204
205 private buildActions () {
206 this.videoActions = [
207 {
208 label: $localize`Display live information`,
209 handler: ({ video }) => this.displayLiveInformation(video),
210 isDisplayed: ({ video }) => video.isLive,
211 iconName: 'live'
212 },
213 {
214 label: $localize`Change ownership`,
215 handler: ({ video }) => this.changeOwnership(video),
216 iconName: 'ownership-change'
217 },
218 {
219 label: $localize`Delete`,
220 handler: ({ video }) => this.deleteVideo(video),
221 iconName: 'delete'
222 }
223 ]
224 }
225 }