1 import { debounceTime, first, tap, throttleTime } 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 { ComponentPaginationLight } 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 { ServerConfig } from '@shared/models'
17 import { GlobalIconName } from '@app/shared/images/global-icon.component'
18 import { UserService, User } from '../users'
19 import { LocalStorageService } from '../misc/storage.service'
30 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
31 pagination: ComponentPaginationLight = {
35 sort: VideoSortField = '-publishedAt'
37 categoryOneOf?: number
38 languageOneOf?: string[]
39 defaultSort: VideoSortField = '-publishedAt'
41 syndicationItems: Syndication[] = []
44 useUserVideoLanguagePreferences = false
45 ownerDisplayType: OwnerDisplayType = 'account'
46 displayModerationBlock = false
48 displayVideoActions = true
52 hasDoneFirstQuery = false
55 displayOptions: MiniatureDisplayOptions = {
67 iconName: GlobalIconName
71 onDataSubject = new Subject<any[]>()
73 protected serverConfig: ServerConfig
75 protected abstract notifier: Notifier
76 protected abstract authService: AuthService
77 protected abstract userService: UserService
78 protected abstract route: ActivatedRoute
79 protected abstract serverService: ServerService
80 protected abstract screenService: ScreenService
81 protected abstract storageService: LocalStorageService
82 protected abstract router: Router
83 protected abstract i18n: I18n
84 abstract titlePage: string
86 private resizeSubscription: Subscription
87 private angularState: number
89 private groupedDateLabels: { [id in GroupDate]: string }
90 private groupedDates: { [id: number]: GroupDate } = {}
92 private lastQueryLength: number
94 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
96 abstract generateSyndicationList (): void
99 return this.authService.getUser()
103 this.serverConfig = this.serverService.getTmpConfig()
104 this.serverService.getConfig()
105 .subscribe(config => this.serverConfig = config)
107 this.groupedDateLabels = {
108 [GroupDate.UNKNOWN]: null,
109 [GroupDate.TODAY]: this.i18n('Today'),
110 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
111 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
112 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
113 [GroupDate.OLDER]: this.i18n('Older')
116 // Subscribe to route changes
117 const routeParams = this.route.snapshot.queryParams
118 this.loadRouteParams(routeParams)
120 this.resizeSubscription = fromEvent(window, 'resize')
121 .pipe(debounceTime(500))
122 .subscribe(() => this.calcPageSizes())
126 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
128 if (this.loadOnInit === true) {
129 loadUserObservable.subscribe(() => this.loadMoreVideos())
132 this.storageService.watch([
133 User.KEYS.NSFW_POLICY,
134 User.KEYS.VIDEO_LANGUAGES
135 ]).pipe(throttleTime(200)).subscribe(
137 this.loadUserVideoLanguagesIfNeeded()
138 if (this.hasDoneFirstQuery) this.reloadVideos()
144 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
152 this.disabled = false
155 videoById (index: number, video: Video) {
160 if (this.disabled) return
163 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
165 this.pagination.currentPage += 1
167 this.setScrollRouteParams()
169 this.loadMoreVideos()
172 loadMoreVideos (reset = false) {
173 this.getVideosObservable(this.pagination.currentPage).subscribe(
175 this.hasDoneFirstQuery = true
176 this.lastQueryLength = data.length
178 if (reset) this.videos = []
179 this.videos = this.videos.concat(data)
181 if (this.groupByDate) this.buildGroupedDateLabels()
185 this.onDataSubject.next(data)
189 const message = this.i18n('Cannot load more videos. Try again later.')
191 console.error(message, { error })
192 this.notifier.error(message)
198 this.pagination.currentPage = 1
199 this.loadMoreVideos(true)
202 toggleModerationDisplay () {
203 throw new Error('toggleModerationDisplay is not implemented')
206 removeVideoFromArray (video: Video) {
207 this.videos = this.videos.filter(v => v.id !== video.id)
210 buildGroupedDateLabels () {
211 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
213 for (const video of this.videos) {
214 const publishedDate = video.publishedAt
216 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
217 if (currentGroupedDate === GroupDate.TODAY) continue
219 currentGroupedDate = GroupDate.TODAY
220 this.groupedDates[ video.id ] = currentGroupedDate
224 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
225 if (currentGroupedDate === GroupDate.YESTERDAY) continue
227 currentGroupedDate = GroupDate.YESTERDAY
228 this.groupedDates[ video.id ] = currentGroupedDate
232 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
233 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
235 currentGroupedDate = GroupDate.LAST_WEEK
236 this.groupedDates[ video.id ] = currentGroupedDate
240 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
241 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
243 currentGroupedDate = GroupDate.LAST_MONTH
244 this.groupedDates[ video.id ] = currentGroupedDate
248 if (currentGroupedDate <= GroupDate.OLDER) {
249 if (currentGroupedDate === GroupDate.OLDER) continue
251 currentGroupedDate = GroupDate.OLDER
252 this.groupedDates[ video.id ] = currentGroupedDate
257 getCurrentGroupedDateLabel (video: Video) {
258 if (this.groupByDate === false) return undefined
260 return this.groupedDateLabels[this.groupedDates[video.id]]
263 // On videos hook for children that want to do something
264 protected onMoreVideos () { /* empty */ }
266 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
267 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
268 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
269 this.angularState = routeParams[ 'a-state' ]
272 private calcPageSizes () {
273 if (this.screenService.isInMobileView()) {
274 this.pagination.itemsPerPage = 5
278 private setScrollRouteParams () {
280 if (this.angularState) return
282 this.angularState = 42
284 const queryParams = {
285 'a-state': this.angularState,
286 categoryOneOf: this.categoryOneOf
289 let path = this.router.url
290 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
292 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
295 private loadUserVideoLanguagesIfNeeded () {
296 if (!this.useUserVideoLanguagePreferences) {
300 if (!this.authService.isLoggedIn()) {
301 this.languageOneOf = this.userService.getAnonymousUser().videoLanguages
305 return this.authService.userInformationLoaded
308 tap(() => this.languageOneOf = this.user.videoLanguages)