1 import { fromEvent, Observable, Subject, Subscription } from 'rxjs'
2 import { debounceTime, switchMap, tap } from 'rxjs/operators'
3 import { Directive, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
7 ComponentPaginationLight,
15 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
16 import { GlobalIconName } from '@app/shared/shared-icons'
17 import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils/miscs/date'
18 import { ServerConfig, UserRight, VideoFilter, VideoSortField } from '@shared/models'
19 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
20 import { Syndication, Video } from '../shared-main'
21 import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component'
34 // tslint:disable-next-line: directive-class-suffix
35 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
36 pagination: ComponentPaginationLight = {
40 sort: VideoSortField = '-publishedAt'
42 categoryOneOf?: number[]
43 languageOneOf?: string[]
44 nsfwPolicy?: NSFWPolicyType
45 defaultSort: VideoSortField = '-publishedAt'
47 syndicationItems: Syndication[] = []
50 useUserVideoPreferences = false
51 ownerDisplayType: OwnerDisplayType = 'account'
52 displayModerationBlock = false
54 displayVideoActions = true
58 hasDoneFirstQuery = false
61 displayOptions: MiniatureDisplayOptions = {
73 iconName: GlobalIconName
78 click?: (e: Event) => void
81 onDataSubject = new Subject<any[]>()
85 protected serverConfig: ServerConfig
87 protected abstract notifier: Notifier
88 protected abstract authService: AuthService
89 protected abstract userService: UserService
90 protected abstract route: ActivatedRoute
91 protected abstract serverService: ServerService
92 protected abstract screenService: ScreenService
93 protected abstract storageService: LocalStorageService
94 protected abstract router: Router
95 abstract titlePage: string
97 private resizeSubscription: Subscription
98 private angularState: number
100 private groupedDateLabels: { [id in GroupDate]: string }
101 private groupedDates: { [id: number]: GroupDate } = {}
103 private lastQueryLength: number
105 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
107 abstract generateSyndicationList (): void
110 this.serverConfig = this.serverService.getTmpConfig()
111 this.serverService.getConfig()
112 .subscribe(config => this.serverConfig = config)
114 this.groupedDateLabels = {
115 [GroupDate.UNKNOWN]: null,
116 [GroupDate.TODAY]: $localize`Today`,
117 [GroupDate.YESTERDAY]: $localize`Yesterday`,
118 [GroupDate.THIS_WEEK]: $localize`This week`,
119 [GroupDate.THIS_MONTH]: $localize`This month`,
120 [GroupDate.LAST_MONTH]: $localize`Last month`,
121 [GroupDate.OLDER]: $localize`Older`
124 // Subscribe to route changes
125 const routeParams = this.route.snapshot.queryParams
126 this.loadRouteParams(routeParams)
128 this.resizeSubscription = fromEvent(window, 'resize')
129 .pipe(debounceTime(500))
130 .subscribe(() => this.calcPageSizes())
134 const loadUserObservable = this.loadUserAndSettings()
136 if (this.loadOnInit === true) {
137 loadUserObservable.subscribe(() => this.loadMoreVideos())
140 this.userService.listenAnonymousUpdate()
141 .pipe(switchMap(() => this.loadUserAndSettings()))
143 if (this.hasDoneFirstQuery) this.reloadVideos()
146 // Display avatar in mobile view
147 if (this.screenService.isInMobileView()) {
148 this.displayOptions.avatar = true
153 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
161 this.disabled = false
164 videoById (index: number, video: Video) {
169 if (this.disabled) return
172 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
174 this.pagination.currentPage += 1
176 this.setScrollRouteParams()
178 this.loadMoreVideos()
181 loadMoreVideos (reset = false) {
182 this.getVideosObservable(this.pagination.currentPage).subscribe(
184 this.hasDoneFirstQuery = true
185 this.lastQueryLength = data.length
187 if (reset) this.videos = []
188 this.videos = this.videos.concat(data)
190 if (this.groupByDate) this.buildGroupedDateLabels()
194 this.onDataSubject.next(data)
198 const message = $localize`Cannot load more videos. Try again later.`
200 console.error(message, { error })
201 this.notifier.error(message)
207 this.pagination.currentPage = 1
208 this.loadMoreVideos(true)
211 removeVideoFromArray (video: Video) {
212 this.videos = this.videos.filter(v => v.id !== video.id)
215 buildGroupedDateLabels () {
216 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
220 value: GroupDate.TODAY,
221 validator: (d: Date) => isToday(d)
224 value: GroupDate.YESTERDAY,
225 validator: (d: Date) => isYesterday(d)
228 value: GroupDate.THIS_WEEK,
229 validator: (d: Date) => isLastWeek(d)
232 value: GroupDate.THIS_MONTH,
233 validator: (d: Date) => isThisMonth(d)
236 value: GroupDate.LAST_MONTH,
237 validator: (d: Date) => isLastMonth(d)
240 value: GroupDate.OLDER,
241 validator: () => true
245 for (const video of this.videos) {
246 const publishedDate = video.publishedAt
248 for (let i = 0; i < periods.length; i++) {
249 const period = periods[i]
251 if (currentGroupedDate <= period.value && period.validator(publishedDate)) {
253 if (currentGroupedDate !== period.value) {
254 currentGroupedDate = period.value
255 this.groupedDates[ video.id ] = currentGroupedDate
264 getCurrentGroupedDateLabel (video: Video) {
265 if (this.groupByDate === false) return undefined
267 return this.groupedDateLabels[this.groupedDates[video.id]]
270 toggleModerationDisplay () {
271 throw new Error('toggleModerationDisplay is not implemented')
274 // On videos hook for children that want to do something
275 protected onMoreVideos () { /* empty */ }
277 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
278 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
279 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
280 this.angularState = routeParams[ 'a-state' ]
283 protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) {
284 if (base === 'local') {
285 return existing === 'local'
286 ? 'all-local' as 'all-local'
290 return existing === 'all'
295 protected enableAllFilterIfPossible () {
296 if (!this.authService.isLoggedIn()) return
298 this.authService.userInformationLoaded
300 const user = this.authService.getUser()
301 this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS)
305 private calcPageSizes () {
306 if (this.screenService.isInMobileView()) {
307 this.pagination.itemsPerPage = 5
311 private setScrollRouteParams () {
313 if (this.angularState) return
315 this.angularState = 42
317 const queryParams = {
318 'a-state': this.angularState,
319 categoryOneOf: this.categoryOneOf
322 let path = this.router.url
323 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
325 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
328 private loadUserAndSettings () {
329 return this.userService.getAnonymousOrLoggedUser()
331 this.userMiniature = user
333 if (!this.useUserVideoPreferences) return
335 this.languageOneOf = user.videoLanguages
336 this.nsfwPolicy = user.nsfwPolicy