]>
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' |
4166caab | 17 | import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils/miscs/date' |
0aa52e17 | 18 | import { ServerConfig, UserRight, VideoFilter, 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, | |
4166caab C |
27 | THIS_WEEK = 3, |
28 | THIS_MONTH = 4, | |
29 | LAST_MONTH = 5, | |
30 | OLDER = 6 | |
34c7f429 | 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 |
9bf9d2a5 | 92 | abstract titlePage: string |
c88593f7 | 93 | |
9af61e84 | 94 | private resizeSubscription: Subscription |
489290b8 C |
95 | private angularState: number |
96 | ||
34c7f429 C |
97 | private groupedDateLabels: { [id in GroupDate]: string } |
98 | private groupedDates: { [id: number]: GroupDate } = {} | |
99 | ||
440d39c5 C |
100 | private lastQueryLength: number |
101 | ||
102 | abstract getVideosObservable (page: number): Observable<{ data: Video[] }> | |
9af61e84 | 103 | |
c199c427 | 104 | abstract generateSyndicationList (): void |
fd45e8f4 C |
105 | |
106 | ngOnInit () { | |
ba430d75 C |
107 | this.serverConfig = this.serverService.getTmpConfig() |
108 | this.serverService.getConfig() | |
109 | .subscribe(config => this.serverConfig = config) | |
110 | ||
34c7f429 C |
111 | this.groupedDateLabels = { |
112 | [GroupDate.UNKNOWN]: null, | |
66357162 C |
113 | [GroupDate.TODAY]: $localize`Today`, |
114 | [GroupDate.YESTERDAY]: $localize`Yesterday`, | |
4166caab C |
115 | [GroupDate.THIS_WEEK]: $localize`This week`, |
116 | [GroupDate.THIS_MONTH]: $localize`This month`, | |
66357162 C |
117 | [GroupDate.LAST_MONTH]: $localize`Last month`, |
118 | [GroupDate.OLDER]: $localize`Older` | |
34c7f429 C |
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 | 194 | error => { |
66357162 | 195 | const message = $localize`Cannot load more videos. Try again later.` |
f27a885a C |
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 | ||
3a0fb65c C |
208 | removeVideoFromArray (video: Video) { |
209 | this.videos = this.videos.filter(v => v.id !== video.id) | |
210 | } | |
211 | ||
34c7f429 C |
212 | buildGroupedDateLabels () { |
213 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
214 | ||
4166caab C |
215 | const periods = [ |
216 | { | |
217 | value: GroupDate.TODAY, | |
218 | validator: (d: Date) => isToday(d) | |
219 | }, | |
220 | { | |
221 | value: GroupDate.YESTERDAY, | |
222 | validator: (d: Date) => isYesterday(d) | |
223 | }, | |
224 | { | |
225 | value: GroupDate.THIS_WEEK, | |
226 | validator: (d: Date) => isLastWeek(d) | |
227 | }, | |
228 | { | |
229 | value: GroupDate.THIS_MONTH, | |
230 | validator: (d: Date) => isThisMonth(d) | |
231 | }, | |
232 | { | |
233 | value: GroupDate.LAST_MONTH, | |
234 | validator: (d: Date) => isLastMonth(d) | |
235 | }, | |
236 | { | |
237 | value: GroupDate.OLDER, | |
238 | validator: () => true | |
34c7f429 | 239 | } |
4166caab | 240 | ] |
34c7f429 | 241 | |
4166caab C |
242 | for (const video of this.videos) { |
243 | const publishedDate = video.publishedAt | |
34c7f429 | 244 | |
4166caab C |
245 | for (let i = 0; i < periods.length; i++) { |
246 | const period = periods[i] | |
4e0c1793 | 247 | |
4166caab | 248 | if (currentGroupedDate <= period.value && period.validator(publishedDate)) { |
34c7f429 | 249 | |
4166caab C |
250 | if (currentGroupedDate !== period.value) { |
251 | currentGroupedDate = period.value | |
252 | this.groupedDates[ video.id ] = currentGroupedDate | |
253 | } | |
4e0c1793 | 254 | |
4166caab C |
255 | break |
256 | } | |
34c7f429 C |
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 | ||
0aa52e17 C |
267 | toggleModerationDisplay () { |
268 | throw new Error('toggleModerationDisplay is not implemented') | |
269 | } | |
270 | ||
693263e9 C |
271 | // On videos hook for children that want to do something |
272 | protected onMoreVideos () { /* empty */ } | |
273 | ||
fd45e8f4 | 274 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { |
489290b8 C |
275 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort |
276 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
277 | this.angularState = routeParams[ 'a-state' ] | |
0cd4344f | 278 | } |
6194c1b4 | 279 | |
0aa52e17 C |
280 | protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) { |
281 | if (base === 'local') { | |
282 | return existing === 'local' | |
283 | ? 'all-local' as 'all-local' | |
284 | : 'local' as 'local' | |
285 | } | |
286 | ||
287 | return existing === 'all' | |
288 | ? null | |
289 | : 'all' | |
290 | } | |
291 | ||
292 | protected enableAllFilterIfPossible () { | |
293 | if (!this.authService.isLoggedIn()) return | |
294 | ||
295 | this.authService.userInformationLoaded | |
296 | .subscribe(() => { | |
297 | const user = this.authService.getUser() | |
298 | this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS) | |
299 | }) | |
300 | } | |
301 | ||
6194c1b4 | 302 | private calcPageSizes () { |
489290b8 | 303 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 304 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 305 | } |
489290b8 | 306 | } |
6194c1b4 | 307 | |
489290b8 C |
308 | private setScrollRouteParams () { |
309 | // Already set | |
310 | if (this.angularState) return | |
6194c1b4 | 311 | |
489290b8 | 312 | this.angularState = 42 |
6194c1b4 | 313 | |
489290b8 C |
314 | const queryParams = { |
315 | 'a-state': this.angularState, | |
316 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 317 | } |
6194c1b4 | 318 | |
489290b8 | 319 | let path = this.router.url |
ba430d75 | 320 | if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute |
489290b8 C |
321 | |
322 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 323 | } |
3caf77d3 | 324 | |
5c20a455 C |
325 | private loadUserAndSettings () { |
326 | return this.userService.getAnonymousOrLoggedUser() | |
327 | .pipe(tap(user => { | |
328 | this.userMiniature = user | |
d3217560 | 329 | |
5c20a455 | 330 | if (!this.useUserVideoPreferences) return |
3caf77d3 | 331 | |
5c20a455 C |
332 | this.languageOneOf = user.videoLanguages |
333 | this.nsfwPolicy = user.nsfwPolicy | |
334 | })) | |
3caf77d3 | 335 | } |
fd45e8f4 | 336 | } |