]>
Commit | Line | Data |
---|---|---|
d3217560 | 1 | import { debounceTime, first, tap, throttleTime } 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' |
d3217560 RK |
18 | import { UserService, User } from '../users' |
19 | import { LocalStorageService } from '../misc/storage.service' | |
34c7f429 C |
20 | |
21 | enum GroupDate { | |
22 | UNKNOWN = 0, | |
23 | TODAY = 1, | |
24 | YESTERDAY = 2, | |
93aa8552 C |
25 | LAST_WEEK = 3, |
26 | LAST_MONTH = 4, | |
34c7f429 C |
27 | OLDER = 5 |
28 | } | |
0cd4344f | 29 | |
489290b8 | 30 | export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook { |
440d39c5 | 31 | pagination: ComponentPaginationLight = { |
fd45e8f4 | 32 | currentPage: 1, |
440d39c5 | 33 | itemsPerPage: 25 |
fd45e8f4 | 34 | } |
136cce4d | 35 | sort: VideoSortField = '-publishedAt' |
489290b8 | 36 | |
d59cba29 | 37 | categoryOneOf?: number |
3caf77d3 | 38 | languageOneOf?: string[] |
136cce4d | 39 | defaultSort: VideoSortField = '-publishedAt' |
489290b8 | 40 | |
c199c427 | 41 | syndicationItems: Syndication[] = [] |
244e76a5 | 42 | |
f3aaa9a9 | 43 | loadOnInit = true |
3caf77d3 | 44 | useUserVideoLanguagePreferences = false |
22a16e36 | 45 | ownerDisplayType: OwnerDisplayType = 'account' |
017c3dca | 46 | displayModerationBlock = false |
9b4b15f9 | 47 | titleTooltip: string |
3a0fb65c | 48 | displayVideoActions = true |
34c7f429 | 49 | groupByDate = false |
fd45e8f4 | 50 | |
3caf77d3 | 51 | videos: Video[] = [] |
440d39c5 | 52 | hasDoneFirstQuery = false |
489290b8 | 53 | disabled = false |
9af61e84 | 54 | |
abf325b4 C |
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 | ||
13adf228 RK |
65 | actions: { |
66 | routerLink: string | |
be27ef3b | 67 | iconName: GlobalIconName |
13adf228 RK |
68 | label: string |
69 | }[] = [] | |
70 | ||
ad453580 C |
71 | onDataSubject = new Subject<any[]>() |
72 | ||
ba430d75 C |
73 | protected serverConfig: ServerConfig |
74 | ||
f8b2c1b4 | 75 | protected abstract notifier: Notifier |
b2731bff | 76 | protected abstract authService: AuthService |
d3217560 | 77 | protected abstract userService: UserService |
b2731bff | 78 | protected abstract route: ActivatedRoute |
489290b8 | 79 | protected abstract serverService: ServerService |
bbe0f064 | 80 | protected abstract screenService: ScreenService |
d3217560 | 81 | protected abstract storageService: LocalStorageService |
489290b8 | 82 | protected abstract router: Router |
34c7f429 | 83 | protected abstract i18n: I18n |
9bf9d2a5 | 84 | abstract titlePage: string |
c88593f7 | 85 | |
9af61e84 | 86 | private resizeSubscription: Subscription |
489290b8 C |
87 | private angularState: number |
88 | ||
34c7f429 C |
89 | private groupedDateLabels: { [id in GroupDate]: string } |
90 | private groupedDates: { [id: number]: GroupDate } = {} | |
91 | ||
440d39c5 C |
92 | private lastQueryLength: number |
93 | ||
94 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
9af61e84 | 95 | |
c199c427 | 96 | abstract generateSyndicationList (): void |
fd45e8f4 | 97 | |
b2731bff C |
98 | get user () { |
99 | return this.authService.getUser() | |
100 | } | |
101 | ||
fd45e8f4 | 102 | ngOnInit () { |
ba430d75 C |
103 | this.serverConfig = this.serverService.getTmpConfig() |
104 | this.serverService.getConfig() | |
105 | .subscribe(config => this.serverConfig = config) | |
106 | ||
34c7f429 C |
107 | this.groupedDateLabels = { |
108 | [GroupDate.UNKNOWN]: null, | |
109 | [GroupDate.TODAY]: this.i18n('Today'), | |
110 | [GroupDate.YESTERDAY]: this.i18n('Yesterday'), | |
93aa8552 C |
111 | [GroupDate.LAST_WEEK]: this.i18n('Last week'), |
112 | [GroupDate.LAST_MONTH]: this.i18n('Last month'), | |
34c7f429 C |
113 | [GroupDate.OLDER]: this.i18n('Older') |
114 | } | |
115 | ||
fd45e8f4 | 116 | // Subscribe to route changes |
5b5e333f | 117 | const routeParams = this.route.snapshot.queryParams |
2bbb3412 | 118 | this.loadRouteParams(routeParams) |
a2b817d3 | 119 | |
9af61e84 | 120 | this.resizeSubscription = fromEvent(window, 'resize') |
db400f44 | 121 | .pipe(debounceTime(500)) |
6194c1b4 | 122 | .subscribe(() => this.calcPageSizes()) |
3290f37c | 123 | |
6194c1b4 | 124 | this.calcPageSizes() |
3caf77d3 C |
125 | |
126 | const loadUserObservable = this.loadUserVideoLanguagesIfNeeded() | |
127 | ||
128 | if (this.loadOnInit === true) { | |
129 | loadUserObservable.subscribe(() => this.loadMoreVideos()) | |
130 | } | |
d3217560 RK |
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 | ) | |
fd45e8f4 C |
141 | } |
142 | ||
9af61e84 C |
143 | ngOnDestroy () { |
144 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
145 | } | |
146 | ||
489290b8 C |
147 | disableForReuse () { |
148 | this.disabled = true | |
89724816 C |
149 | } |
150 | ||
489290b8 C |
151 | enabledForReuse () { |
152 | this.disabled = false | |
89724816 C |
153 | } |
154 | ||
489290b8 C |
155 | videoById (index: number, video: Video) { |
156 | return video.id | |
2bbb3412 C |
157 | } |
158 | ||
159 | onNearOfBottom () { | |
489290b8 | 160 | if (this.disabled) return |
2bbb3412 | 161 | |
440d39c5 C |
162 | // No more results |
163 | if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return | |
0cd4344f | 164 | |
489290b8 | 165 | this.pagination.currentPage += 1 |
a8ecc6f6 | 166 | |
489290b8 | 167 | this.setScrollRouteParams() |
a8ecc6f6 | 168 | |
489290b8 C |
169 | this.loadMoreVideos() |
170 | } | |
fd45e8f4 | 171 | |
9d45db29 | 172 | loadMoreVideos (reset = false) { |
93cae479 | 173 | this.getVideosObservable(this.pagination.currentPage).subscribe( |
440d39c5 C |
174 | ({ data }) => { |
175 | this.hasDoneFirstQuery = true | |
176 | this.lastQueryLength = data.length | |
177 | ||
9d45db29 | 178 | if (reset) this.videos = [] |
93cae479 | 179 | this.videos = this.videos.concat(data) |
693263e9 | 180 | |
34c7f429 C |
181 | if (this.groupByDate) this.buildGroupedDateLabels() |
182 | ||
693263e9 | 183 | this.onMoreVideos() |
ad453580 C |
184 | |
185 | this.onDataSubject.next(data) | |
fd45e8f4 | 186 | }, |
017c3dca | 187 | |
f27a885a C |
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 | } | |
489290b8 | 194 | ) |
2bbb3412 C |
195 | } |
196 | ||
489290b8 C |
197 | reloadVideos () { |
198 | this.pagination.currentPage = 1 | |
9d45db29 | 199 | this.loadMoreVideos(true) |
fd45e8f4 C |
200 | } |
201 | ||
489290b8 C |
202 | toggleModerationDisplay () { |
203 | throw new Error('toggleModerationDisplay is not implemented') | |
fd45e8f4 C |
204 | } |
205 | ||
3a0fb65c C |
206 | removeVideoFromArray (video: Video) { |
207 | this.videos = this.videos.filter(v => v.id !== video.id) | |
208 | } | |
209 | ||
34c7f429 C |
210 | buildGroupedDateLabels () { |
211 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
212 | ||
213 | for (const video of this.videos) { | |
214 | const publishedDate = video.publishedAt | |
215 | ||
4e0c1793 C |
216 | if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) { |
217 | if (currentGroupedDate === GroupDate.TODAY) continue | |
218 | ||
34c7f429 C |
219 | currentGroupedDate = GroupDate.TODAY |
220 | this.groupedDates[ video.id ] = currentGroupedDate | |
221 | continue | |
222 | } | |
223 | ||
4e0c1793 C |
224 | if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) { |
225 | if (currentGroupedDate === GroupDate.YESTERDAY) continue | |
226 | ||
34c7f429 C |
227 | currentGroupedDate = GroupDate.YESTERDAY |
228 | this.groupedDates[ video.id ] = currentGroupedDate | |
229 | continue | |
230 | } | |
231 | ||
93aa8552 C |
232 | if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) { |
233 | if (currentGroupedDate === GroupDate.LAST_WEEK) continue | |
4e0c1793 | 234 | |
93aa8552 | 235 | currentGroupedDate = GroupDate.LAST_WEEK |
34c7f429 C |
236 | this.groupedDates[ video.id ] = currentGroupedDate |
237 | continue | |
238 | } | |
239 | ||
93aa8552 C |
240 | if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) { |
241 | if (currentGroupedDate === GroupDate.LAST_MONTH) continue | |
4e0c1793 | 242 | |
93aa8552 | 243 | currentGroupedDate = GroupDate.LAST_MONTH |
34c7f429 C |
244 | this.groupedDates[ video.id ] = currentGroupedDate |
245 | continue | |
246 | } | |
247 | ||
4e0c1793 C |
248 | if (currentGroupedDate <= GroupDate.OLDER) { |
249 | if (currentGroupedDate === GroupDate.OLDER) continue | |
250 | ||
34c7f429 C |
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 | ||
693263e9 C |
263 | // On videos hook for children that want to do something |
264 | protected onMoreVideos () { /* empty */ } | |
265 | ||
fd45e8f4 | 266 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { |
489290b8 C |
267 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort |
268 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
269 | this.angularState = routeParams[ 'a-state' ] | |
0cd4344f | 270 | } |
6194c1b4 C |
271 | |
272 | private calcPageSizes () { | |
489290b8 | 273 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 274 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 275 | } |
489290b8 | 276 | } |
6194c1b4 | 277 | |
489290b8 C |
278 | private setScrollRouteParams () { |
279 | // Already set | |
280 | if (this.angularState) return | |
6194c1b4 | 281 | |
489290b8 | 282 | this.angularState = 42 |
6194c1b4 | 283 | |
489290b8 C |
284 | const queryParams = { |
285 | 'a-state': this.angularState, | |
286 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 287 | } |
6194c1b4 | 288 | |
489290b8 | 289 | let path = this.router.url |
ba430d75 | 290 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute |
489290b8 C |
291 | |
292 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 293 | } |
3caf77d3 C |
294 | |
295 | private loadUserVideoLanguagesIfNeeded () { | |
d3217560 RK |
296 | if (!this.useUserVideoLanguagePreferences) { |
297 | return of(true) | |
298 | } | |
299 | ||
300 | if (!this.authService.isLoggedIn()) { | |
301 | this.languageOneOf = this.userService.getAnonymousUser().videoLanguages | |
3caf77d3 C |
302 | return of(true) |
303 | } | |
304 | ||
305 | return this.authService.userInformationLoaded | |
306 | .pipe( | |
307 | first(), | |
308 | tap(() => this.languageOneOf = this.user.videoLanguages) | |
309 | ) | |
310 | } | |
fd45e8f4 | 311 | } |