]>
Commit | Line | Data |
---|---|---|
3caf77d3 | 1 | import { debounceTime, first, tap } from 'rxjs/operators' |
489290b8 | 2 | import { OnDestroy, OnInit } from '@angular/core' |
fd45e8f4 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
ad453580 | 4 | import { fromEvent, Observable, of, Subject, Subscription } from 'rxjs' |
b2731bff | 5 | import { AuthService } from '../../core/auth' |
440d39c5 | 6 | import { ComponentPaginationLight } from '../rest/component-pagination.model' |
7b87d2d5 | 7 | import { VideoSortField } from './sort-field.type' |
202f6b6c | 8 | import { Video } from './video.model' |
bbe0f064 | 9 | import { ScreenService } from '@app/shared/misc/screen.service' |
abf325b4 | 10 | import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component' |
c199c427 | 11 | import { Syndication } from '@app/shared/video/syndication.model' |
489290b8 C |
12 | import { Notifier, ServerService } from '@app/core' |
13 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' | |
34c7f429 | 14 | import { I18n } from '@ngx-translate/i18n-polyfill' |
93aa8552 | 15 | import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date' |
440d39c5 | 16 | import { ServerConfig } from '@shared/models' |
be27ef3b | 17 | import { GlobalIconName } from '@app/shared/images/global-icon.component' |
34c7f429 C |
18 | |
19 | enum GroupDate { | |
20 | UNKNOWN = 0, | |
21 | TODAY = 1, | |
22 | YESTERDAY = 2, | |
93aa8552 C |
23 | LAST_WEEK = 3, |
24 | LAST_MONTH = 4, | |
34c7f429 C |
25 | OLDER = 5 |
26 | } | |
0cd4344f | 27 | |
489290b8 | 28 | export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook { |
440d39c5 | 29 | pagination: ComponentPaginationLight = { |
fd45e8f4 | 30 | currentPage: 1, |
440d39c5 | 31 | itemsPerPage: 25 |
fd45e8f4 | 32 | } |
136cce4d | 33 | sort: VideoSortField = '-publishedAt' |
489290b8 | 34 | |
d59cba29 | 35 | categoryOneOf?: number |
3caf77d3 | 36 | languageOneOf?: string[] |
136cce4d | 37 | defaultSort: VideoSortField = '-publishedAt' |
489290b8 | 38 | |
c199c427 | 39 | syndicationItems: Syndication[] = [] |
244e76a5 | 40 | |
f3aaa9a9 | 41 | loadOnInit = true |
3caf77d3 | 42 | useUserVideoLanguagePreferences = false |
22a16e36 | 43 | ownerDisplayType: OwnerDisplayType = 'account' |
017c3dca | 44 | displayModerationBlock = false |
9b4b15f9 | 45 | titleTooltip: string |
3a0fb65c | 46 | displayVideoActions = true |
34c7f429 | 47 | groupByDate = false |
fd45e8f4 | 48 | |
3caf77d3 | 49 | videos: Video[] = [] |
440d39c5 | 50 | hasDoneFirstQuery = false |
489290b8 | 51 | disabled = false |
9af61e84 | 52 | |
abf325b4 C |
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 | ||
13adf228 RK |
63 | actions: { |
64 | routerLink: string | |
be27ef3b | 65 | iconName: GlobalIconName |
13adf228 RK |
66 | label: string |
67 | }[] = [] | |
68 | ||
ad453580 C |
69 | onDataSubject = new Subject<any[]>() |
70 | ||
ba430d75 C |
71 | protected serverConfig: ServerConfig |
72 | ||
f8b2c1b4 | 73 | protected abstract notifier: Notifier |
b2731bff | 74 | protected abstract authService: AuthService |
b2731bff | 75 | protected abstract route: ActivatedRoute |
489290b8 | 76 | protected abstract serverService: ServerService |
bbe0f064 | 77 | protected abstract screenService: ScreenService |
489290b8 | 78 | protected abstract router: Router |
34c7f429 | 79 | protected abstract i18n: I18n |
9bf9d2a5 | 80 | abstract titlePage: string |
c88593f7 | 81 | |
9af61e84 | 82 | private resizeSubscription: Subscription |
489290b8 C |
83 | private angularState: number |
84 | ||
34c7f429 C |
85 | private groupedDateLabels: { [id in GroupDate]: string } |
86 | private groupedDates: { [id: number]: GroupDate } = {} | |
87 | ||
440d39c5 C |
88 | private lastQueryLength: number |
89 | ||
90 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
9af61e84 | 91 | |
c199c427 | 92 | abstract generateSyndicationList (): void |
fd45e8f4 | 93 | |
b2731bff C |
94 | get user () { |
95 | return this.authService.getUser() | |
96 | } | |
97 | ||
fd45e8f4 | 98 | ngOnInit () { |
ba430d75 C |
99 | this.serverConfig = this.serverService.getTmpConfig() |
100 | this.serverService.getConfig() | |
101 | .subscribe(config => this.serverConfig = config) | |
102 | ||
34c7f429 C |
103 | this.groupedDateLabels = { |
104 | [GroupDate.UNKNOWN]: null, | |
105 | [GroupDate.TODAY]: this.i18n('Today'), | |
106 | [GroupDate.YESTERDAY]: this.i18n('Yesterday'), | |
93aa8552 C |
107 | [GroupDate.LAST_WEEK]: this.i18n('Last week'), |
108 | [GroupDate.LAST_MONTH]: this.i18n('Last month'), | |
34c7f429 C |
109 | [GroupDate.OLDER]: this.i18n('Older') |
110 | } | |
111 | ||
fd45e8f4 | 112 | // Subscribe to route changes |
5b5e333f | 113 | const routeParams = this.route.snapshot.queryParams |
2bbb3412 | 114 | this.loadRouteParams(routeParams) |
a2b817d3 | 115 | |
9af61e84 | 116 | this.resizeSubscription = fromEvent(window, 'resize') |
db400f44 | 117 | .pipe(debounceTime(500)) |
6194c1b4 | 118 | .subscribe(() => this.calcPageSizes()) |
3290f37c | 119 | |
6194c1b4 | 120 | this.calcPageSizes() |
3caf77d3 C |
121 | |
122 | const loadUserObservable = this.loadUserVideoLanguagesIfNeeded() | |
123 | ||
124 | if (this.loadOnInit === true) { | |
125 | loadUserObservable.subscribe(() => this.loadMoreVideos()) | |
126 | } | |
fd45e8f4 C |
127 | } |
128 | ||
9af61e84 C |
129 | ngOnDestroy () { |
130 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
131 | } | |
132 | ||
489290b8 C |
133 | disableForReuse () { |
134 | this.disabled = true | |
89724816 C |
135 | } |
136 | ||
489290b8 C |
137 | enabledForReuse () { |
138 | this.disabled = false | |
89724816 C |
139 | } |
140 | ||
489290b8 C |
141 | videoById (index: number, video: Video) { |
142 | return video.id | |
2bbb3412 C |
143 | } |
144 | ||
145 | onNearOfBottom () { | |
489290b8 | 146 | if (this.disabled) return |
2bbb3412 | 147 | |
440d39c5 C |
148 | // No more results |
149 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
0cd4344f | 150 | |
489290b8 | 151 | this.pagination.currentPage += 1 |
a8ecc6f6 | 152 | |
489290b8 | 153 | this.setScrollRouteParams() |
a8ecc6f6 | 154 | |
489290b8 C |
155 | this.loadMoreVideos() |
156 | } | |
fd45e8f4 | 157 | |
9d45db29 | 158 | loadMoreVideos (reset = false) { |
93cae479 | 159 | this.getVideosObservable(this.pagination.currentPage).subscribe( |
440d39c5 C |
160 | ({ data }) => { |
161 | this.hasDoneFirstQuery = true | |
162 | this.lastQueryLength = data.length | |
163 | ||
9d45db29 | 164 | if (reset) this.videos = [] |
93cae479 | 165 | this.videos = this.videos.concat(data) |
693263e9 | 166 | |
34c7f429 C |
167 | if (this.groupByDate) this.buildGroupedDateLabels() |
168 | ||
693263e9 | 169 | this.onMoreVideos() |
ad453580 C |
170 | |
171 | this.onDataSubject.next(data) | |
fd45e8f4 | 172 | }, |
017c3dca | 173 | |
f27a885a C |
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 | } | |
489290b8 | 180 | ) |
2bbb3412 C |
181 | } |
182 | ||
489290b8 C |
183 | reloadVideos () { |
184 | this.pagination.currentPage = 1 | |
9d45db29 | 185 | this.loadMoreVideos(true) |
fd45e8f4 C |
186 | } |
187 | ||
489290b8 C |
188 | toggleModerationDisplay () { |
189 | throw new Error('toggleModerationDisplay is not implemented') | |
fd45e8f4 C |
190 | } |
191 | ||
3a0fb65c C |
192 | removeVideoFromArray (video: Video) { |
193 | this.videos = this.videos.filter(v => v.id !== video.id) | |
194 | } | |
195 | ||
34c7f429 C |
196 | buildGroupedDateLabels () { |
197 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
198 | ||
199 | for (const video of this.videos) { | |
200 | const publishedDate = video.publishedAt | |
201 | ||
4e0c1793 C |
202 | if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) { |
203 | if (currentGroupedDate === GroupDate.TODAY) continue | |
204 | ||
34c7f429 C |
205 | currentGroupedDate = GroupDate.TODAY |
206 | this.groupedDates[ video.id ] = currentGroupedDate | |
207 | continue | |
208 | } | |
209 | ||
4e0c1793 C |
210 | if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) { |
211 | if (currentGroupedDate === GroupDate.YESTERDAY) continue | |
212 | ||
34c7f429 C |
213 | currentGroupedDate = GroupDate.YESTERDAY |
214 | this.groupedDates[ video.id ] = currentGroupedDate | |
215 | continue | |
216 | } | |
217 | ||
93aa8552 C |
218 | if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) { |
219 | if (currentGroupedDate === GroupDate.LAST_WEEK) continue | |
4e0c1793 | 220 | |
93aa8552 | 221 | currentGroupedDate = GroupDate.LAST_WEEK |
34c7f429 C |
222 | this.groupedDates[ video.id ] = currentGroupedDate |
223 | continue | |
224 | } | |
225 | ||
93aa8552 C |
226 | if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) { |
227 | if (currentGroupedDate === GroupDate.LAST_MONTH) continue | |
4e0c1793 | 228 | |
93aa8552 | 229 | currentGroupedDate = GroupDate.LAST_MONTH |
34c7f429 C |
230 | this.groupedDates[ video.id ] = currentGroupedDate |
231 | continue | |
232 | } | |
233 | ||
4e0c1793 C |
234 | if (currentGroupedDate <= GroupDate.OLDER) { |
235 | if (currentGroupedDate === GroupDate.OLDER) continue | |
236 | ||
34c7f429 C |
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 | ||
693263e9 C |
249 | // On videos hook for children that want to do something |
250 | protected onMoreVideos () { /* empty */ } | |
251 | ||
fd45e8f4 | 252 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { |
489290b8 C |
253 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort |
254 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
255 | this.angularState = routeParams[ 'a-state' ] | |
0cd4344f | 256 | } |
6194c1b4 C |
257 | |
258 | private calcPageSizes () { | |
489290b8 | 259 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 260 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 261 | } |
489290b8 | 262 | } |
6194c1b4 | 263 | |
489290b8 C |
264 | private setScrollRouteParams () { |
265 | // Already set | |
266 | if (this.angularState) return | |
6194c1b4 | 267 | |
489290b8 | 268 | this.angularState = 42 |
6194c1b4 | 269 | |
489290b8 C |
270 | const queryParams = { |
271 | 'a-state': this.angularState, | |
272 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 273 | } |
6194c1b4 | 274 | |
489290b8 | 275 | let path = this.router.url |
ba430d75 | 276 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute |
489290b8 C |
277 | |
278 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 279 | } |
3caf77d3 C |
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 | } | |
fd45e8f4 | 292 | } |