]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { fromEvent, Observable, Subject, Subscription } from 'rxjs' |
2 | import { debounceTime, switchMap, tap } from 'rxjs/operators' | |
a02b93ce | 3 | import { Directive, 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' |
93aa8552 | 17 | import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date' |
67ed6552 | 18 | import { ServerConfig, VideoSortField } from '@shared/models' |
5c20a455 | 19 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' |
67ed6552 C |
20 | import { Syndication, Video } from '../shared-main' |
21 | import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component' | |
34c7f429 C |
22 | |
23 | enum GroupDate { | |
24 | UNKNOWN = 0, | |
25 | TODAY = 1, | |
26 | YESTERDAY = 2, | |
93aa8552 C |
27 | LAST_WEEK = 3, |
28 | LAST_MONTH = 4, | |
34c7f429 C |
29 | OLDER = 5 |
30 | } | |
0cd4344f | 31 | |
583eb04b | 32 | @Directive() |
a02b93ce | 33 | // tslint:disable-next-line: directive-class-suffix |
489290b8 | 34 | export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook { |
440d39c5 | 35 | pagination: ComponentPaginationLight = { |
fd45e8f4 | 36 | currentPage: 1, |
440d39c5 | 37 | itemsPerPage: 25 |
fd45e8f4 | 38 | } |
136cce4d | 39 | sort: VideoSortField = '-publishedAt' |
489290b8 | 40 | |
5c20a455 | 41 | categoryOneOf?: number[] |
3caf77d3 | 42 | languageOneOf?: string[] |
5c20a455 | 43 | nsfwPolicy?: NSFWPolicyType |
136cce4d | 44 | defaultSort: VideoSortField = '-publishedAt' |
489290b8 | 45 | |
c199c427 | 46 | syndicationItems: Syndication[] = [] |
244e76a5 | 47 | |
f3aaa9a9 | 48 | loadOnInit = true |
5c20a455 | 49 | useUserVideoPreferences = false |
22a16e36 | 50 | ownerDisplayType: OwnerDisplayType = 'account' |
017c3dca | 51 | displayModerationBlock = false |
9b4b15f9 | 52 | titleTooltip: string |
3a0fb65c | 53 | displayVideoActions = true |
34c7f429 | 54 | groupByDate = false |
fd45e8f4 | 55 | |
3caf77d3 | 56 | videos: Video[] = [] |
440d39c5 | 57 | hasDoneFirstQuery = false |
489290b8 | 58 | disabled = false |
9af61e84 | 59 | |
abf325b4 C |
60 | displayOptions: MiniatureDisplayOptions = { |
61 | date: true, | |
62 | views: true, | |
63 | by: true, | |
cf78883c | 64 | avatar: false, |
abf325b4 C |
65 | privacyLabel: true, |
66 | privacyText: false, | |
67 | state: false, | |
68 | blacklistInfo: false | |
69 | } | |
70 | ||
13adf228 RK |
71 | actions: { |
72 | routerLink: string | |
be27ef3b | 73 | iconName: GlobalIconName |
13adf228 RK |
74 | label: string |
75 | }[] = [] | |
76 | ||
ad453580 C |
77 | onDataSubject = new Subject<any[]>() |
78 | ||
5c20a455 C |
79 | userMiniature: User |
80 | ||
ba430d75 C |
81 | protected serverConfig: ServerConfig |
82 | ||
f8b2c1b4 | 83 | protected abstract notifier: Notifier |
b2731bff | 84 | protected abstract authService: AuthService |
d3217560 | 85 | protected abstract userService: UserService |
b2731bff | 86 | protected abstract route: ActivatedRoute |
489290b8 | 87 | protected abstract serverService: ServerService |
bbe0f064 | 88 | protected abstract screenService: ScreenService |
d3217560 | 89 | protected abstract storageService: LocalStorageService |
489290b8 | 90 | protected abstract router: Router |
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, | |
66357162 C |
112 | [GroupDate.TODAY]: $localize`Today`, |
113 | [GroupDate.YESTERDAY]: $localize`Yesterday`, | |
114 | [GroupDate.LAST_WEEK]: $localize`Last week`, | |
115 | [GroupDate.LAST_MONTH]: $localize`Last month`, | |
116 | [GroupDate.OLDER]: $localize`Older` | |
34c7f429 C |
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 | 192 | error => { |
66357162 | 193 | const message = $localize`Cannot load more videos. Try again later.` |
f27a885a C |
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 | } |