1 import { fromEvent, Observable, Subject, Subscription } from 'rxjs'
2 import { debounceTime, switchMap, tap } from 'rxjs/operators'
3 import { Directive, 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'
34 // tslint:disable-next-line: directive-class-suffix
35 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
36 pagination: ComponentPaginationLight = {
40 sort: VideoSortField = '-publishedAt'
42 categoryOneOf?: number[]
43 languageOneOf?: string[]
44 nsfwPolicy?: NSFWPolicyType
45 defaultSort: VideoSortField = '-publishedAt'
47 syndicationItems: Syndication[] = []
50 useUserVideoPreferences = false
51 ownerDisplayType: OwnerDisplayType = 'account'
52 displayModerationBlock = false
54 displayVideoActions = true
58 hasDoneFirstQuery = false
61 displayOptions: MiniatureDisplayOptions = {
74 iconName: GlobalIconName
78 onDataSubject = new Subject<any[]>()
82 protected serverConfig: ServerConfig
84 protected abstract notifier: Notifier
85 protected abstract authService: AuthService
86 protected abstract userService: UserService
87 protected abstract route: ActivatedRoute
88 protected abstract serverService: ServerService
89 protected abstract screenService: ScreenService
90 protected abstract storageService: LocalStorageService
91 protected abstract router: Router
92 protected abstract i18n: I18n
93 abstract titlePage: string
95 private resizeSubscription: Subscription
96 private angularState: number
98 private groupedDateLabels: { [id in GroupDate]: string }
99 private groupedDates: { [id: number]: GroupDate } = {}
101 private lastQueryLength: number
103 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
105 abstract generateSyndicationList (): void
108 this.serverConfig = this.serverService.getTmpConfig()
109 this.serverService.getConfig()
110 .subscribe(config => this.serverConfig = config)
112 this.groupedDateLabels = {
113 [GroupDate.UNKNOWN]: null,
114 [GroupDate.TODAY]: this.i18n('Today'),
115 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
116 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
117 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
118 [GroupDate.OLDER]: this.i18n('Older')
121 // Subscribe to route changes
122 const routeParams = this.route.snapshot.queryParams
123 this.loadRouteParams(routeParams)
125 this.resizeSubscription = fromEvent(window, 'resize')
126 .pipe(debounceTime(500))
127 .subscribe(() => this.calcPageSizes())
131 const loadUserObservable = this.loadUserAndSettings()
133 if (this.loadOnInit === true) {
134 loadUserObservable.subscribe(() => this.loadMoreVideos())
137 this.userService.listenAnonymousUpdate()
138 .pipe(switchMap(() => this.loadUserAndSettings()))
140 if (this.hasDoneFirstQuery) this.reloadVideos()
143 // Display avatar in mobile view
144 if (this.screenService.isInMobileView()) {
145 this.displayOptions.avatar = true
150 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
158 this.disabled = false
161 videoById (index: number, video: Video) {
166 if (this.disabled) return
169 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
171 this.pagination.currentPage += 1
173 this.setScrollRouteParams()
175 this.loadMoreVideos()
178 loadMoreVideos (reset = false) {
179 this.getVideosObservable(this.pagination.currentPage).subscribe(
181 this.hasDoneFirstQuery = true
182 this.lastQueryLength = data.length
184 if (reset) this.videos = []
185 this.videos = this.videos.concat(data)
187 if (this.groupByDate) this.buildGroupedDateLabels()
191 this.onDataSubject.next(data)
195 const message = this.i18n('Cannot load more videos. Try again later.')
197 console.error(message, { error })
198 this.notifier.error(message)
204 this.pagination.currentPage = 1
205 this.loadMoreVideos(true)
208 toggleModerationDisplay () {
209 throw new Error('toggleModerationDisplay is not implemented')
212 removeVideoFromArray (video: Video) {
213 this.videos = this.videos.filter(v => v.id !== video.id)
216 buildGroupedDateLabels () {
217 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
219 for (const video of this.videos) {
220 const publishedDate = video.publishedAt
222 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
223 if (currentGroupedDate === GroupDate.TODAY) continue
225 currentGroupedDate = GroupDate.TODAY
226 this.groupedDates[ video.id ] = currentGroupedDate
230 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
231 if (currentGroupedDate === GroupDate.YESTERDAY) continue
233 currentGroupedDate = GroupDate.YESTERDAY
234 this.groupedDates[ video.id ] = currentGroupedDate
238 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
239 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
241 currentGroupedDate = GroupDate.LAST_WEEK
242 this.groupedDates[ video.id ] = currentGroupedDate
246 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
247 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
249 currentGroupedDate = GroupDate.LAST_MONTH
250 this.groupedDates[ video.id ] = currentGroupedDate
254 if (currentGroupedDate <= GroupDate.OLDER) {
255 if (currentGroupedDate === GroupDate.OLDER) continue
257 currentGroupedDate = GroupDate.OLDER
258 this.groupedDates[ video.id ] = currentGroupedDate
263 getCurrentGroupedDateLabel (video: Video) {
264 if (this.groupByDate === false) return undefined
266 return this.groupedDateLabels[this.groupedDates[video.id]]
269 // On videos hook for children that want to do something
270 protected onMoreVideos () { /* empty */ }
272 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
273 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
274 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
275 this.angularState = routeParams[ 'a-state' ]
278 private calcPageSizes () {
279 if (this.screenService.isInMobileView()) {
280 this.pagination.itemsPerPage = 5
284 private setScrollRouteParams () {
286 if (this.angularState) return
288 this.angularState = 42
290 const queryParams = {
291 'a-state': this.angularState,
292 categoryOneOf: this.categoryOneOf
295 let path = this.router.url
296 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
298 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
301 private loadUserAndSettings () {
302 return this.userService.getAnonymousOrLoggedUser()
304 this.userMiniature = user
306 if (!this.useUserVideoPreferences) return
308 this.languageOneOf = user.videoLanguages
309 this.nsfwPolicy = user.nsfwPolicy