]>
Commit | Line | Data |
---|---|---|
07f81d9d | 1 | import { fromEvent, Observable, ReplaySubject, Subject, Subscription } from 'rxjs' |
67ed6552 | 2 | import { debounceTime, switchMap, tap } from 'rxjs/operators' |
5bcbcbe3 RK |
3 | import { |
4 | AfterContentInit, | |
5 | ComponentFactoryResolver, | |
6 | Directive, | |
7 | Injector, | |
8 | OnDestroy, | |
9 | OnInit, | |
10 | Type, | |
11 | ViewChild, | |
12 | ViewContainerRef | |
13 | } from '@angular/core' | |
15bedeeb | 14 | import { ActivatedRoute, Params, Router } from '@angular/router' |
67ed6552 C |
15 | import { |
16 | AuthService, | |
17 | ComponentPaginationLight, | |
18 | LocalStorageService, | |
19 | Notifier, | |
20 | ScreenService, | |
21 | ServerService, | |
22 | User, | |
23 | UserService | |
24 | } from '@app/core' | |
489290b8 | 25 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' |
67ed6552 | 26 | import { GlobalIconName } from '@app/shared/shared-icons' |
15a7eafb | 27 | import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils' |
2989628b | 28 | import { HTMLServerConfig, UserRight, VideoFilter, VideoSortField } from '@shared/models' |
5c20a455 | 29 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' |
67ed6552 | 30 | import { Syndication, Video } from '../shared-main' |
5bcbcbe3 | 31 | import { GenericHeaderComponent, VideoListHeaderComponent } from './video-list-header.component' |
733dbc53 | 32 | import { MiniatureDisplayOptions } from './video-miniature.component' |
34c7f429 C |
33 | |
34 | enum GroupDate { | |
35 | UNKNOWN = 0, | |
36 | TODAY = 1, | |
37 | YESTERDAY = 2, | |
4166caab C |
38 | THIS_WEEK = 3, |
39 | THIS_MONTH = 4, | |
40 | LAST_MONTH = 5, | |
41 | OLDER = 6 | |
34c7f429 | 42 | } |
0cd4344f | 43 | |
583eb04b | 44 | @Directive() |
9df52d66 | 45 | // eslint-disable-next-line @angular-eslint/directive-class-suffix |
5bcbcbe3 RK |
46 | export abstract class AbstractVideoList implements OnInit, OnDestroy, AfterContentInit, DisableForReuseHook { |
47 | @ViewChild('videoListHeader', { static: true, read: ViewContainerRef }) videoListHeader: ViewContainerRef | |
48 | ||
49 | HeaderComponent: Type<GenericHeaderComponent> = VideoListHeaderComponent | |
50 | headerComponentInjector: Injector | |
51 | ||
440d39c5 | 52 | pagination: ComponentPaginationLight = { |
fd45e8f4 | 53 | currentPage: 1, |
440d39c5 | 54 | itemsPerPage: 25 |
fd45e8f4 | 55 | } |
136cce4d | 56 | sort: VideoSortField = '-publishedAt' |
489290b8 | 57 | |
5c20a455 | 58 | categoryOneOf?: number[] |
3caf77d3 | 59 | languageOneOf?: string[] |
5c20a455 | 60 | nsfwPolicy?: NSFWPolicyType |
136cce4d | 61 | defaultSort: VideoSortField = '-publishedAt' |
489290b8 | 62 | |
c199c427 | 63 | syndicationItems: Syndication[] = [] |
244e76a5 | 64 | |
f3aaa9a9 | 65 | loadOnInit = true |
07f81d9d C |
66 | loadUserVideoPreferences = false |
67 | ||
017c3dca | 68 | displayModerationBlock = false |
9b4b15f9 | 69 | titleTooltip: string |
3a0fb65c | 70 | displayVideoActions = true |
34c7f429 | 71 | groupByDate = false |
fd45e8f4 | 72 | |
3caf77d3 | 73 | videos: Video[] = [] |
440d39c5 | 74 | hasDoneFirstQuery = false |
489290b8 | 75 | disabled = false |
9af61e84 | 76 | |
abf325b4 C |
77 | displayOptions: MiniatureDisplayOptions = { |
78 | date: true, | |
79 | views: true, | |
80 | by: true, | |
cf78883c | 81 | avatar: false, |
abf325b4 C |
82 | privacyLabel: true, |
83 | privacyText: false, | |
84 | state: false, | |
85 | blacklistInfo: false | |
86 | } | |
87 | ||
13adf228 | 88 | actions: { |
be27ef3b | 89 | iconName: GlobalIconName |
13adf228 | 90 | label: string |
afff310e RK |
91 | justIcon?: boolean |
92 | routerLink?: string | |
7af5ded4 C |
93 | href?: string |
94 | click?: (e: Event) => void | |
13adf228 RK |
95 | }[] = [] |
96 | ||
ad453580 C |
97 | onDataSubject = new Subject<any[]>() |
98 | ||
5c20a455 C |
99 | userMiniature: User |
100 | ||
07f81d9d C |
101 | protected onUserLoadedSubject = new ReplaySubject<void>(1) |
102 | ||
2989628b | 103 | protected serverConfig: HTMLServerConfig |
ba430d75 | 104 | |
f8b2c1b4 | 105 | protected abstract notifier: Notifier |
b2731bff | 106 | protected abstract authService: AuthService |
d3217560 | 107 | protected abstract userService: UserService |
b2731bff | 108 | protected abstract route: ActivatedRoute |
489290b8 | 109 | protected abstract serverService: ServerService |
bbe0f064 | 110 | protected abstract screenService: ScreenService |
d3217560 | 111 | protected abstract storageService: LocalStorageService |
489290b8 | 112 | protected abstract router: Router |
5bcbcbe3 | 113 | protected abstract cfr: ComponentFactoryResolver |
9bf9d2a5 | 114 | abstract titlePage: string |
c88593f7 | 115 | |
9af61e84 | 116 | private resizeSubscription: Subscription |
489290b8 C |
117 | private angularState: number |
118 | ||
34c7f429 C |
119 | private groupedDateLabels: { [id in GroupDate]: string } |
120 | private groupedDates: { [id: number]: GroupDate } = {} | |
121 | ||
440d39c5 C |
122 | private lastQueryLength: number |
123 | ||
124 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
9af61e84 | 125 | |
c199c427 | 126 | abstract generateSyndicationList (): void |
fd45e8f4 C |
127 | |
128 | ngOnInit () { | |
2989628b | 129 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 130 | |
34c7f429 C |
131 | this.groupedDateLabels = { |
132 | [GroupDate.UNKNOWN]: null, | |
66357162 C |
133 | [GroupDate.TODAY]: $localize`Today`, |
134 | [GroupDate.YESTERDAY]: $localize`Yesterday`, | |
4166caab C |
135 | [GroupDate.THIS_WEEK]: $localize`This week`, |
136 | [GroupDate.THIS_MONTH]: $localize`This month`, | |
66357162 C |
137 | [GroupDate.LAST_MONTH]: $localize`Last month`, |
138 | [GroupDate.OLDER]: $localize`Older` | |
34c7f429 C |
139 | } |
140 | ||
fd45e8f4 | 141 | // Subscribe to route changes |
5b5e333f | 142 | const routeParams = this.route.snapshot.queryParams |
2bbb3412 | 143 | this.loadRouteParams(routeParams) |
a2b817d3 | 144 | |
9af61e84 | 145 | this.resizeSubscription = fromEvent(window, 'resize') |
db400f44 | 146 | .pipe(debounceTime(500)) |
6194c1b4 | 147 | .subscribe(() => this.calcPageSizes()) |
3290f37c | 148 | |
6194c1b4 | 149 | this.calcPageSizes() |
3caf77d3 | 150 | |
5c20a455 | 151 | const loadUserObservable = this.loadUserAndSettings() |
07f81d9d C |
152 | loadUserObservable.subscribe(() => { |
153 | this.onUserLoadedSubject.next() | |
3caf77d3 | 154 | |
07f81d9d C |
155 | if (this.loadOnInit === true) this.loadMoreVideos() |
156 | }) | |
d3217560 | 157 | |
5c20a455 C |
158 | this.userService.listenAnonymousUpdate() |
159 | .pipe(switchMap(() => this.loadUserAndSettings())) | |
160 | .subscribe(() => { | |
d3217560 | 161 | if (this.hasDoneFirstQuery) this.reloadVideos() |
5c20a455 | 162 | }) |
cf78883c C |
163 | |
164 | // Display avatar in mobile view | |
165 | if (this.screenService.isInMobileView()) { | |
166 | this.displayOptions.avatar = true | |
167 | } | |
fd45e8f4 C |
168 | } |
169 | ||
9af61e84 C |
170 | ngOnDestroy () { |
171 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
172 | } | |
173 | ||
5bcbcbe3 RK |
174 | ngAfterContentInit () { |
175 | if (this.videoListHeader) { | |
176 | // some components don't use the header: they use their own template, like my-history.component.html | |
9df52d66 | 177 | this.setHeader(this.HeaderComponent, this.headerComponentInjector) |
5bcbcbe3 RK |
178 | } |
179 | } | |
180 | ||
489290b8 C |
181 | disableForReuse () { |
182 | this.disabled = true | |
89724816 C |
183 | } |
184 | ||
489290b8 C |
185 | enabledForReuse () { |
186 | this.disabled = false | |
89724816 C |
187 | } |
188 | ||
489290b8 C |
189 | videoById (index: number, video: Video) { |
190 | return video.id | |
2bbb3412 C |
191 | } |
192 | ||
193 | onNearOfBottom () { | |
489290b8 | 194 | if (this.disabled) return |
2bbb3412 | 195 | |
440d39c5 C |
196 | // No more results |
197 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
0cd4344f | 198 | |
489290b8 | 199 | this.pagination.currentPage += 1 |
a8ecc6f6 | 200 | |
489290b8 | 201 | this.setScrollRouteParams() |
a8ecc6f6 | 202 | |
489290b8 C |
203 | this.loadMoreVideos() |
204 | } | |
fd45e8f4 | 205 | |
9d45db29 | 206 | loadMoreVideos (reset = false) { |
1378c0d3 C |
207 | this.getVideosObservable(this.pagination.currentPage) |
208 | .subscribe({ | |
209 | next: ({ data }) => { | |
210 | this.hasDoneFirstQuery = true | |
211 | this.lastQueryLength = data.length | |
440d39c5 | 212 | |
1378c0d3 C |
213 | if (reset) this.videos = [] |
214 | this.videos = this.videos.concat(data) | |
693263e9 | 215 | |
1378c0d3 | 216 | if (this.groupByDate) this.buildGroupedDateLabels() |
34c7f429 | 217 | |
1378c0d3 | 218 | this.onMoreVideos() |
ad453580 | 219 | |
1378c0d3 C |
220 | this.onDataSubject.next(data) |
221 | }, | |
017c3dca | 222 | |
1378c0d3 C |
223 | error: err => { |
224 | const message = $localize`Cannot load more videos. Try again later.` | |
f27a885a | 225 | |
1378c0d3 C |
226 | console.error(message, { err }) |
227 | this.notifier.error(message) | |
228 | } | |
229 | }) | |
2bbb3412 C |
230 | } |
231 | ||
489290b8 C |
232 | reloadVideos () { |
233 | this.pagination.currentPage = 1 | |
9d45db29 | 234 | this.loadMoreVideos(true) |
fd45e8f4 C |
235 | } |
236 | ||
3a0fb65c C |
237 | removeVideoFromArray (video: Video) { |
238 | this.videos = this.videos.filter(v => v.id !== video.id) | |
239 | } | |
240 | ||
34c7f429 C |
241 | buildGroupedDateLabels () { |
242 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
243 | ||
4166caab C |
244 | const periods = [ |
245 | { | |
246 | value: GroupDate.TODAY, | |
247 | validator: (d: Date) => isToday(d) | |
248 | }, | |
249 | { | |
250 | value: GroupDate.YESTERDAY, | |
251 | validator: (d: Date) => isYesterday(d) | |
252 | }, | |
253 | { | |
254 | value: GroupDate.THIS_WEEK, | |
255 | validator: (d: Date) => isLastWeek(d) | |
256 | }, | |
257 | { | |
258 | value: GroupDate.THIS_MONTH, | |
259 | validator: (d: Date) => isThisMonth(d) | |
260 | }, | |
261 | { | |
262 | value: GroupDate.LAST_MONTH, | |
263 | validator: (d: Date) => isLastMonth(d) | |
264 | }, | |
265 | { | |
266 | value: GroupDate.OLDER, | |
267 | validator: () => true | |
34c7f429 | 268 | } |
4166caab | 269 | ] |
34c7f429 | 270 | |
4166caab C |
271 | for (const video of this.videos) { |
272 | const publishedDate = video.publishedAt | |
34c7f429 | 273 | |
4166caab C |
274 | for (let i = 0; i < periods.length; i++) { |
275 | const period = periods[i] | |
4e0c1793 | 276 | |
4166caab | 277 | if (currentGroupedDate <= period.value && period.validator(publishedDate)) { |
34c7f429 | 278 | |
4166caab C |
279 | if (currentGroupedDate !== period.value) { |
280 | currentGroupedDate = period.value | |
9df52d66 | 281 | this.groupedDates[video.id] = currentGroupedDate |
4166caab | 282 | } |
4e0c1793 | 283 | |
4166caab C |
284 | break |
285 | } | |
34c7f429 C |
286 | } |
287 | } | |
288 | } | |
289 | ||
290 | getCurrentGroupedDateLabel (video: Video) { | |
291 | if (this.groupByDate === false) return undefined | |
292 | ||
293 | return this.groupedDateLabels[this.groupedDates[video.id]] | |
294 | } | |
295 | ||
0aa52e17 | 296 | toggleModerationDisplay () { |
5bcbcbe3 RK |
297 | throw new Error('toggleModerationDisplay ' + $localize`function is not implemented`) |
298 | } | |
299 | ||
300 | setHeader ( | |
301 | t: Type<any> = this.HeaderComponent, | |
302 | i: Injector = this.headerComponentInjector | |
303 | ) { | |
304 | const injector = i || Injector.create({ | |
9df52d66 | 305 | providers: [ { |
5bcbcbe3 RK |
306 | provide: 'data', |
307 | useValue: { | |
308 | titlePage: this.titlePage, | |
309 | titleTooltip: this.titleTooltip | |
310 | } | |
9df52d66 | 311 | } ] |
5bcbcbe3 RK |
312 | }) |
313 | const viewContainerRef = this.videoListHeader | |
314 | viewContainerRef.clear() | |
315 | ||
316 | const componentFactory = this.cfr.resolveComponentFactory(t) | |
317 | viewContainerRef.createComponent(componentFactory, 0, injector) | |
0aa52e17 C |
318 | } |
319 | ||
4d5e572f C |
320 | // Can be redefined by child |
321 | displayAsRow () { | |
322 | return false | |
323 | } | |
324 | ||
693263e9 C |
325 | // On videos hook for children that want to do something |
326 | protected onMoreVideos () { /* empty */ } | |
327 | ||
15bedeeb C |
328 | protected load () { /* empty */ } |
329 | ||
330 | // Hook if the page has custom route params | |
331 | protected loadPageRouteParams (_queryParams: Params) { /* empty */ } | |
332 | ||
333 | protected loadRouteParams (queryParams: Params) { | |
9df52d66 C |
334 | this.sort = queryParams['sort'] as VideoSortField || this.defaultSort |
335 | this.categoryOneOf = queryParams['categoryOneOf'] | |
336 | this.angularState = queryParams['a-state'] | |
15bedeeb C |
337 | |
338 | this.loadPageRouteParams(queryParams) | |
0cd4344f | 339 | } |
6194c1b4 | 340 | |
0aa52e17 C |
341 | protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) { |
342 | if (base === 'local') { | |
343 | return existing === 'local' | |
344 | ? 'all-local' as 'all-local' | |
345 | : 'local' as 'local' | |
346 | } | |
347 | ||
348 | return existing === 'all' | |
349 | ? null | |
350 | : 'all' | |
351 | } | |
352 | ||
353 | protected enableAllFilterIfPossible () { | |
354 | if (!this.authService.isLoggedIn()) return | |
355 | ||
356 | this.authService.userInformationLoaded | |
357 | .subscribe(() => { | |
358 | const user = this.authService.getUser() | |
359 | this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS) | |
360 | }) | |
361 | } | |
362 | ||
6194c1b4 | 363 | private calcPageSizes () { |
489290b8 | 364 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 365 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 366 | } |
489290b8 | 367 | } |
6194c1b4 | 368 | |
489290b8 C |
369 | private setScrollRouteParams () { |
370 | // Already set | |
371 | if (this.angularState) return | |
6194c1b4 | 372 | |
489290b8 | 373 | this.angularState = 42 |
6194c1b4 | 374 | |
489290b8 C |
375 | const queryParams = { |
376 | 'a-state': this.angularState, | |
377 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 378 | } |
6194c1b4 | 379 | |
fb7b009d | 380 | let path = this.getUrlWithoutParams() |
ba430d75 | 381 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute |
489290b8 C |
382 | |
383 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 384 | } |
3caf77d3 | 385 | |
5c20a455 C |
386 | private loadUserAndSettings () { |
387 | return this.userService.getAnonymousOrLoggedUser() | |
388 | .pipe(tap(user => { | |
389 | this.userMiniature = user | |
d3217560 | 390 | |
07f81d9d | 391 | if (!this.loadUserVideoPreferences) return |
3caf77d3 | 392 | |
5c20a455 C |
393 | this.languageOneOf = user.videoLanguages |
394 | this.nsfwPolicy = user.nsfwPolicy | |
395 | })) | |
3caf77d3 | 396 | } |
fb7b009d RK |
397 | |
398 | private getUrlWithoutParams () { | |
40930fda | 399 | const urlTree = this.router.parseUrl(this.router.url) |
fb7b009d | 400 | urlTree.queryParams = {} |
40930fda | 401 | |
fb7b009d RK |
402 | return urlTree.toString() |
403 | } | |
fd45e8f4 | 404 | } |