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