1 import { fromEvent, Observable, of, Subject, Subscription } from 'rxjs'
2 import { debounceTime, tap, throttleTime, switchMap } from 'rxjs/operators'
3 import { OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { Notifier, ServerService } from '@app/core'
6 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7 import { GlobalIconName } from '@app/shared/images/global-icon.component'
8 import { ScreenService } from '@app/shared/misc/screen.service'
9 import { Syndication } from '@app/shared/video/syndication.model'
10 import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
13 import { ServerConfig } from '@shared/models'
14 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
15 import { AuthService } from '../../core/auth'
16 import { LocalStorageService } from '../misc/storage.service'
17 import { ComponentPaginationLight } from '../rest/component-pagination.model'
18 import { User, UserService } from '../users'
19 import { VideoSortField } from './sort-field.type'
20 import { Video } from './video.model'
31 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
32 pagination: ComponentPaginationLight = {
36 sort: VideoSortField = '-publishedAt'
38 categoryOneOf?: number[]
39 languageOneOf?: string[]
40 nsfwPolicy?: NSFWPolicyType
41 defaultSort: VideoSortField = '-publishedAt'
43 syndicationItems: Syndication[] = []
46 useUserVideoPreferences = false
47 ownerDisplayType: OwnerDisplayType = 'account'
48 displayModerationBlock = false
50 displayVideoActions = true
54 hasDoneFirstQuery = false
57 displayOptions: MiniatureDisplayOptions = {
70 iconName: GlobalIconName
74 onDataSubject = new Subject<any[]>()
78 protected serverConfig: ServerConfig
80 protected abstract notifier: Notifier
81 protected abstract authService: AuthService
82 protected abstract userService: UserService
83 protected abstract route: ActivatedRoute
84 protected abstract serverService: ServerService
85 protected abstract screenService: ScreenService
86 protected abstract storageService: LocalStorageService
87 protected abstract router: Router
88 protected abstract i18n: I18n
89 abstract titlePage: string
91 private resizeSubscription: Subscription
92 private angularState: number
94 private groupedDateLabels: { [id in GroupDate]: string }
95 private groupedDates: { [id: number]: GroupDate } = {}
97 private lastQueryLength: number
99 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
101 abstract generateSyndicationList (): void
104 this.serverConfig = this.serverService.getTmpConfig()
105 this.serverService.getConfig()
106 .subscribe(config => this.serverConfig = config)
108 this.groupedDateLabels = {
109 [GroupDate.UNKNOWN]: null,
110 [GroupDate.TODAY]: this.i18n('Today'),
111 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
112 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
113 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
114 [GroupDate.OLDER]: this.i18n('Older')
117 // Subscribe to route changes
118 const routeParams = this.route.snapshot.queryParams
119 this.loadRouteParams(routeParams)
121 this.resizeSubscription = fromEvent(window, 'resize')
122 .pipe(debounceTime(500))
123 .subscribe(() => this.calcPageSizes())
127 const loadUserObservable = this.loadUserAndSettings()
129 if (this.loadOnInit === true) {
130 loadUserObservable.subscribe(() => this.loadMoreVideos())
133 this.userService.listenAnonymousUpdate()
134 .pipe(switchMap(() => this.loadUserAndSettings()))
136 if (this.hasDoneFirstQuery) this.reloadVideos()
139 // Display avatar in mobile view
140 if (this.screenService.isInMobileView()) {
141 this.displayOptions.avatar = true
146 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
154 this.disabled = false
157 videoById (index: number, video: Video) {
162 if (this.disabled) return
165 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
167 this.pagination.currentPage += 1
169 this.setScrollRouteParams()
171 this.loadMoreVideos()
174 loadMoreVideos (reset = false) {
175 this.getVideosObservable(this.pagination.currentPage).subscribe(
177 this.hasDoneFirstQuery = true
178 this.lastQueryLength = data.length
180 if (reset) this.videos = []
181 this.videos = this.videos.concat(data)
183 if (this.groupByDate) this.buildGroupedDateLabels()
187 this.onDataSubject.next(data)
191 const message = this.i18n('Cannot load more videos. Try again later.')
193 console.error(message, { error })
194 this.notifier.error(message)
200 this.pagination.currentPage = 1
201 this.loadMoreVideos(true)
204 toggleModerationDisplay () {
205 throw new Error('toggleModerationDisplay is not implemented')
208 removeVideoFromArray (video: Video) {
209 this.videos = this.videos.filter(v => v.id !== video.id)
212 buildGroupedDateLabels () {
213 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
215 for (const video of this.videos) {
216 const publishedDate = video.publishedAt
218 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
219 if (currentGroupedDate === GroupDate.TODAY) continue
221 currentGroupedDate = GroupDate.TODAY
222 this.groupedDates[ video.id ] = currentGroupedDate
226 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
227 if (currentGroupedDate === GroupDate.YESTERDAY) continue
229 currentGroupedDate = GroupDate.YESTERDAY
230 this.groupedDates[ video.id ] = currentGroupedDate
234 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
235 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
237 currentGroupedDate = GroupDate.LAST_WEEK
238 this.groupedDates[ video.id ] = currentGroupedDate
242 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
243 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
245 currentGroupedDate = GroupDate.LAST_MONTH
246 this.groupedDates[ video.id ] = currentGroupedDate
250 if (currentGroupedDate <= GroupDate.OLDER) {
251 if (currentGroupedDate === GroupDate.OLDER) continue
253 currentGroupedDate = GroupDate.OLDER
254 this.groupedDates[ video.id ] = currentGroupedDate
259 getCurrentGroupedDateLabel (video: Video) {
260 if (this.groupByDate === false) return undefined
262 return this.groupedDateLabels[this.groupedDates[video.id]]
265 // On videos hook for children that want to do something
266 protected onMoreVideos () { /* empty */ }
268 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
269 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
270 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
271 this.angularState = routeParams[ 'a-state' ]
274 private calcPageSizes () {
275 if (this.screenService.isInMobileView()) {
276 this.pagination.itemsPerPage = 5
280 private setScrollRouteParams () {
282 if (this.angularState) return
284 this.angularState = 42
286 const queryParams = {
287 'a-state': this.angularState,
288 categoryOneOf: this.categoryOneOf
291 let path = this.router.url
292 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
294 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
297 private loadUserAndSettings () {
298 return this.userService.getAnonymousOrLoggedUser()
300 this.userMiniature = user
302 if (!this.useUserVideoPreferences) return
304 this.languageOneOf = user.videoLanguages
305 this.nsfwPolicy = user.nsfwPolicy