]>
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 | ||
108 | const channelFilters = this.userChannels.map(c => { | |
109 | return { | |
dd6d2a7c | 110 | value: 'channel:' + c.name, |
978c87e7 C |
111 | label: c.name |
112 | } | |
113 | }) | |
114 | ||
115 | this.inputFilters = [ | |
116 | { | |
117 | title: $localize`Advanced filters`, | |
118 | children: [ | |
119 | { | |
dd6d2a7c | 120 | value: 'isLive:true', |
978c87e7 C |
121 | label: $localize`Only live videos` |
122 | } | |
123 | ] | |
124 | }, | |
125 | ||
126 | { | |
127 | title: $localize`Channel filters`, | |
128 | children: channelFilters | |
129 | } | |
130 | ] | |
131 | }) | |
bf64ed41 RK |
132 | } |
133 | ||
2e46eb97 C |
134 | onSearch (search: string) { |
135 | this.search = search | |
136 | this.reloadData() | |
4f5d0459 RK |
137 | } |
138 | ||
2e46eb97 | 139 | reloadData () { |
1fd61899 | 140 | this.videosSelection.reloadVideos() |
bf64ed41 RK |
141 | } |
142 | ||
8e286cdc RK |
143 | onChangeSortColumn () { |
144 | this.videosSelection.reloadVideos() | |
145 | } | |
146 | ||
693263e9 | 147 | disableForReuse () { |
dd24f1bb | 148 | this.disabled = true |
9af61e84 C |
149 | } |
150 | ||
693263e9 | 151 | enabledForReuse () { |
dd24f1bb | 152 | this.disabled = false |
ce0e281d C |
153 | } |
154 | ||
0df302ca | 155 | getVideosObservable (page: number) { |
0cd4344f C |
156 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) |
157 | ||
978c87e7 C |
158 | return this.videoService.getMyVideos({ |
159 | videoPagination: newPagination, | |
160 | sort: this.sort, | |
161 | userChannels: this.userChannels, | |
162 | search: this.search | |
38a3ccc7 | 163 | }).pipe( |
164 | tap(res => this.pagination.totalItems = res.total), | |
165 | tap(({ data }) => this.fetchVideosContainedInPlaylists(data)) | |
166 | ) | |
167 | } | |
168 | ||
169 | private fetchVideosContainedInPlaylists (videos: Video[]) { | |
170 | this.playlistService.doVideosExistInPlaylist(videos.map(v => v.id)) | |
171 | .subscribe(result => { | |
172 | this.videosContainedInPlaylists = Object.keys(result).reduce((acc, videoId) => ({ | |
173 | ...acc, | |
174 | [videoId]: uniqBy(result[videoId], (p: VideoExistInPlaylist) => p.playlistId) | |
175 | }), this.videosContainedInPlaylists) | |
176 | }) | |
244e76a5 RK |
177 | } |
178 | ||
1f30a185 | 179 | async deleteSelectedVideos () { |
693263e9 | 180 | const toDeleteVideosIds = Object.keys(this.selection) |
9df52d66 | 181 | .filter(k => this.selection[k] === true) |
2186386c | 182 | .map(k => parseInt(k, 10)) |
ce0e281d | 183 | |
2186386c | 184 | const res = await this.confirmService.confirm( |
eaa52952 C |
185 | prepareIcu($localize`Do you really want to delete {length, plural, =1 {this video} other {{length} videos}}?`)( |
186 | { length: toDeleteVideosIds.length }, | |
187 | $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?` | |
188 | ), | |
66357162 | 189 | $localize`Delete` |
2186386c | 190 | ) |
1f30a185 C |
191 | if (res === false) return |
192 | ||
193 | const observables: Observable<any>[] = [] | |
194 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 195 | const o = this.videoService.removeVideo(videoId) |
489290b8 | 196 | .pipe(tap(() => this.removeVideoFromArray(videoId))) |
1f30a185 C |
197 | |
198 | observables.push(o) | |
199 | } | |
200 | ||
489290b8 C |
201 | concat(...observables) |
202 | .pipe(toArray()) | |
1378c0d3 C |
203 | .subscribe({ |
204 | next: () => { | |
eaa52952 C |
205 | this.notifier.success( |
206 | prepareIcu($localize`{length, plural, =1 {Video has been deleted} other {{length} videos have been deleted}}`)( | |
207 | { length: toDeleteVideosIds.length }, | |
208 | $localize`${toDeleteVideosIds.length} have been deleted.` | |
209 | ) | |
210 | ) | |
211 | ||
693263e9 | 212 | this.selection = {} |
1f30a185 C |
213 | }, |
214 | ||
1378c0d3 C |
215 | error: err => this.notifier.error(err.message) |
216 | }) | |
ce0e281d C |
217 | } |
218 | ||
384ba8b7 C |
219 | onVideoRemoved (video: Video) { |
220 | this.removeVideoFromArray(video.id) | |
2186386c | 221 | } |
1f30a185 | 222 | |
d846d99c | 223 | changeOwnership (video: Video) { |
74d63469 GR |
224 | this.videoChangeOwnershipModal.show(video) |
225 | } | |
226 | ||
489290b8 C |
227 | private removeVideoFromArray (id: number) { |
228 | this.videos = this.videos.filter(v => v.id !== id) | |
ce0e281d | 229 | } |
d846d99c C |
230 | |
231 | private buildActions () { | |
384ba8b7 C |
232 | this.moreVideoActions = [ |
233 | [ | |
234 | { | |
235 | label: $localize`Change ownership`, | |
236 | handler: ({ video }) => this.changeOwnership(video), | |
237 | iconName: 'ownership-change' | |
238 | } | |
239 | ] | |
d846d99c C |
240 | ] |
241 | } | |
202f6b6c | 242 | } |