1 import { fromEvent, Observable, Subject, Subscription } from 'rxjs'
2 import { debounceTime, switchMap, tap } from 'rxjs/operators'
3 import { OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
7 ComponentPaginationLight,
15 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
16 import { GlobalIconName } from '@app/shared/shared-icons'
17 import { I18n } from '@ngx-translate/i18n-polyfill'
18 import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
19 import { ServerConfig, VideoSortField } from '@shared/models'
20 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
21 import { Syndication, Video } from '../shared-main'
22 import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component'
33 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
34 pagination: ComponentPaginationLight = {
38 sort: VideoSortField = '-publishedAt'
40 categoryOneOf?: number[]
41 languageOneOf?: string[]
42 nsfwPolicy?: NSFWPolicyType
43 defaultSort: VideoSortField = '-publishedAt'
45 syndicationItems: Syndication[] = []
48 useUserVideoPreferences = false
49 ownerDisplayType: OwnerDisplayType = 'account'
50 displayModerationBlock = false
52 displayVideoActions = true
56 hasDoneFirstQuery = false
59 displayOptions: MiniatureDisplayOptions = {
72 iconName: GlobalIconName
76 onDataSubject = new Subject<any[]>()
80 protected serverConfig: ServerConfig
82 protected abstract notifier: Notifier
83 protected abstract authService: AuthService
84 protected abstract userService: UserService
85 protected abstract route: ActivatedRoute
86 protected abstract serverService: ServerService
87 protected abstract screenService: ScreenService
88 protected abstract storageService: LocalStorageService
89 protected abstract router: Router
90 protected abstract i18n: I18n
91 abstract titlePage: string
93 private resizeSubscription: Subscription
94 private angularState: number
96 private groupedDateLabels: { [id in GroupDate]: string }
97 private groupedDates: { [id: number]: GroupDate } = {}
99 private lastQueryLength: number
101 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
103 abstract generateSyndicationList (): void
106 this.serverConfig = this.serverService.getTmpConfig()
107 this.serverService.getConfig()
108 .subscribe(config => this.serverConfig = config)
110 this.groupedDateLabels = {
111 [GroupDate.UNKNOWN]: null,
112 [GroupDate.TODAY]: this.i18n('Today'),
113 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
114 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
115 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
116 [GroupDate.OLDER]: this.i18n('Older')
119 // Subscribe to route changes
120 const routeParams = this.route.snapshot.queryParams
121 this.loadRouteParams(routeParams)
123 this.resizeSubscription = fromEvent(window, 'resize')
124 .pipe(debounceTime(500))
125 .subscribe(() => this.calcPageSizes())
129 const loadUserObservable = this.loadUserAndSettings()
131 if (this.loadOnInit === true) {
132 loadUserObservable.subscribe(() => this.loadMoreVideos())
135 this.userService.listenAnonymousUpdate()
136 .pipe(switchMap(() => this.loadUserAndSettings()))
138 if (this.hasDoneFirstQuery) this.reloadVideos()
141 // Display avatar in mobile view
142 if (this.screenService.isInMobileView()) {
143 this.displayOptions.avatar = true
148 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
156 this.disabled = false
159 videoById (index: number, video: Video) {
164 if (this.disabled) return
167 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
169 this.pagination.currentPage += 1
171 this.setScrollRouteParams()
173 this.loadMoreVideos()
176 loadMoreVideos (reset = false) {
177 this.getVideosObservable(this.pagination.currentPage).subscribe(
179 this.hasDoneFirstQuery = true
180 this.lastQueryLength = data.length
182 if (reset) this.videos = []
183 this.videos = this.videos.concat(data)
185 if (this.groupByDate) this.buildGroupedDateLabels()
189 this.onDataSubject.next(data)
193 const message = this.i18n('Cannot load more videos. Try again later.')
195 console.error(message, { error })
196 this.notifier.error(message)
202 this.pagination.currentPage = 1
203 this.loadMoreVideos(true)
206 toggleModerationDisplay () {
207 throw new Error('toggleModerationDisplay is not implemented')
210 removeVideoFromArray (video: Video) {
211 this.videos = this.videos.filter(v => v.id !== video.id)
214 buildGroupedDateLabels () {
215 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
217 for (const video of this.videos) {
218 const publishedDate = video.publishedAt
220 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
221 if (currentGroupedDate === GroupDate.TODAY) continue
223 currentGroupedDate = GroupDate.TODAY
224 this.groupedDates[ video.id ] = currentGroupedDate
228 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
229 if (currentGroupedDate === GroupDate.YESTERDAY) continue
231 currentGroupedDate = GroupDate.YESTERDAY
232 this.groupedDates[ video.id ] = currentGroupedDate
236 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
237 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
239 currentGroupedDate = GroupDate.LAST_WEEK
240 this.groupedDates[ video.id ] = currentGroupedDate
244 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
245 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
247 currentGroupedDate = GroupDate.LAST_MONTH
248 this.groupedDates[ video.id ] = currentGroupedDate
252 if (currentGroupedDate <= GroupDate.OLDER) {
253 if (currentGroupedDate === GroupDate.OLDER) continue
255 currentGroupedDate = GroupDate.OLDER
256 this.groupedDates[ video.id ] = currentGroupedDate
261 getCurrentGroupedDateLabel (video: Video) {
262 if (this.groupByDate === false) return undefined
264 return this.groupedDateLabels[this.groupedDates[video.id]]
267 // On videos hook for children that want to do something
268 protected onMoreVideos () { /* empty */ }
270 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
271 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
272 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
273 this.angularState = routeParams[ 'a-state' ]
276 private calcPageSizes () {
277 if (this.screenService.isInMobileView()) {
278 this.pagination.itemsPerPage = 5
282 private setScrollRouteParams () {
284 if (this.angularState) return
286 this.angularState = 42
288 const queryParams = {
289 'a-state': this.angularState,
290 categoryOneOf: this.categoryOneOf
293 let path = this.router.url
294 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
296 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
299 private loadUserAndSettings () {
300 return this.userService.getAnonymousOrLoggedUser()
302 this.userMiniature = user
304 if (!this.useUserVideoPreferences) return
306 this.languageOneOf = user.videoLanguages
307 this.nsfwPolicy = user.nsfwPolicy