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