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'
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 protected abstract notifier: Notifier
63 protected abstract authService: AuthService
64 protected abstract route: ActivatedRoute
65 protected abstract serverService: ServerService
66 protected abstract screenService: ScreenService
67 protected abstract router: Router
68 protected abstract i18n: I18n
69 abstract titlePage: string
71 private resizeSubscription: Subscription
72 private angularState: number
74 private groupedDateLabels: { [id in GroupDate]: string }
75 private groupedDates: { [id: number]: GroupDate } = {}
77 abstract getVideosObservable (page: number): Observable<ResultList<Video>>
79 abstract generateSyndicationList (): void
82 return this.authService.getUser()
86 this.groupedDateLabels = {
87 [GroupDate.UNKNOWN]: null,
88 [GroupDate.TODAY]: this.i18n('Today'),
89 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
90 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
91 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
92 [GroupDate.OLDER]: this.i18n('Older')
95 // Subscribe to route changes
96 const routeParams = this.route.snapshot.queryParams
97 this.loadRouteParams(routeParams)
99 this.resizeSubscription = fromEvent(window, 'resize')
100 .pipe(debounceTime(500))
101 .subscribe(() => this.calcPageSizes())
105 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
107 if (this.loadOnInit === true) {
108 loadUserObservable.subscribe(() => this.loadMoreVideos())
113 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
121 this.disabled = false
124 videoById (index: number, video: Video) {
129 if (this.disabled) return
132 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
134 this.pagination.currentPage += 1
136 this.setScrollRouteParams()
138 this.loadMoreVideos()
142 this.getVideosObservable(this.pagination.currentPage).subscribe(
143 ({ data, total }) => {
144 this.pagination.totalItems = total
145 this.videos = this.videos.concat(data)
147 if (this.groupByDate) this.buildGroupedDateLabels()
152 error => this.notifier.error(error.message)
157 this.pagination.currentPage = 1
159 this.loadMoreVideos()
162 toggleModerationDisplay () {
163 throw new Error('toggleModerationDisplay is not implemented')
166 removeVideoFromArray (video: Video) {
167 this.videos = this.videos.filter(v => v.id !== video.id)
170 buildGroupedDateLabels () {
171 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
173 for (const video of this.videos) {
174 const publishedDate = video.publishedAt
176 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
177 if (currentGroupedDate === GroupDate.TODAY) continue
179 currentGroupedDate = GroupDate.TODAY
180 this.groupedDates[ video.id ] = currentGroupedDate
184 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
185 if (currentGroupedDate === GroupDate.YESTERDAY) continue
187 currentGroupedDate = GroupDate.YESTERDAY
188 this.groupedDates[ video.id ] = currentGroupedDate
192 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
193 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
195 currentGroupedDate = GroupDate.LAST_WEEK
196 this.groupedDates[ video.id ] = currentGroupedDate
200 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
201 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
203 currentGroupedDate = GroupDate.LAST_MONTH
204 this.groupedDates[ video.id ] = currentGroupedDate
208 if (currentGroupedDate <= GroupDate.OLDER) {
209 if (currentGroupedDate === GroupDate.OLDER) continue
211 currentGroupedDate = GroupDate.OLDER
212 this.groupedDates[ video.id ] = currentGroupedDate
217 getCurrentGroupedDateLabel (video: Video) {
218 if (this.groupByDate === false) return undefined
220 return this.groupedDateLabels[this.groupedDates[video.id]]
223 // On videos hook for children that want to do something
224 protected onMoreVideos () { /* empty */ }
226 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
227 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
228 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
229 this.angularState = routeParams[ 'a-state' ]
232 private calcPageSizes () {
233 if (this.screenService.isInMobileView()) {
234 this.pagination.itemsPerPage = 5
238 private setScrollRouteParams () {
240 if (this.angularState) return
242 this.angularState = 42
244 const queryParams = {
245 'a-state': this.angularState,
246 categoryOneOf: this.categoryOneOf
249 let path = this.router.url
250 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
252 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
255 private loadUserVideoLanguagesIfNeeded () {
256 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
260 return this.authService.userInformationLoaded
263 tap(() => this.languageOneOf = this.user.videoLanguages)