]>
Commit | Line | Data |
---|---|---|
1 | import { fromEvent, Observable, Subject, Subscription } from 'rxjs' | |
2 | import { debounceTime, switchMap, tap } from 'rxjs/operators' | |
3 | import { | |
4 | AfterContentInit, | |
5 | ComponentFactoryResolver, | |
6 | Directive, | |
7 | Injector, | |
8 | OnDestroy, | |
9 | OnInit, | |
10 | Type, | |
11 | ViewChild, | |
12 | ViewContainerRef | |
13 | } from '@angular/core' | |
14 | import { ActivatedRoute, Router } from '@angular/router' | |
15 | import { | |
16 | AuthService, | |
17 | ComponentPaginationLight, | |
18 | LocalStorageService, | |
19 | Notifier, | |
20 | ScreenService, | |
21 | ServerService, | |
22 | User, | |
23 | UserService | |
24 | } from '@app/core' | |
25 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' | |
26 | import { GlobalIconName } from '@app/shared/shared-icons' | |
27 | import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils/miscs/date' | |
28 | import { ServerConfig, UserRight, VideoFilter, VideoSortField } from '@shared/models' | |
29 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' | |
30 | import { Syndication, Video } from '../shared-main' | |
31 | import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component' | |
32 | import { GenericHeaderComponent, VideoListHeaderComponent } from './video-list-header.component' | |
33 | ||
34 | enum GroupDate { | |
35 | UNKNOWN = 0, | |
36 | TODAY = 1, | |
37 | YESTERDAY = 2, | |
38 | THIS_WEEK = 3, | |
39 | THIS_MONTH = 4, | |
40 | LAST_MONTH = 5, | |
41 | OLDER = 6 | |
42 | } | |
43 | ||
44 | @Directive() | |
45 | // tslint:disable-next-line: directive-class-suffix | |
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 | ||
52 | pagination: ComponentPaginationLight = { | |
53 | currentPage: 1, | |
54 | itemsPerPage: 25 | |
55 | } | |
56 | sort: VideoSortField = '-publishedAt' | |
57 | ||
58 | categoryOneOf?: number[] | |
59 | languageOneOf?: string[] | |
60 | nsfwPolicy?: NSFWPolicyType | |
61 | defaultSort: VideoSortField = '-publishedAt' | |
62 | ||
63 | syndicationItems: Syndication[] = [] | |
64 | ||
65 | loadOnInit = true | |
66 | useUserVideoPreferences = false | |
67 | ownerDisplayType: OwnerDisplayType = 'account' | |
68 | displayModerationBlock = false | |
69 | titleTooltip: string | |
70 | displayVideoActions = true | |
71 | groupByDate = false | |
72 | ||
73 | videos: Video[] = [] | |
74 | hasDoneFirstQuery = false | |
75 | disabled = false | |
76 | ||
77 | displayOptions: MiniatureDisplayOptions = { | |
78 | date: true, | |
79 | views: true, | |
80 | by: true, | |
81 | avatar: false, | |
82 | privacyLabel: true, | |
83 | privacyText: false, | |
84 | state: false, | |
85 | blacklistInfo: false | |
86 | } | |
87 | ||
88 | actions: { | |
89 | iconName: GlobalIconName | |
90 | label: string | |
91 | justIcon?: boolean | |
92 | routerLink?: string | |
93 | href?: string | |
94 | click?: (e: Event) => void | |
95 | }[] = [] | |
96 | ||
97 | onDataSubject = new Subject<any[]>() | |
98 | ||
99 | userMiniature: User | |
100 | ||
101 | protected serverConfig: ServerConfig | |
102 | ||
103 | protected abstract notifier: Notifier | |
104 | protected abstract authService: AuthService | |
105 | protected abstract userService: UserService | |
106 | protected abstract route: ActivatedRoute | |
107 | protected abstract serverService: ServerService | |
108 | protected abstract screenService: ScreenService | |
109 | protected abstract storageService: LocalStorageService | |
110 | protected abstract router: Router | |
111 | protected abstract cfr: ComponentFactoryResolver | |
112 | abstract titlePage: string | |
113 | ||
114 | private resizeSubscription: Subscription | |
115 | private angularState: number | |
116 | ||
117 | private groupedDateLabels: { [id in GroupDate]: string } | |
118 | private groupedDates: { [id: number]: GroupDate } = {} | |
119 | ||
120 | private lastQueryLength: number | |
121 | ||
122 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
123 | ||
124 | abstract generateSyndicationList (): void | |
125 | ||
126 | ngOnInit () { | |
127 | this.serverConfig = this.serverService.getTmpConfig() | |
128 | this.serverService.getConfig() | |
129 | .subscribe(config => this.serverConfig = config) | |
130 | ||
131 | this.groupedDateLabels = { | |
132 | [GroupDate.UNKNOWN]: null, | |
133 | [GroupDate.TODAY]: $localize`Today`, | |
134 | [GroupDate.YESTERDAY]: $localize`Yesterday`, | |
135 | [GroupDate.THIS_WEEK]: $localize`This week`, | |
136 | [GroupDate.THIS_MONTH]: $localize`This month`, | |
137 | [GroupDate.LAST_MONTH]: $localize`Last month`, | |
138 | [GroupDate.OLDER]: $localize`Older` | |
139 | } | |
140 | ||
141 | // Subscribe to route changes | |
142 | const routeParams = this.route.snapshot.queryParams | |
143 | this.loadRouteParams(routeParams) | |
144 | ||
145 | this.resizeSubscription = fromEvent(window, 'resize') | |
146 | .pipe(debounceTime(500)) | |
147 | .subscribe(() => this.calcPageSizes()) | |
148 | ||
149 | this.calcPageSizes() | |
150 | ||
151 | const loadUserObservable = this.loadUserAndSettings() | |
152 | ||
153 | if (this.loadOnInit === true) { | |
154 | loadUserObservable.subscribe(() => this.loadMoreVideos()) | |
155 | } | |
156 | ||
157 | this.userService.listenAnonymousUpdate() | |
158 | .pipe(switchMap(() => this.loadUserAndSettings())) | |
159 | .subscribe(() => { | |
160 | if (this.hasDoneFirstQuery) this.reloadVideos() | |
161 | }) | |
162 | ||
163 | // Display avatar in mobile view | |
164 | if (this.screenService.isInMobileView()) { | |
165 | this.displayOptions.avatar = true | |
166 | } | |
167 | } | |
168 | ||
169 | ngOnDestroy () { | |
170 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
171 | } | |
172 | ||
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 | ||
180 | disableForReuse () { | |
181 | this.disabled = true | |
182 | } | |
183 | ||
184 | enabledForReuse () { | |
185 | this.disabled = false | |
186 | } | |
187 | ||
188 | videoById (index: number, video: Video) { | |
189 | return video.id | |
190 | } | |
191 | ||
192 | onNearOfBottom () { | |
193 | if (this.disabled) return | |
194 | ||
195 | // No more results | |
196 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
197 | ||
198 | this.pagination.currentPage += 1 | |
199 | ||
200 | this.setScrollRouteParams() | |
201 | ||
202 | this.loadMoreVideos() | |
203 | } | |
204 | ||
205 | loadMoreVideos (reset = false) { | |
206 | this.getVideosObservable(this.pagination.currentPage).subscribe( | |
207 | ({ data }) => { | |
208 | this.hasDoneFirstQuery = true | |
209 | this.lastQueryLength = data.length | |
210 | ||
211 | if (reset) this.videos = [] | |
212 | this.videos = this.videos.concat(data) | |
213 | ||
214 | if (this.groupByDate) this.buildGroupedDateLabels() | |
215 | ||
216 | this.onMoreVideos() | |
217 | ||
218 | this.onDataSubject.next(data) | |
219 | }, | |
220 | ||
221 | error => { | |
222 | const message = $localize`Cannot load more videos. Try again later.` | |
223 | ||
224 | console.error(message, { error }) | |
225 | this.notifier.error(message) | |
226 | } | |
227 | ) | |
228 | } | |
229 | ||
230 | reloadVideos () { | |
231 | this.pagination.currentPage = 1 | |
232 | this.loadMoreVideos(true) | |
233 | } | |
234 | ||
235 | removeVideoFromArray (video: Video) { | |
236 | this.videos = this.videos.filter(v => v.id !== video.id) | |
237 | } | |
238 | ||
239 | buildGroupedDateLabels () { | |
240 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
241 | ||
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 | |
266 | } | |
267 | ] | |
268 | ||
269 | for (const video of this.videos) { | |
270 | const publishedDate = video.publishedAt | |
271 | ||
272 | for (let i = 0; i < periods.length; i++) { | |
273 | const period = periods[i] | |
274 | ||
275 | if (currentGroupedDate <= period.value && period.validator(publishedDate)) { | |
276 | ||
277 | if (currentGroupedDate !== period.value) { | |
278 | currentGroupedDate = period.value | |
279 | this.groupedDates[ video.id ] = currentGroupedDate | |
280 | } | |
281 | ||
282 | break | |
283 | } | |
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 | ||
294 | toggleModerationDisplay () { | |
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) | |
316 | } | |
317 | ||
318 | // On videos hook for children that want to do something | |
319 | protected onMoreVideos () { /* empty */ } | |
320 | ||
321 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { | |
322 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort | |
323 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
324 | this.angularState = routeParams[ 'a-state' ] | |
325 | } | |
326 | ||
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 | ||
349 | private calcPageSizes () { | |
350 | if (this.screenService.isInMobileView()) { | |
351 | this.pagination.itemsPerPage = 5 | |
352 | } | |
353 | } | |
354 | ||
355 | private setScrollRouteParams () { | |
356 | // Already set | |
357 | if (this.angularState) return | |
358 | ||
359 | this.angularState = 42 | |
360 | ||
361 | const queryParams = { | |
362 | 'a-state': this.angularState, | |
363 | categoryOneOf: this.categoryOneOf | |
364 | } | |
365 | ||
366 | let path = this.getUrlWithoutParams() | |
367 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute | |
368 | ||
369 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
370 | } | |
371 | ||
372 | private loadUserAndSettings () { | |
373 | return this.userService.getAnonymousOrLoggedUser() | |
374 | .pipe(tap(user => { | |
375 | this.userMiniature = user | |
376 | ||
377 | if (!this.useUserVideoPreferences) return | |
378 | ||
379 | this.languageOneOf = user.videoLanguages | |
380 | this.nsfwPolicy = user.nsfwPolicy | |
381 | })) | |
382 | } | |
383 | ||
384 | private getUrlWithoutParams () { | |
385 | const urlTree = this.router.parseUrl(this.router.url) | |
386 | urlTree.queryParams = {} | |
387 | ||
388 | return urlTree.toString() | |
389 | } | |
390 | } |