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