1 import { debounceTime, first, tap } from 'rxjs/operators'
2 import { OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { fromEvent, Observable, of, Subject, Subscription } from 'rxjs'
5 import { AuthService } from '../../core/auth'
6 import { ComponentPaginationLight } from '../rest/component-pagination.model'
7 import { VideoSortField } from './sort-field.type'
8 import { Video } from './video.model'
9 import { ScreenService } from '@app/shared/misc/screen.service'
10 import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component'
11 import { Syndication } from '@app/shared/video/syndication.model'
12 import { Notifier, ServerService } from '@app/core'
13 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
16 import { ServerConfig } from '@shared/models'
27 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
28 pagination: ComponentPaginationLight = {
32 sort: VideoSortField = '-publishedAt'
34 categoryOneOf?: number
35 languageOneOf?: string[]
36 defaultSort: VideoSortField = '-publishedAt'
38 syndicationItems: Syndication[] = []
41 useUserVideoLanguagePreferences = false
42 ownerDisplayType: OwnerDisplayType = 'account'
43 displayModerationBlock = false
45 displayVideoActions = true
49 hasDoneFirstQuery = false
52 displayOptions: MiniatureDisplayOptions = {
68 onDataSubject = new Subject<any[]>()
70 protected serverConfig: ServerConfig
72 protected abstract notifier: Notifier
73 protected abstract authService: AuthService
74 protected abstract route: ActivatedRoute
75 protected abstract serverService: ServerService
76 protected abstract screenService: ScreenService
77 protected abstract router: Router
78 protected abstract i18n: I18n
79 abstract titlePage: string
81 private resizeSubscription: Subscription
82 private angularState: number
84 private groupedDateLabels: { [id in GroupDate]: string }
85 private groupedDates: { [id: number]: GroupDate } = {}
87 private lastQueryLength: number
89 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
91 abstract generateSyndicationList (): void
94 return this.authService.getUser()
98 this.serverConfig = this.serverService.getTmpConfig()
99 this.serverService.getConfig()
100 .subscribe(config => this.serverConfig = config)
102 this.groupedDateLabels = {
103 [GroupDate.UNKNOWN]: null,
104 [GroupDate.TODAY]: this.i18n('Today'),
105 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
106 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
107 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
108 [GroupDate.OLDER]: this.i18n('Older')
111 // Subscribe to route changes
112 const routeParams = this.route.snapshot.queryParams
113 this.loadRouteParams(routeParams)
115 this.resizeSubscription = fromEvent(window, 'resize')
116 .pipe(debounceTime(500))
117 .subscribe(() => this.calcPageSizes())
121 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
123 if (this.loadOnInit === true) {
124 loadUserObservable.subscribe(() => this.loadMoreVideos())
129 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
137 this.disabled = false
140 videoById (index: number, video: Video) {
145 if (this.disabled) return
148 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
150 this.pagination.currentPage += 1
152 this.setScrollRouteParams()
154 this.loadMoreVideos()
157 loadMoreVideos (reset = false) {
158 this.getVideosObservable(this.pagination.currentPage).subscribe(
160 this.hasDoneFirstQuery = true
161 this.lastQueryLength = data.length
163 if (reset) this.videos = []
164 this.videos = this.videos.concat(data)
166 if (this.groupByDate) this.buildGroupedDateLabels()
170 this.onDataSubject.next(data)
174 const message = this.i18n('Cannot load more videos. Try again later.')
176 console.error(message, { error })
177 this.notifier.error(message)
183 this.pagination.currentPage = 1
184 this.loadMoreVideos(true)
187 toggleModerationDisplay () {
188 throw new Error('toggleModerationDisplay is not implemented')
191 removeVideoFromArray (video: Video) {
192 this.videos = this.videos.filter(v => v.id !== video.id)
195 buildGroupedDateLabels () {
196 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
198 for (const video of this.videos) {
199 const publishedDate = video.publishedAt
201 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
202 if (currentGroupedDate === GroupDate.TODAY) continue
204 currentGroupedDate = GroupDate.TODAY
205 this.groupedDates[ video.id ] = currentGroupedDate
209 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
210 if (currentGroupedDate === GroupDate.YESTERDAY) continue
212 currentGroupedDate = GroupDate.YESTERDAY
213 this.groupedDates[ video.id ] = currentGroupedDate
217 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
218 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
220 currentGroupedDate = GroupDate.LAST_WEEK
221 this.groupedDates[ video.id ] = currentGroupedDate
225 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
226 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
228 currentGroupedDate = GroupDate.LAST_MONTH
229 this.groupedDates[ video.id ] = currentGroupedDate
233 if (currentGroupedDate <= GroupDate.OLDER) {
234 if (currentGroupedDate === GroupDate.OLDER) continue
236 currentGroupedDate = GroupDate.OLDER
237 this.groupedDates[ video.id ] = currentGroupedDate
242 getCurrentGroupedDateLabel (video: Video) {
243 if (this.groupByDate === false) return undefined
245 return this.groupedDateLabels[this.groupedDates[video.id]]
248 // On videos hook for children that want to do something
249 protected onMoreVideos () { /* empty */ }
251 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
252 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
253 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
254 this.angularState = routeParams[ 'a-state' ]
257 private calcPageSizes () {
258 if (this.screenService.isInMobileView()) {
259 this.pagination.itemsPerPage = 5
263 private setScrollRouteParams () {
265 if (this.angularState) return
267 this.angularState = 42
269 const queryParams = {
270 'a-state': this.angularState,
271 categoryOneOf: this.categoryOneOf
274 let path = this.router.url
275 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
277 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
280 private loadUserVideoLanguagesIfNeeded () {
281 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
285 return this.authService.userInformationLoaded
288 tap(() => this.languageOneOf = this.user.videoLanguages)