1 import { fromEvent, Observable, Subject, Subscription } from 'rxjs'
2 import { debounceTime, switchMap, tap } from 'rxjs/operators'
3 import { OnDestroy, OnInit, Directive } 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 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
35 pagination: ComponentPaginationLight = {
39 sort: VideoSortField = '-publishedAt'
41 categoryOneOf?: number[]
42 languageOneOf?: string[]
43 nsfwPolicy?: NSFWPolicyType
44 defaultSort: VideoSortField = '-publishedAt'
46 syndicationItems: Syndication[] = []
49 useUserVideoPreferences = false
50 ownerDisplayType: OwnerDisplayType = 'account'
51 displayModerationBlock = false
53 displayVideoActions = true
57 hasDoneFirstQuery = false
60 displayOptions: MiniatureDisplayOptions = {
73 iconName: GlobalIconName
77 onDataSubject = new Subject<any[]>()
81 protected serverConfig: ServerConfig
83 protected abstract notifier: Notifier
84 protected abstract authService: AuthService
85 protected abstract userService: UserService
86 protected abstract route: ActivatedRoute
87 protected abstract serverService: ServerService
88 protected abstract screenService: ScreenService
89 protected abstract storageService: LocalStorageService
90 protected abstract router: Router
91 protected abstract i18n: I18n
92 abstract titlePage: string
94 private resizeSubscription: Subscription
95 private angularState: number
97 private groupedDateLabels: { [id in GroupDate]: string }
98 private groupedDates: { [id: number]: GroupDate } = {}
100 private lastQueryLength: number
102 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
104 abstract generateSyndicationList (): void
107 this.serverConfig = this.serverService.getTmpConfig()
108 this.serverService.getConfig()
109 .subscribe(config => this.serverConfig = config)
111 this.groupedDateLabels = {
112 [GroupDate.UNKNOWN]: null,
113 [GroupDate.TODAY]: this.i18n('Today'),
114 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
115 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
116 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
117 [GroupDate.OLDER]: this.i18n('Older')
120 // Subscribe to route changes
121 const routeParams = this.route.snapshot.queryParams
122 this.loadRouteParams(routeParams)
124 this.resizeSubscription = fromEvent(window, 'resize')
125 .pipe(debounceTime(500))
126 .subscribe(() => this.calcPageSizes())
130 const loadUserObservable = this.loadUserAndSettings()
132 if (this.loadOnInit === true) {
133 loadUserObservable.subscribe(() => this.loadMoreVideos())
136 this.userService.listenAnonymousUpdate()
137 .pipe(switchMap(() => this.loadUserAndSettings()))
139 if (this.hasDoneFirstQuery) this.reloadVideos()
142 // Display avatar in mobile view
143 if (this.screenService.isInMobileView()) {
144 this.displayOptions.avatar = true
149 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
157 this.disabled = false
160 videoById (index: number, video: Video) {
165 if (this.disabled) return
168 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
170 this.pagination.currentPage += 1
172 this.setScrollRouteParams()
174 this.loadMoreVideos()
177 loadMoreVideos (reset = false) {
178 this.getVideosObservable(this.pagination.currentPage).subscribe(
180 this.hasDoneFirstQuery = true
181 this.lastQueryLength = data.length
183 if (reset) this.videos = []
184 this.videos = this.videos.concat(data)
186 if (this.groupByDate) this.buildGroupedDateLabels()
190 this.onDataSubject.next(data)
194 const message = this.i18n('Cannot load more videos. Try again later.')
196 console.error(message, { error })
197 this.notifier.error(message)
203 this.pagination.currentPage = 1
204 this.loadMoreVideos(true)
207 toggleModerationDisplay () {
208 throw new Error('toggleModerationDisplay is not implemented')
211 removeVideoFromArray (video: Video) {
212 this.videos = this.videos.filter(v => v.id !== video.id)
215 buildGroupedDateLabels () {
216 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
218 for (const video of this.videos) {
219 const publishedDate = video.publishedAt
221 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
222 if (currentGroupedDate === GroupDate.TODAY) continue
224 currentGroupedDate = GroupDate.TODAY
225 this.groupedDates[ video.id ] = currentGroupedDate
229 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
230 if (currentGroupedDate === GroupDate.YESTERDAY) continue
232 currentGroupedDate = GroupDate.YESTERDAY
233 this.groupedDates[ video.id ] = currentGroupedDate
237 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
238 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
240 currentGroupedDate = GroupDate.LAST_WEEK
241 this.groupedDates[ video.id ] = currentGroupedDate
245 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
246 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
248 currentGroupedDate = GroupDate.LAST_MONTH
249 this.groupedDates[ video.id ] = currentGroupedDate
253 if (currentGroupedDate <= GroupDate.OLDER) {
254 if (currentGroupedDate === GroupDate.OLDER) continue
256 currentGroupedDate = GroupDate.OLDER
257 this.groupedDates[ video.id ] = currentGroupedDate
262 getCurrentGroupedDateLabel (video: Video) {
263 if (this.groupByDate === false) return undefined
265 return this.groupedDateLabels[this.groupedDates[video.id]]
268 // On videos hook for children that want to do something
269 protected onMoreVideos () { /* empty */ }
271 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
272 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
273 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
274 this.angularState = routeParams[ 'a-state' ]
277 private calcPageSizes () {
278 if (this.screenService.isInMobileView()) {
279 this.pagination.itemsPerPage = 5
283 private setScrollRouteParams () {
285 if (this.angularState) return
287 this.angularState = 42
289 const queryParams = {
290 'a-state': this.angularState,
291 categoryOneOf: this.categoryOneOf
294 let path = this.router.url
295 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
297 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
300 private loadUserAndSettings () {
301 return this.userService.getAnonymousOrLoggedUser()
303 this.userMiniature = user
305 if (!this.useUserVideoPreferences) return
307 this.languageOneOf = user.videoLanguages
308 this.nsfwPolicy = user.nsfwPolicy