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'
17 import { GlobalIconName } from '@app/shared/images/global-icon.component'
28 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
29 pagination: ComponentPaginationLight = {
33 sort: VideoSortField = '-publishedAt'
35 categoryOneOf?: number
36 languageOneOf?: string[]
37 defaultSort: VideoSortField = '-publishedAt'
39 syndicationItems: Syndication[] = []
42 useUserVideoLanguagePreferences = false
43 ownerDisplayType: OwnerDisplayType = 'account'
44 displayModerationBlock = false
46 displayVideoActions = true
50 hasDoneFirstQuery = false
53 displayOptions: MiniatureDisplayOptions = {
65 iconName: GlobalIconName
69 onDataSubject = new Subject<any[]>()
71 protected serverConfig: ServerConfig
73 protected abstract notifier: Notifier
74 protected abstract authService: AuthService
75 protected abstract route: ActivatedRoute
76 protected abstract serverService: ServerService
77 protected abstract screenService: ScreenService
78 protected abstract router: Router
79 protected abstract i18n: I18n
80 abstract titlePage: string
82 private resizeSubscription: Subscription
83 private angularState: number
85 private groupedDateLabels: { [id in GroupDate]: string }
86 private groupedDates: { [id: number]: GroupDate } = {}
88 private lastQueryLength: number
90 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
92 abstract generateSyndicationList (): void
95 return this.authService.getUser()
99 this.serverConfig = this.serverService.getTmpConfig()
100 this.serverService.getConfig()
101 .subscribe(config => this.serverConfig = config)
103 this.groupedDateLabels = {
104 [GroupDate.UNKNOWN]: null,
105 [GroupDate.TODAY]: this.i18n('Today'),
106 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
107 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
108 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
109 [GroupDate.OLDER]: this.i18n('Older')
112 // Subscribe to route changes
113 const routeParams = this.route.snapshot.queryParams
114 this.loadRouteParams(routeParams)
116 this.resizeSubscription = fromEvent(window, 'resize')
117 .pipe(debounceTime(500))
118 .subscribe(() => this.calcPageSizes())
122 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
124 if (this.loadOnInit === true) {
125 loadUserObservable.subscribe(() => this.loadMoreVideos())
130 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
138 this.disabled = false
141 videoById (index: number, video: Video) {
146 if (this.disabled) return
149 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
151 this.pagination.currentPage += 1
153 this.setScrollRouteParams()
155 this.loadMoreVideos()
158 loadMoreVideos (reset = false) {
159 this.getVideosObservable(this.pagination.currentPage).subscribe(
161 this.hasDoneFirstQuery = true
162 this.lastQueryLength = data.length
164 if (reset) this.videos = []
165 this.videos = this.videos.concat(data)
167 if (this.groupByDate) this.buildGroupedDateLabels()
171 this.onDataSubject.next(data)
175 const message = this.i18n('Cannot load more videos. Try again later.')
177 console.error(message, { error })
178 this.notifier.error(message)
184 this.pagination.currentPage = 1
185 this.loadMoreVideos(true)
188 toggleModerationDisplay () {
189 throw new Error('toggleModerationDisplay is not implemented')
192 removeVideoFromArray (video: Video) {
193 this.videos = this.videos.filter(v => v.id !== video.id)
196 buildGroupedDateLabels () {
197 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
199 for (const video of this.videos) {
200 const publishedDate = video.publishedAt
202 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
203 if (currentGroupedDate === GroupDate.TODAY) continue
205 currentGroupedDate = GroupDate.TODAY
206 this.groupedDates[ video.id ] = currentGroupedDate
210 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
211 if (currentGroupedDate === GroupDate.YESTERDAY) continue
213 currentGroupedDate = GroupDate.YESTERDAY
214 this.groupedDates[ video.id ] = currentGroupedDate
218 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
219 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
221 currentGroupedDate = GroupDate.LAST_WEEK
222 this.groupedDates[ video.id ] = currentGroupedDate
226 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
227 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
229 currentGroupedDate = GroupDate.LAST_MONTH
230 this.groupedDates[ video.id ] = currentGroupedDate
234 if (currentGroupedDate <= GroupDate.OLDER) {
235 if (currentGroupedDate === GroupDate.OLDER) continue
237 currentGroupedDate = GroupDate.OLDER
238 this.groupedDates[ video.id ] = currentGroupedDate
243 getCurrentGroupedDateLabel (video: Video) {
244 if (this.groupByDate === false) return undefined
246 return this.groupedDateLabels[this.groupedDates[video.id]]
249 // On videos hook for children that want to do something
250 protected onMoreVideos () { /* empty */ }
252 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
253 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
254 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
255 this.angularState = routeParams[ 'a-state' ]
258 private calcPageSizes () {
259 if (this.screenService.isInMobileView()) {
260 this.pagination.itemsPerPage = 5
264 private setScrollRouteParams () {
266 if (this.angularState) return
268 this.angularState = 42
270 const queryParams = {
271 'a-state': this.angularState,
272 categoryOneOf: this.categoryOneOf
275 let path = this.router.url
276 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
278 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
281 private loadUserVideoLanguagesIfNeeded () {
282 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
286 return this.authService.userInformationLoaded
289 tap(() => this.languageOneOf = this.user.videoLanguages)