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, Subscription } from 'rxjs'
5 import { AuthService } from '../../core/auth'
6 import { ComponentPagination } 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'
26 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
27 pagination: ComponentPagination = {
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
51 displayOptions: MiniatureDisplayOptions = {
61 protected abstract notifier: Notifier
62 protected abstract authService: AuthService
63 protected abstract route: ActivatedRoute
64 protected abstract serverService: ServerService
65 protected abstract screenService: ScreenService
66 protected abstract router: Router
67 protected abstract i18n: I18n
68 abstract titlePage: string
70 private resizeSubscription: Subscription
71 private angularState: number
73 private groupedDateLabels: { [id in GroupDate]: string }
74 private groupedDates: { [id: number]: GroupDate } = {}
76 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
78 abstract generateSyndicationList (): void
81 return this.authService.getUser()
85 this.groupedDateLabels = {
86 [GroupDate.UNKNOWN]: null,
87 [GroupDate.TODAY]: this.i18n('Today'),
88 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
89 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
90 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
91 [GroupDate.OLDER]: this.i18n('Older')
94 // Subscribe to route changes
95 const routeParams = this.route.snapshot.queryParams
96 this.loadRouteParams(routeParams)
98 this.resizeSubscription = fromEvent(window, 'resize')
99 .pipe(debounceTime(500))
100 .subscribe(() => this.calcPageSizes())
104 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
106 if (this.loadOnInit === true) {
107 loadUserObservable.subscribe(() => this.loadMoreVideos())
112 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
120 this.disabled = false
123 videoById (index: number, video: Video) {
128 if (this.disabled) return
131 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
133 this.pagination.currentPage += 1
135 this.setScrollRouteParams()
137 this.loadMoreVideos()
141 const observable = this.getVideosObservable(this.pagination.currentPage)
143 observable.subscribe(
144 ({ videos, totalVideos }) => {
145 this.pagination.totalItems = totalVideos
146 this.videos = this.videos.concat(videos)
148 if (this.groupByDate) this.buildGroupedDateLabels()
153 error => this.notifier.error(error.message)
158 this.pagination.currentPage = 1
160 this.loadMoreVideos()
163 toggleModerationDisplay () {
164 throw new Error('toggleModerationDisplay is not implemented')
167 removeVideoFromArray (video: Video) {
168 this.videos = this.videos.filter(v => v.id !== video.id)
171 buildGroupedDateLabels () {
172 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
174 for (const video of this.videos) {
175 const publishedDate = video.publishedAt
177 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
178 if (currentGroupedDate === GroupDate.TODAY) continue
180 currentGroupedDate = GroupDate.TODAY
181 this.groupedDates[ video.id ] = currentGroupedDate
185 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
186 if (currentGroupedDate === GroupDate.YESTERDAY) continue
188 currentGroupedDate = GroupDate.YESTERDAY
189 this.groupedDates[ video.id ] = currentGroupedDate
193 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
194 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
196 currentGroupedDate = GroupDate.LAST_WEEK
197 this.groupedDates[ video.id ] = currentGroupedDate
201 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
202 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
204 currentGroupedDate = GroupDate.LAST_MONTH
205 this.groupedDates[ video.id ] = currentGroupedDate
209 if (currentGroupedDate <= GroupDate.OLDER) {
210 if (currentGroupedDate === GroupDate.OLDER) continue
212 currentGroupedDate = GroupDate.OLDER
213 this.groupedDates[ video.id ] = currentGroupedDate
218 getCurrentGroupedDateLabel (video: Video) {
219 if (this.groupByDate === false) return undefined
221 return this.groupedDateLabels[this.groupedDates[video.id]]
224 // On videos hook for children that want to do something
225 protected onMoreVideos () { /* empty */ }
227 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
228 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
229 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
230 this.angularState = routeParams[ 'a-state' ]
233 private calcPageSizes () {
234 if (this.screenService.isInMobileView()) {
235 this.pagination.itemsPerPage = 5
239 private setScrollRouteParams () {
241 if (this.angularState) return
243 this.angularState = 42
245 const queryParams = {
246 'a-state': this.angularState,
247 categoryOneOf: this.categoryOneOf
250 let path = this.router.url
251 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
253 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
256 private loadUserVideoLanguagesIfNeeded () {
257 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
261 return this.authService.userInformationLoaded
264 tap(() => this.languageOneOf = this.user.videoLanguages)