]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { fromEvent, Observable, Subject, Subscription } from 'rxjs' |
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' | |
fd45e8f4 | 14 | import { ActivatedRoute, 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 |
5c20a455 | 66 | useUserVideoPreferences = false |
22a16e36 | 67 | ownerDisplayType: OwnerDisplayType = 'account' |
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 | ||
ba430d75 C |
101 | protected serverConfig: ServerConfig |
102 | ||
f8b2c1b4 | 103 | protected abstract notifier: Notifier |
b2731bff | 104 | protected abstract authService: AuthService |
d3217560 | 105 | protected abstract userService: UserService |
b2731bff | 106 | protected abstract route: ActivatedRoute |
489290b8 | 107 | protected abstract serverService: ServerService |
bbe0f064 | 108 | protected abstract screenService: ScreenService |
d3217560 | 109 | protected abstract storageService: LocalStorageService |
489290b8 | 110 | protected abstract router: Router |
5bcbcbe3 | 111 | protected abstract cfr: ComponentFactoryResolver |
9bf9d2a5 | 112 | abstract titlePage: string |
c88593f7 | 113 | |
9af61e84 | 114 | private resizeSubscription: Subscription |
489290b8 C |
115 | private angularState: number |
116 | ||
34c7f429 C |
117 | private groupedDateLabels: { [id in GroupDate]: string } |
118 | private groupedDates: { [id: number]: GroupDate } = {} | |
119 | ||
440d39c5 C |
120 | private lastQueryLength: number |
121 | ||
122 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
9af61e84 | 123 | |
c199c427 | 124 | abstract generateSyndicationList (): void |
fd45e8f4 C |
125 | |
126 | ngOnInit () { | |
ba430d75 C |
127 | this.serverConfig = this.serverService.getTmpConfig() |
128 | this.serverService.getConfig() | |
129 | .subscribe(config => this.serverConfig = config) | |
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() |
3caf77d3 C |
152 | |
153 | if (this.loadOnInit === true) { | |
154 | loadUserObservable.subscribe(() => this.loadMoreVideos()) | |
155 | } | |
d3217560 | 156 | |
5c20a455 C |
157 | this.userService.listenAnonymousUpdate() |
158 | .pipe(switchMap(() => this.loadUserAndSettings())) | |
159 | .subscribe(() => { | |
d3217560 | 160 | if (this.hasDoneFirstQuery) this.reloadVideos() |
5c20a455 | 161 | }) |
cf78883c C |
162 | |
163 | // Display avatar in mobile view | |
164 | if (this.screenService.isInMobileView()) { | |
165 | this.displayOptions.avatar = true | |
166 | } | |
fd45e8f4 C |
167 | } |
168 | ||
9af61e84 C |
169 | ngOnDestroy () { |
170 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
171 | } | |
172 | ||
5bcbcbe3 RK |
173 | ngAfterContentInit () { |
174 | if (this.videoListHeader) { | |
175 | // some components don't use the header: they use their own template, like my-history.component.html | |
176 | this.setHeader.apply(this, [ this.HeaderComponent, this.headerComponentInjector ]) | |
177 | } | |
178 | } | |
179 | ||
489290b8 C |
180 | disableForReuse () { |
181 | this.disabled = true | |
89724816 C |
182 | } |
183 | ||
489290b8 C |
184 | enabledForReuse () { |
185 | this.disabled = false | |
89724816 C |
186 | } |
187 | ||
489290b8 C |
188 | videoById (index: number, video: Video) { |
189 | return video.id | |
2bbb3412 C |
190 | } |
191 | ||
192 | onNearOfBottom () { | |
489290b8 | 193 | if (this.disabled) return |
2bbb3412 | 194 | |
440d39c5 C |
195 | // No more results |
196 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
0cd4344f | 197 | |
489290b8 | 198 | this.pagination.currentPage += 1 |
a8ecc6f6 | 199 | |
489290b8 | 200 | this.setScrollRouteParams() |
a8ecc6f6 | 201 | |
489290b8 C |
202 | this.loadMoreVideos() |
203 | } | |
fd45e8f4 | 204 | |
9d45db29 | 205 | loadMoreVideos (reset = false) { |
93cae479 | 206 | this.getVideosObservable(this.pagination.currentPage).subscribe( |
440d39c5 C |
207 | ({ data }) => { |
208 | this.hasDoneFirstQuery = true | |
209 | this.lastQueryLength = data.length | |
210 | ||
9d45db29 | 211 | if (reset) this.videos = [] |
93cae479 | 212 | this.videos = this.videos.concat(data) |
693263e9 | 213 | |
34c7f429 C |
214 | if (this.groupByDate) this.buildGroupedDateLabels() |
215 | ||
693263e9 | 216 | this.onMoreVideos() |
ad453580 C |
217 | |
218 | this.onDataSubject.next(data) | |
fd45e8f4 | 219 | }, |
017c3dca | 220 | |
f27a885a | 221 | error => { |
66357162 | 222 | const message = $localize`Cannot load more videos. Try again later.` |
f27a885a C |
223 | |
224 | console.error(message, { error }) | |
225 | this.notifier.error(message) | |
226 | } | |
489290b8 | 227 | ) |
2bbb3412 C |
228 | } |
229 | ||
489290b8 C |
230 | reloadVideos () { |
231 | this.pagination.currentPage = 1 | |
9d45db29 | 232 | this.loadMoreVideos(true) |
fd45e8f4 C |
233 | } |
234 | ||
3a0fb65c C |
235 | removeVideoFromArray (video: Video) { |
236 | this.videos = this.videos.filter(v => v.id !== video.id) | |
237 | } | |
238 | ||
34c7f429 C |
239 | buildGroupedDateLabels () { |
240 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
241 | ||
4166caab C |
242 | const periods = [ |
243 | { | |
244 | value: GroupDate.TODAY, | |
245 | validator: (d: Date) => isToday(d) | |
246 | }, | |
247 | { | |
248 | value: GroupDate.YESTERDAY, | |
249 | validator: (d: Date) => isYesterday(d) | |
250 | }, | |
251 | { | |
252 | value: GroupDate.THIS_WEEK, | |
253 | validator: (d: Date) => isLastWeek(d) | |
254 | }, | |
255 | { | |
256 | value: GroupDate.THIS_MONTH, | |
257 | validator: (d: Date) => isThisMonth(d) | |
258 | }, | |
259 | { | |
260 | value: GroupDate.LAST_MONTH, | |
261 | validator: (d: Date) => isLastMonth(d) | |
262 | }, | |
263 | { | |
264 | value: GroupDate.OLDER, | |
265 | validator: () => true | |
34c7f429 | 266 | } |
4166caab | 267 | ] |
34c7f429 | 268 | |
4166caab C |
269 | for (const video of this.videos) { |
270 | const publishedDate = video.publishedAt | |
34c7f429 | 271 | |
4166caab C |
272 | for (let i = 0; i < periods.length; i++) { |
273 | const period = periods[i] | |
4e0c1793 | 274 | |
4166caab | 275 | if (currentGroupedDate <= period.value && period.validator(publishedDate)) { |
34c7f429 | 276 | |
4166caab C |
277 | if (currentGroupedDate !== period.value) { |
278 | currentGroupedDate = period.value | |
279 | this.groupedDates[ video.id ] = currentGroupedDate | |
280 | } | |
4e0c1793 | 281 | |
4166caab C |
282 | break |
283 | } | |
34c7f429 C |
284 | } |
285 | } | |
286 | } | |
287 | ||
288 | getCurrentGroupedDateLabel (video: Video) { | |
289 | if (this.groupByDate === false) return undefined | |
290 | ||
291 | return this.groupedDateLabels[this.groupedDates[video.id]] | |
292 | } | |
293 | ||
0aa52e17 | 294 | toggleModerationDisplay () { |
5bcbcbe3 RK |
295 | throw new Error('toggleModerationDisplay ' + $localize`function is not implemented`) |
296 | } | |
297 | ||
298 | setHeader ( | |
299 | t: Type<any> = this.HeaderComponent, | |
300 | i: Injector = this.headerComponentInjector | |
301 | ) { | |
302 | const injector = i || Injector.create({ | |
303 | providers: [{ | |
304 | provide: 'data', | |
305 | useValue: { | |
306 | titlePage: this.titlePage, | |
307 | titleTooltip: this.titleTooltip | |
308 | } | |
309 | }] | |
310 | }) | |
311 | const viewContainerRef = this.videoListHeader | |
312 | viewContainerRef.clear() | |
313 | ||
314 | const componentFactory = this.cfr.resolveComponentFactory(t) | |
315 | viewContainerRef.createComponent(componentFactory, 0, injector) | |
0aa52e17 C |
316 | } |
317 | ||
693263e9 C |
318 | // On videos hook for children that want to do something |
319 | protected onMoreVideos () { /* empty */ } | |
320 | ||
fd45e8f4 | 321 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { |
489290b8 C |
322 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort |
323 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
324 | this.angularState = routeParams[ 'a-state' ] | |
0cd4344f | 325 | } |
6194c1b4 | 326 | |
0aa52e17 C |
327 | protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) { |
328 | if (base === 'local') { | |
329 | return existing === 'local' | |
330 | ? 'all-local' as 'all-local' | |
331 | : 'local' as 'local' | |
332 | } | |
333 | ||
334 | return existing === 'all' | |
335 | ? null | |
336 | : 'all' | |
337 | } | |
338 | ||
339 | protected enableAllFilterIfPossible () { | |
340 | if (!this.authService.isLoggedIn()) return | |
341 | ||
342 | this.authService.userInformationLoaded | |
343 | .subscribe(() => { | |
344 | const user = this.authService.getUser() | |
345 | this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS) | |
346 | }) | |
347 | } | |
348 | ||
6194c1b4 | 349 | private calcPageSizes () { |
489290b8 | 350 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 351 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 352 | } |
489290b8 | 353 | } |
6194c1b4 | 354 | |
489290b8 C |
355 | private setScrollRouteParams () { |
356 | // Already set | |
357 | if (this.angularState) return | |
6194c1b4 | 358 | |
489290b8 | 359 | this.angularState = 42 |
6194c1b4 | 360 | |
489290b8 C |
361 | const queryParams = { |
362 | 'a-state': this.angularState, | |
363 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 364 | } |
6194c1b4 | 365 | |
fb7b009d | 366 | let path = this.getUrlWithoutParams() |
ba430d75 | 367 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute |
489290b8 C |
368 | |
369 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 370 | } |
3caf77d3 | 371 | |
5c20a455 C |
372 | private loadUserAndSettings () { |
373 | return this.userService.getAnonymousOrLoggedUser() | |
374 | .pipe(tap(user => { | |
375 | this.userMiniature = user | |
d3217560 | 376 | |
5c20a455 | 377 | if (!this.useUserVideoPreferences) return |
3caf77d3 | 378 | |
5c20a455 C |
379 | this.languageOneOf = user.videoLanguages |
380 | this.nsfwPolicy = user.nsfwPolicy | |
381 | })) | |
3caf77d3 | 382 | } |
fb7b009d RK |
383 | |
384 | private getUrlWithoutParams () { | |
40930fda | 385 | const urlTree = this.router.parseUrl(this.router.url) |
fb7b009d | 386 | urlTree.queryParams = {} |
40930fda | 387 | |
fb7b009d RK |
388 | return urlTree.toString() |
389 | } | |
fd45e8f4 | 390 | } |