]>
Commit | Line | Data |
---|---|---|
8fac9adc | 1 | import { uniqBy } from 'lodash-es' |
1fd61899 C |
2 | import { concat, Observable } from 'rxjs' |
3 | import { tap, toArray } from 'rxjs/operators' | |
2e46eb97 | 4 | import { Component, OnInit, ViewChild } from '@angular/core' |
be447678 | 5 | import { ActivatedRoute, Router } from '@angular/router' |
2e46eb97 | 6 | import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core' |
67ed6552 | 7 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' |
38a3ccc7 | 8 | import { immutableAssign, prepareIcu } from '@app/helpers' |
1fd61899 | 9 | import { AdvancedInputFilter } from '@app/shared/shared-forms' |
d846d99c | 10 | import { DropdownAction, Video, VideoService } from '@app/shared/shared-main' |
f8c00564 | 11 | import { LiveStreamInformationComponent } from '@app/shared/shared-video-live' |
384ba8b7 C |
12 | import { |
13 | MiniatureDisplayOptions, | |
14 | SelectionType, | |
15 | VideoActionsDisplayType, | |
16 | VideosSelectionComponent | |
17 | } from '@app/shared/shared-video-miniature' | |
38a3ccc7 | 18 | import { VideoPlaylistService } from '@app/shared/shared-video-playlist' |
19 | import { VideoChannel, VideoExistInPlaylist, VideosExistInPlaylists, VideoSortField } from '@shared/models' | |
d846d99c | 20 | import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component' |
202f6b6c C |
21 | |
22 | @Component({ | |
17119e4a C |
23 | templateUrl: './my-videos.component.html', |
24 | styleUrls: [ './my-videos.component.scss' ] | |
202f6b6c | 25 | }) |
2e46eb97 | 26 | export class MyVideosComponent implements OnInit, DisableForReuseHook { |
f36da21e C |
27 | @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent |
28 | @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent | |
d846d99c | 29 | @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent |
489290b8 | 30 | |
38a3ccc7 | 31 | videosContainedInPlaylists: VideosExistInPlaylists = {} |
b1d40cff | 32 | titlePage: string |
693263e9 | 33 | selection: SelectionType = {} |
0cd4344f | 34 | pagination: ComponentPagination = { |
234b535d | 35 | currentPage: 1, |
1d904806 | 36 | itemsPerPage: 10, |
234b535d C |
37 | totalItems: null |
38 | } | |
e2409062 C |
39 | miniatureDisplayOptions: MiniatureDisplayOptions = { |
40 | date: true, | |
41 | views: true, | |
c4a6f790 | 42 | by: true, |
e2409062 C |
43 | privacyLabel: false, |
44 | privacyText: true, | |
45 | state: true, | |
3fca30a7 C |
46 | blacklistInfo: true, |
47 | forceChannelInBy: true | |
e2409062 | 48 | } |
384ba8b7 C |
49 | videoDropdownDisplayOptions: VideoActionsDisplayType = { |
50 | playlist: false, | |
51 | download: false, | |
52 | update: false, | |
53 | blacklist: false, | |
54 | delete: true, | |
55 | report: false, | |
56 | duplicate: false, | |
57 | mute: false, | |
26490335 | 58 | liveInfo: true, |
384ba8b7 C |
59 | removeFiles: false, |
60 | transcoding: false, | |
61 | studio: true, | |
62 | stats: true | |
63 | } | |
c4a6f790 | 64 | |
384ba8b7 | 65 | moreVideoActions: DropdownAction<{ video: Video }>[][] = [] |
d846d99c | 66 | |
693263e9 C |
67 | videos: Video[] = [] |
68 | getVideosObservableFunction = this.getVideosObservable.bind(this) | |
2e46eb97 | 69 | |
8e286cdc | 70 | sort: VideoSortField = '-publishedAt' |
202f6b6c | 71 | |
241609f1 C |
72 | user: User |
73 | ||
60ab5b99 | 74 | inputFilters: AdvancedInputFilter[] = [] |
1fd61899 | 75 | |
dd24f1bb C |
76 | disabled = false |
77 | ||
2e46eb97 | 78 | private search: string |
978c87e7 | 79 | private userChannels: VideoChannel[] = [] |
2e46eb97 | 80 | |
b1d40cff C |
81 | constructor ( |
82 | protected router: Router, | |
489290b8 | 83 | protected serverService: ServerService, |
b1d40cff C |
84 | protected route: ActivatedRoute, |
85 | protected authService: AuthService, | |
f8b2c1b4 | 86 | protected notifier: Notifier, |
bbe0f064 | 87 | protected screenService: ScreenService, |
308c4275 | 88 | private confirmService: ConfirmService, |
38a3ccc7 | 89 | private videoService: VideoService, |
90 | private playlistService: VideoPlaylistService | |
b1d40cff | 91 | ) { |
66357162 | 92 | this.titlePage = $localize`My videos` |
202f6b6c C |
93 | } |
94 | ||
bf64ed41 | 95 | ngOnInit () { |
d846d99c C |
96 | this.buildActions() |
97 | ||
241609f1 | 98 | this.user = this.authService.getUser() |
ca44cb36 C |
99 | |
100 | if (this.route.snapshot.queryParams['search']) { | |
101 | this.search = this.route.snapshot.queryParams['search'] | |
102 | } | |
978c87e7 C |
103 | |
104 | this.authService.userInformationLoaded.subscribe(() => { | |
105 | this.user = this.authService.getUser() | |
106 | this.userChannels = this.user.videoChannels | |
107 | ||
7d758898 C |
108 | const channelFilters = [ ...this.userChannels ] |
109 | .sort((a, b) => a.displayName.localeCompare(b.displayName)) | |
110 | .map(c => { | |
111 | return { | |
112 | value: 'channel:' + c.name, | |
113 | label: c.displayName | |
114 | } | |
115 | }) | |
978c87e7 C |
116 | |
117 | this.inputFilters = [ | |
118 | { | |
119 | title: $localize`Advanced filters`, | |
120 | children: [ | |
121 | { | |
dd6d2a7c | 122 | value: 'isLive:true', |
978c87e7 C |
123 | label: $localize`Only live videos` |
124 | } | |
125 | ] | |
126 | }, | |
127 | ||
128 | { | |
129 | title: $localize`Channel filters`, | |
130 | children: channelFilters | |
131 | } | |
132 | ] | |
133 | }) | |
bf64ed41 RK |
134 | } |
135 | ||
2e46eb97 C |
136 | onSearch (search: string) { |
137 | this.search = search | |
138 | this.reloadData() | |
4f5d0459 RK |
139 | } |
140 | ||
2e46eb97 | 141 | reloadData () { |
1fd61899 | 142 | this.videosSelection.reloadVideos() |
bf64ed41 RK |
143 | } |
144 | ||
8e286cdc RK |
145 | onChangeSortColumn () { |
146 | this.videosSelection.reloadVideos() | |
147 | } | |
148 | ||
693263e9 | 149 | disableForReuse () { |
dd24f1bb | 150 | this.disabled = true |
9af61e84 C |
151 | } |
152 | ||
693263e9 | 153 | enabledForReuse () { |
dd24f1bb | 154 | this.disabled = false |
ce0e281d C |
155 | } |
156 | ||
0df302ca | 157 | getVideosObservable (page: number) { |
0cd4344f C |
158 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) |
159 | ||
978c87e7 C |
160 | return this.videoService.getMyVideos({ |
161 | videoPagination: newPagination, | |
162 | sort: this.sort, | |
163 | userChannels: this.userChannels, | |
164 | search: this.search | |
38a3ccc7 | 165 | }).pipe( |
166 | tap(res => this.pagination.totalItems = res.total), | |
167 | tap(({ data }) => this.fetchVideosContainedInPlaylists(data)) | |
168 | ) | |
169 | } | |
170 | ||
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) => ({ | |
175 | ...acc, | |
54909304 | 176 | [videoId]: uniqBy(result[+videoId], (p: VideoExistInPlaylist) => p.playlistId) |
38a3ccc7 | 177 | }), this.videosContainedInPlaylists) |
178 | }) | |
244e76a5 RK |
179 | } |
180 | ||
1f30a185 | 181 | async deleteSelectedVideos () { |
54909304 C |
182 | const toDeleteVideosIds = Object.entries(this.selection) |
183 | .filter(([ _k, v ]) => v === true) | |
184 | .map(([ k, _v ]) => parseInt(k, 10)) | |
ce0e281d | 185 | |
2186386c | 186 | const res = await this.confirmService.confirm( |
eaa52952 C |
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?` | |
190 | ), | |
66357162 | 191 | $localize`Delete` |
2186386c | 192 | ) |
1f30a185 C |
193 | if (res === false) return |
194 | ||
195 | const observables: Observable<any>[] = [] | |
196 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 197 | const o = this.videoService.removeVideo(videoId) |
489290b8 | 198 | .pipe(tap(() => this.removeVideoFromArray(videoId))) |
1f30a185 C |
199 | |
200 | observables.push(o) | |
201 | } | |
202 | ||
489290b8 C |
203 | concat(...observables) |
204 | .pipe(toArray()) | |
1378c0d3 C |
205 | .subscribe({ |
206 | next: () => { | |
eaa52952 C |
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.` | |
211 | ) | |
212 | ) | |
213 | ||
693263e9 | 214 | this.selection = {} |
1f30a185 C |
215 | }, |
216 | ||
1378c0d3 C |
217 | error: err => this.notifier.error(err.message) |
218 | }) | |
ce0e281d C |
219 | } |
220 | ||
384ba8b7 C |
221 | onVideoRemoved (video: Video) { |
222 | this.removeVideoFromArray(video.id) | |
2186386c | 223 | } |
1f30a185 | 224 | |
d846d99c | 225 | changeOwnership (video: Video) { |
74d63469 GR |
226 | this.videoChangeOwnershipModal.show(video) |
227 | } | |
228 | ||
489290b8 C |
229 | private removeVideoFromArray (id: number) { |
230 | this.videos = this.videos.filter(v => v.id !== id) | |
ce0e281d | 231 | } |
d846d99c C |
232 | |
233 | private buildActions () { | |
384ba8b7 C |
234 | this.moreVideoActions = [ |
235 | [ | |
236 | { | |
237 | label: $localize`Change ownership`, | |
238 | handler: ({ video }) => this.changeOwnership(video), | |
239 | iconName: 'ownership-change' | |
240 | } | |
241 | ] | |
d846d99c C |
242 | ] |
243 | } | |
202f6b6c | 244 | } |