]>
Commit | Line | Data |
---|---|---|
1 | import { debounceTime, first, tap, throttleTime } from 'rxjs/operators' | |
2 | import { OnDestroy, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
4 | import { fromEvent, Observable, of, Subject, Subscription } from 'rxjs' | |
5 | import { AuthService } from '../../core/auth' | |
6 | import { ComponentPaginationLight } from '../rest/component-pagination.model' | |
7 | import { VideoSortField } from './sort-field.type' | |
8 | import { Video } from './video.model' | |
9 | import { ScreenService } from '@app/shared/misc/screen.service' | |
10 | import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component' | |
11 | import { Syndication } from '@app/shared/video/syndication.model' | |
12 | import { Notifier, ServerService } from '@app/core' | |
13 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' | |
14 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
15 | import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date' | |
16 | import { ServerConfig } from '@shared/models' | |
17 | import { GlobalIconName } from '@app/shared/images/global-icon.component' | |
18 | import { UserService, User } from '../users' | |
19 | import { LocalStorageService } from '../misc/storage.service' | |
20 | ||
21 | enum GroupDate { | |
22 | UNKNOWN = 0, | |
23 | TODAY = 1, | |
24 | YESTERDAY = 2, | |
25 | LAST_WEEK = 3, | |
26 | LAST_MONTH = 4, | |
27 | OLDER = 5 | |
28 | } | |
29 | ||
30 | export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook { | |
31 | pagination: ComponentPaginationLight = { | |
32 | currentPage: 1, | |
33 | itemsPerPage: 25 | |
34 | } | |
35 | sort: VideoSortField = '-publishedAt' | |
36 | ||
37 | categoryOneOf?: number | |
38 | languageOneOf?: string[] | |
39 | defaultSort: VideoSortField = '-publishedAt' | |
40 | ||
41 | syndicationItems: Syndication[] = [] | |
42 | ||
43 | loadOnInit = true | |
44 | useUserVideoLanguagePreferences = false | |
45 | ownerDisplayType: OwnerDisplayType = 'account' | |
46 | displayModerationBlock = false | |
47 | titleTooltip: string | |
48 | displayVideoActions = true | |
49 | groupByDate = false | |
50 | ||
51 | videos: Video[] = [] | |
52 | hasDoneFirstQuery = false | |
53 | disabled = false | |
54 | ||
55 | displayOptions: MiniatureDisplayOptions = { | |
56 | date: true, | |
57 | views: true, | |
58 | by: true, | |
59 | privacyLabel: true, | |
60 | privacyText: false, | |
61 | state: false, | |
62 | blacklistInfo: false | |
63 | } | |
64 | ||
65 | actions: { | |
66 | routerLink: string | |
67 | iconName: GlobalIconName | |
68 | label: string | |
69 | }[] = [] | |
70 | ||
71 | onDataSubject = new Subject<any[]>() | |
72 | ||
73 | protected serverConfig: ServerConfig | |
74 | ||
75 | protected abstract notifier: Notifier | |
76 | protected abstract authService: AuthService | |
77 | protected abstract userService: UserService | |
78 | protected abstract route: ActivatedRoute | |
79 | protected abstract serverService: ServerService | |
80 | protected abstract screenService: ScreenService | |
81 | protected abstract storageService: LocalStorageService | |
82 | protected abstract router: Router | |
83 | protected abstract i18n: I18n | |
84 | abstract titlePage: string | |
85 | ||
86 | private resizeSubscription: Subscription | |
87 | private angularState: number | |
88 | ||
89 | private groupedDateLabels: { [id in GroupDate]: string } | |
90 | private groupedDates: { [id: number]: GroupDate } = {} | |
91 | ||
92 | private lastQueryLength: number | |
93 | ||
94 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
95 | ||
96 | abstract generateSyndicationList (): void | |
97 | ||
98 | get user () { | |
99 | return this.authService.getUser() | |
100 | } | |
101 | ||
102 | ngOnInit () { | |
103 | this.serverConfig = this.serverService.getTmpConfig() | |
104 | this.serverService.getConfig() | |
105 | .subscribe(config => this.serverConfig = config) | |
106 | ||
107 | this.groupedDateLabels = { | |
108 | [GroupDate.UNKNOWN]: null, | |
109 | [GroupDate.TODAY]: this.i18n('Today'), | |
110 | [GroupDate.YESTERDAY]: this.i18n('Yesterday'), | |
111 | [GroupDate.LAST_WEEK]: this.i18n('Last week'), | |
112 | [GroupDate.LAST_MONTH]: this.i18n('Last month'), | |
113 | [GroupDate.OLDER]: this.i18n('Older') | |
114 | } | |
115 | ||
116 | // Subscribe to route changes | |
117 | const routeParams = this.route.snapshot.queryParams | |
118 | this.loadRouteParams(routeParams) | |
119 | ||
120 | this.resizeSubscription = fromEvent(window, 'resize') | |
121 | .pipe(debounceTime(500)) | |
122 | .subscribe(() => this.calcPageSizes()) | |
123 | ||
124 | this.calcPageSizes() | |
125 | ||
126 | const loadUserObservable = this.loadUserVideoLanguagesIfNeeded() | |
127 | ||
128 | if (this.loadOnInit === true) { | |
129 | loadUserObservable.subscribe(() => this.loadMoreVideos()) | |
130 | } | |
131 | ||
132 | this.storageService.watch([ | |
133 | User.KEYS.NSFW_POLICY, | |
134 | User.KEYS.VIDEO_LANGUAGES | |
135 | ]).pipe(throttleTime(200)).subscribe( | |
136 | () => { | |
137 | this.loadUserVideoLanguagesIfNeeded() | |
138 | if (this.hasDoneFirstQuery) this.reloadVideos() | |
139 | } | |
140 | ) | |
141 | } | |
142 | ||
143 | ngOnDestroy () { | |
144 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
145 | } | |
146 | ||
147 | disableForReuse () { | |
148 | this.disabled = true | |
149 | } | |
150 | ||
151 | enabledForReuse () { | |
152 | this.disabled = false | |
153 | } | |
154 | ||
155 | videoById (index: number, video: Video) { | |
156 | return video.id | |
157 | } | |
158 | ||
159 | onNearOfBottom () { | |
160 | if (this.disabled) return | |
161 | ||
162 | // No more results | |
163 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
164 | ||
165 | this.pagination.currentPage += 1 | |
166 | ||
167 | this.setScrollRouteParams() | |
168 | ||
169 | this.loadMoreVideos() | |
170 | } | |
171 | ||
172 | loadMoreVideos (reset = false) { | |
173 | this.getVideosObservable(this.pagination.currentPage).subscribe( | |
174 | ({ data }) => { | |
175 | this.hasDoneFirstQuery = true | |
176 | this.lastQueryLength = data.length | |
177 | ||
178 | if (reset) this.videos = [] | |
179 | this.videos = this.videos.concat(data) | |
180 | ||
181 | if (this.groupByDate) this.buildGroupedDateLabels() | |
182 | ||
183 | this.onMoreVideos() | |
184 | ||
185 | this.onDataSubject.next(data) | |
186 | }, | |
187 | ||
188 | error => { | |
189 | const message = this.i18n('Cannot load more videos. Try again later.') | |
190 | ||
191 | console.error(message, { error }) | |
192 | this.notifier.error(message) | |
193 | } | |
194 | ) | |
195 | } | |
196 | ||
197 | reloadVideos () { | |
198 | this.pagination.currentPage = 1 | |
199 | this.loadMoreVideos(true) | |
200 | } | |
201 | ||
202 | toggleModerationDisplay () { | |
203 | throw new Error('toggleModerationDisplay is not implemented') | |
204 | } | |
205 | ||
206 | removeVideoFromArray (video: Video) { | |
207 | this.videos = this.videos.filter(v => v.id !== video.id) | |
208 | } | |
209 | ||
210 | buildGroupedDateLabels () { | |
211 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
212 | ||
213 | for (const video of this.videos) { | |
214 | const publishedDate = video.publishedAt | |
215 | ||
216 | if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) { | |
217 | if (currentGroupedDate === GroupDate.TODAY) continue | |
218 | ||
219 | currentGroupedDate = GroupDate.TODAY | |
220 | this.groupedDates[ video.id ] = currentGroupedDate | |
221 | continue | |
222 | } | |
223 | ||
224 | if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) { | |
225 | if (currentGroupedDate === GroupDate.YESTERDAY) continue | |
226 | ||
227 | currentGroupedDate = GroupDate.YESTERDAY | |
228 | this.groupedDates[ video.id ] = currentGroupedDate | |
229 | continue | |
230 | } | |
231 | ||
232 | if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) { | |
233 | if (currentGroupedDate === GroupDate.LAST_WEEK) continue | |
234 | ||
235 | currentGroupedDate = GroupDate.LAST_WEEK | |
236 | this.groupedDates[ video.id ] = currentGroupedDate | |
237 | continue | |
238 | } | |
239 | ||
240 | if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) { | |
241 | if (currentGroupedDate === GroupDate.LAST_MONTH) continue | |
242 | ||
243 | currentGroupedDate = GroupDate.LAST_MONTH | |
244 | this.groupedDates[ video.id ] = currentGroupedDate | |
245 | continue | |
246 | } | |
247 | ||
248 | if (currentGroupedDate <= GroupDate.OLDER) { | |
249 | if (currentGroupedDate === GroupDate.OLDER) continue | |
250 | ||
251 | currentGroupedDate = GroupDate.OLDER | |
252 | this.groupedDates[ video.id ] = currentGroupedDate | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
257 | getCurrentGroupedDateLabel (video: Video) { | |
258 | if (this.groupByDate === false) return undefined | |
259 | ||
260 | return this.groupedDateLabels[this.groupedDates[video.id]] | |
261 | } | |
262 | ||
263 | // On videos hook for children that want to do something | |
264 | protected onMoreVideos () { /* empty */ } | |
265 | ||
266 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { | |
267 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort | |
268 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
269 | this.angularState = routeParams[ 'a-state' ] | |
270 | } | |
271 | ||
272 | private calcPageSizes () { | |
273 | if (this.screenService.isInMobileView()) { | |
274 | this.pagination.itemsPerPage = 5 | |
275 | } | |
276 | } | |
277 | ||
278 | private setScrollRouteParams () { | |
279 | // Already set | |
280 | if (this.angularState) return | |
281 | ||
282 | this.angularState = 42 | |
283 | ||
284 | const queryParams = { | |
285 | 'a-state': this.angularState, | |
286 | categoryOneOf: this.categoryOneOf | |
287 | } | |
288 | ||
289 | let path = this.router.url | |
290 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute | |
291 | ||
292 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
293 | } | |
294 | ||
295 | private loadUserVideoLanguagesIfNeeded () { | |
296 | if (!this.useUserVideoLanguagePreferences) { | |
297 | return of(true) | |
298 | } | |
299 | ||
300 | if (!this.authService.isLoggedIn()) { | |
301 | this.languageOneOf = this.userService.getAnonymousUser().videoLanguages | |
302 | return of(true) | |
303 | } | |
304 | ||
305 | return this.authService.userInformationLoaded | |
306 | .pipe( | |
307 | first(), | |
308 | tap(() => this.languageOneOf = this.user.videoLanguages) | |
309 | ) | |
310 | } | |
311 | } |