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 { 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'
16 import { ResultList } from '@shared/models'
27 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
28 pagination: ComponentPagination = {
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
52 displayOptions: MiniatureDisplayOptions = {
62 onDataSubject = new Subject<any[]>()
64 protected abstract notifier: Notifier
65 protected abstract authService: AuthService
66 protected abstract route: ActivatedRoute
67 protected abstract serverService: ServerService
68 protected abstract screenService: ScreenService
69 protected abstract router: Router
70 protected abstract i18n: I18n
71 abstract titlePage: string
73 private resizeSubscription: Subscription
74 private angularState: number
76 private groupedDateLabels: { [id in GroupDate]: string }
77 private groupedDates: { [id: number]: GroupDate } = {}
79 abstract getVideosObservable (page: number): Observable<ResultList<Video>>
81 abstract generateSyndicationList (): void
84 return this.authService.getUser()
88 this.groupedDateLabels = {
89 [GroupDate.UNKNOWN]: null,
90 [GroupDate.TODAY]: this.i18n('Today'),
91 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
92 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
93 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
94 [GroupDate.OLDER]: this.i18n('Older')
97 // Subscribe to route changes
98 const routeParams = this.route.snapshot.queryParams
99 this.loadRouteParams(routeParams)
101 this.resizeSubscription = fromEvent(window, 'resize')
102 .pipe(debounceTime(500))
103 .subscribe(() => this.calcPageSizes())
107 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
109 if (this.loadOnInit === true) {
110 loadUserObservable.subscribe(() => this.loadMoreVideos())
115 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
123 this.disabled = false
126 videoById (index: number, video: Video) {
131 if (this.disabled) return
134 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
136 this.pagination.currentPage += 1
138 this.setScrollRouteParams()
140 this.loadMoreVideos()
144 this.getVideosObservable(this.pagination.currentPage).subscribe(
145 ({ data, total }) => {
146 this.pagination.totalItems = total
147 this.videos = this.videos.concat(data)
149 if (this.groupByDate) this.buildGroupedDateLabels()
153 this.onDataSubject.next(data)
156 error => this.notifier.error(error.message)
161 this.pagination.currentPage = 1
163 this.loadMoreVideos()
166 toggleModerationDisplay () {
167 throw new Error('toggleModerationDisplay is not implemented')
170 removeVideoFromArray (video: Video) {
171 this.videos = this.videos.filter(v => v.id !== video.id)
174 buildGroupedDateLabels () {
175 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
177 for (const video of this.videos) {
178 const publishedDate = video.publishedAt
180 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
181 if (currentGroupedDate === GroupDate.TODAY) continue
183 currentGroupedDate = GroupDate.TODAY
184 this.groupedDates[ video.id ] = currentGroupedDate
188 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
189 if (currentGroupedDate === GroupDate.YESTERDAY) continue
191 currentGroupedDate = GroupDate.YESTERDAY
192 this.groupedDates[ video.id ] = currentGroupedDate
196 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
197 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
199 currentGroupedDate = GroupDate.LAST_WEEK
200 this.groupedDates[ video.id ] = currentGroupedDate
204 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
205 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
207 currentGroupedDate = GroupDate.LAST_MONTH
208 this.groupedDates[ video.id ] = currentGroupedDate
212 if (currentGroupedDate <= GroupDate.OLDER) {
213 if (currentGroupedDate === GroupDate.OLDER) continue
215 currentGroupedDate = GroupDate.OLDER
216 this.groupedDates[ video.id ] = currentGroupedDate
221 getCurrentGroupedDateLabel (video: Video) {
222 if (this.groupByDate === false) return undefined
224 return this.groupedDateLabels[this.groupedDates[video.id]]
227 // On videos hook for children that want to do something
228 protected onMoreVideos () { /* empty */ }
230 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
231 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
232 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
233 this.angularState = routeParams[ 'a-state' ]
236 private calcPageSizes () {
237 if (this.screenService.isInMobileView()) {
238 this.pagination.itemsPerPage = 5
242 private setScrollRouteParams () {
244 if (this.angularState) return
246 this.angularState = 42
248 const queryParams = {
249 'a-state': this.angularState,
250 categoryOneOf: this.categoryOneOf
253 let path = this.router.url
254 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
256 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
259 private loadUserVideoLanguagesIfNeeded () {
260 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
264 return this.authService.userInformationLoaded
267 tap(() => this.languageOneOf = this.user.videoLanguages)