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