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, ServerConfig } 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 serverConfig: ServerConfig
66 protected abstract notifier: Notifier
67 protected abstract authService: AuthService
68 protected abstract route: ActivatedRoute
69 protected abstract serverService: ServerService
70 protected abstract screenService: ScreenService
71 protected abstract router: Router
72 protected abstract i18n: I18n
73 abstract titlePage: string
75 private resizeSubscription: Subscription
76 private angularState: number
78 private groupedDateLabels: { [id in GroupDate]: string }
79 private groupedDates: { [id: number]: GroupDate } = {}
81 abstract getVideosObservable (page: number): Observable<ResultList<Video>>
83 abstract generateSyndicationList (): void
86 return this.authService.getUser()
90 this.serverConfig = this.serverService.getTmpConfig()
91 this.serverService.getConfig()
92 .subscribe(config => this.serverConfig = config)
94 this.groupedDateLabels = {
95 [GroupDate.UNKNOWN]: null,
96 [GroupDate.TODAY]: this.i18n('Today'),
97 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
98 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
99 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
100 [GroupDate.OLDER]: this.i18n('Older')
103 // Subscribe to route changes
104 const routeParams = this.route.snapshot.queryParams
105 this.loadRouteParams(routeParams)
107 this.resizeSubscription = fromEvent(window, 'resize')
108 .pipe(debounceTime(500))
109 .subscribe(() => this.calcPageSizes())
113 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
115 if (this.loadOnInit === true) {
116 loadUserObservable.subscribe(() => this.loadMoreVideos())
121 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
129 this.disabled = false
132 videoById (index: number, video: Video) {
137 if (this.disabled) return
140 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
142 this.pagination.currentPage += 1
144 this.setScrollRouteParams()
146 this.loadMoreVideos()
150 this.getVideosObservable(this.pagination.currentPage).subscribe(
151 ({ data, total }) => {
152 this.pagination.totalItems = total
153 this.videos = this.videos.concat(data)
155 if (this.groupByDate) this.buildGroupedDateLabels()
159 this.onDataSubject.next(data)
162 error => this.notifier.error(error.message)
167 this.pagination.currentPage = 1
169 this.loadMoreVideos()
172 toggleModerationDisplay () {
173 throw new Error('toggleModerationDisplay is not implemented')
176 removeVideoFromArray (video: Video) {
177 this.videos = this.videos.filter(v => v.id !== video.id)
180 buildGroupedDateLabels () {
181 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
183 for (const video of this.videos) {
184 const publishedDate = video.publishedAt
186 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
187 if (currentGroupedDate === GroupDate.TODAY) continue
189 currentGroupedDate = GroupDate.TODAY
190 this.groupedDates[ video.id ] = currentGroupedDate
194 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
195 if (currentGroupedDate === GroupDate.YESTERDAY) continue
197 currentGroupedDate = GroupDate.YESTERDAY
198 this.groupedDates[ video.id ] = currentGroupedDate
202 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
203 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
205 currentGroupedDate = GroupDate.LAST_WEEK
206 this.groupedDates[ video.id ] = currentGroupedDate
210 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
211 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
213 currentGroupedDate = GroupDate.LAST_MONTH
214 this.groupedDates[ video.id ] = currentGroupedDate
218 if (currentGroupedDate <= GroupDate.OLDER) {
219 if (currentGroupedDate === GroupDate.OLDER) continue
221 currentGroupedDate = GroupDate.OLDER
222 this.groupedDates[ video.id ] = currentGroupedDate
227 getCurrentGroupedDateLabel (video: Video) {
228 if (this.groupByDate === false) return undefined
230 return this.groupedDateLabels[this.groupedDates[video.id]]
233 // On videos hook for children that want to do something
234 protected onMoreVideos () { /* empty */ }
236 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
237 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
238 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
239 this.angularState = routeParams[ 'a-state' ]
242 private calcPageSizes () {
243 if (this.screenService.isInMobileView()) {
244 this.pagination.itemsPerPage = 5
248 private setScrollRouteParams () {
250 if (this.angularState) return
252 this.angularState = 42
254 const queryParams = {
255 'a-state': this.angularState,
256 categoryOneOf: this.categoryOneOf
259 let path = this.router.url
260 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
262 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
265 private loadUserVideoLanguagesIfNeeded () {
266 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
270 return this.authService.userInformationLoaded
273 tap(() => this.languageOneOf = this.user.videoLanguages)