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