1 import { debounceTime } from 'rxjs/operators'
2 import { OnDestroy, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { fromEvent, Observable, 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 defaultSort: VideoSortField = '-publishedAt'
37 syndicationItems: Syndication[] = []
41 ownerDisplayType: OwnerDisplayType = 'account'
42 displayModerationBlock = false
44 displayVideoActions = true
49 displayOptions: MiniatureDisplayOptions = {
59 protected abstract notifier: Notifier
60 protected abstract authService: AuthService
61 protected abstract route: ActivatedRoute
62 protected abstract serverService: ServerService
63 protected abstract screenService: ScreenService
64 protected abstract router: Router
65 protected abstract i18n: I18n
66 abstract titlePage: string
68 private resizeSubscription: Subscription
69 private angularState: number
71 private groupedDateLabels: { [id in GroupDate]: string }
72 private groupedDates: { [id: number]: GroupDate } = {}
74 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
76 abstract generateSyndicationList (): void
79 return this.authService.getUser()
83 this.groupedDateLabels = {
84 [GroupDate.UNKNOWN]: null,
85 [GroupDate.TODAY]: this.i18n('Today'),
86 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
87 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
88 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
89 [GroupDate.OLDER]: this.i18n('Older')
92 // Subscribe to route changes
93 const routeParams = this.route.snapshot.queryParams
94 this.loadRouteParams(routeParams)
96 this.resizeSubscription = fromEvent(window, 'resize')
97 .pipe(debounceTime(500))
98 .subscribe(() => this.calcPageSizes())
101 if (this.loadOnInit === true) this.loadMoreVideos()
105 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
113 this.disabled = false
116 videoById (index: number, video: Video) {
121 if (this.disabled) return
124 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
126 this.pagination.currentPage += 1
128 this.setScrollRouteParams()
130 this.loadMoreVideos()
134 const observable = this.getVideosObservable(this.pagination.currentPage)
136 observable.subscribe(
137 ({ videos, totalVideos }) => {
138 this.pagination.totalItems = totalVideos
139 this.videos = this.videos.concat(videos)
141 if (this.groupByDate) this.buildGroupedDateLabels()
146 error => this.notifier.error(error.message)
151 this.pagination.currentPage = 1
153 this.loadMoreVideos()
156 toggleModerationDisplay () {
157 throw new Error('toggleModerationDisplay is not implemented')
160 removeVideoFromArray (video: Video) {
161 this.videos = this.videos.filter(v => v.id !== video.id)
164 buildGroupedDateLabels () {
165 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
167 for (const video of this.videos) {
168 const publishedDate = video.publishedAt
170 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
171 if (currentGroupedDate === GroupDate.TODAY) continue
173 currentGroupedDate = GroupDate.TODAY
174 this.groupedDates[ video.id ] = currentGroupedDate
178 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
179 if (currentGroupedDate === GroupDate.YESTERDAY) continue
181 currentGroupedDate = GroupDate.YESTERDAY
182 this.groupedDates[ video.id ] = currentGroupedDate
186 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
187 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
189 currentGroupedDate = GroupDate.LAST_WEEK
190 this.groupedDates[ video.id ] = currentGroupedDate
194 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
195 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
197 currentGroupedDate = GroupDate.LAST_MONTH
198 this.groupedDates[ video.id ] = currentGroupedDate
202 if (currentGroupedDate <= GroupDate.OLDER) {
203 if (currentGroupedDate === GroupDate.OLDER) continue
205 currentGroupedDate = GroupDate.OLDER
206 this.groupedDates[ video.id ] = currentGroupedDate
211 getCurrentGroupedDateLabel (video: Video) {
212 if (this.groupByDate === false) return undefined
214 return this.groupedDateLabels[this.groupedDates[video.id]]
217 // On videos hook for children that want to do something
218 protected onMoreVideos () { /* empty */ }
220 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
221 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
222 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
223 this.angularState = routeParams[ 'a-state' ]
226 private calcPageSizes () {
227 if (this.screenService.isInMobileView()) {
228 this.pagination.itemsPerPage = 5
232 private setScrollRouteParams () {
234 if (this.angularState) return
236 this.angularState = 42
238 const queryParams = {
239 'a-state': this.angularState,
240 categoryOneOf: this.categoryOneOf
243 let path = this.router.url
244 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
246 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })