PeerTube/client/src/app/shared/shared-video-miniature/abstract-video-list.ts

312 lines
8.4 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { fromEvent, Observable, Subject, Subscription } from 'rxjs'
import { debounceTime, switchMap, tap } from 'rxjs/operators'
2020-06-26 08:37:26 +02:00
import { OnDestroy, OnInit, Directive } from '@angular/core'
2017-10-31 11:52:52 +01:00
import { ActivatedRoute, Router } from '@angular/router'
2020-06-23 14:10:17 +02:00
import {
AuthService,
ComponentPaginationLight,
LocalStorageService,
Notifier,
ScreenService,
ServerService,
User,
UserService
} from '@app/core'
2019-03-21 16:49:46 +01:00
import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
2020-06-23 14:10:17 +02:00
import { GlobalIconName } from '@app/shared/shared-icons'
2019-05-31 11:14:38 +02:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2019-06-04 10:28:19 +02:00
import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
2020-06-23 14:10:17 +02:00
import { ServerConfig, VideoSortField } from '@shared/models'
2020-06-16 11:00:35 +02:00
import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
2020-06-23 14:10:17 +02:00
import { Syndication, Video } from '../shared-main'
import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component'
2019-05-31 11:14:38 +02:00
enum GroupDate {
UNKNOWN = 0,
TODAY = 1,
YESTERDAY = 2,
2019-06-04 10:28:19 +02:00
LAST_WEEK = 3,
LAST_MONTH = 4,
2019-05-31 11:14:38 +02:00
OLDER = 5
}
2018-02-13 14:11:05 +01:00
2020-06-26 08:37:26 +02:00
@Directive()
2019-03-21 16:49:46 +01:00
export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
pagination: ComponentPaginationLight = {
2017-10-31 11:52:52 +01:00
currentPage: 1,
itemsPerPage: 25
2017-10-31 11:52:52 +01:00
}
sort: VideoSortField = '-publishedAt'
2019-03-21 16:49:46 +01:00
2020-06-16 11:00:35 +02:00
categoryOneOf?: number[]
languageOneOf?: string[]
2020-06-16 11:00:35 +02:00
nsfwPolicy?: NSFWPolicyType
defaultSort: VideoSortField = '-publishedAt'
2019-03-21 16:49:46 +01:00
2018-10-18 14:35:31 +02:00
syndicationItems: Syndication[] = []
2017-12-05 17:46:33 +01:00
loadOnInit = true
2020-06-16 11:00:35 +02:00
useUserVideoPreferences = false
2018-08-21 16:18:59 +02:00
ownerDisplayType: OwnerDisplayType = 'account'
displayModerationBlock = false
titleTooltip: string
2019-04-05 10:52:27 +02:00
displayVideoActions = true
2019-05-31 11:14:38 +02:00
groupByDate = false
2017-10-31 11:52:52 +01:00
videos: Video[] = []
hasDoneFirstQuery = false
2019-03-21 16:49:46 +01:00
disabled = false
2018-03-19 18:00:31 +01:00
2019-04-08 09:33:37 +02:00
displayOptions: MiniatureDisplayOptions = {
date: true,
views: true,
by: true,
2020-06-11 14:33:33 +02:00
avatar: false,
2019-04-08 09:33:37 +02:00
privacyLabel: true,
privacyText: false,
state: false,
blacklistInfo: false
}
actions: {
routerLink: string
2020-02-10 14:25:38 +01:00
iconName: GlobalIconName
label: string
}[] = []
2019-08-02 14:49:25 +02:00
onDataSubject = new Subject<any[]>()
2020-06-16 11:00:35 +02:00
userMiniature: User
2019-12-18 15:31:54 +01:00
protected serverConfig: ServerConfig
protected abstract notifier: Notifier
2017-12-12 14:41:59 +01:00
protected abstract authService: AuthService
protected abstract userService: UserService
2017-12-12 14:41:59 +01:00
protected abstract route: ActivatedRoute
2019-03-21 16:49:46 +01:00
protected abstract serverService: ServerService
protected abstract screenService: ScreenService
protected abstract storageService: LocalStorageService
2019-03-21 16:49:46 +01:00
protected abstract router: Router
2019-05-31 11:14:38 +02:00
protected abstract i18n: I18n
2017-12-01 14:46:22 +01:00
abstract titlePage: string
2018-01-29 09:30:06 +01:00
2018-03-19 18:00:31 +01:00
private resizeSubscription: Subscription
2019-03-21 16:49:46 +01:00
private angularState: number
2019-05-31 11:14:38 +02:00
private groupedDateLabels: { [id in GroupDate]: string }
private groupedDates: { [id: number]: GroupDate } = {}
private lastQueryLength: number
abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
2018-03-19 18:00:31 +01:00
2018-10-18 14:35:31 +02:00
abstract generateSyndicationList (): void
2017-10-31 11:52:52 +01:00
ngOnInit () {
2019-12-18 15:31:54 +01:00
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
.subscribe(config => this.serverConfig = config)
2019-05-31 11:14:38 +02:00
this.groupedDateLabels = {
[GroupDate.UNKNOWN]: null,
[GroupDate.TODAY]: this.i18n('Today'),
[GroupDate.YESTERDAY]: this.i18n('Yesterday'),
2019-06-04 10:28:19 +02:00
[GroupDate.LAST_WEEK]: this.i18n('Last week'),
[GroupDate.LAST_MONTH]: this.i18n('Last month'),
2019-05-31 11:14:38 +02:00
[GroupDate.OLDER]: this.i18n('Older')
}
2017-10-31 11:52:52 +01:00
// Subscribe to route changes
2018-03-27 17:10:56 +02:00
const routeParams = this.route.snapshot.queryParams
2017-12-01 16:17:32 +01:00
this.loadRouteParams(routeParams)
2017-12-06 09:19:25 +01:00
2018-03-19 18:00:31 +01:00
this.resizeSubscription = fromEvent(window, 'resize')
2018-05-15 11:55:51 +02:00
.pipe(debounceTime(500))
2018-03-19 16:18:41 +01:00
.subscribe(() => this.calcPageSizes())
2018-03-19 16:18:41 +01:00
this.calcPageSizes()
2020-06-16 11:00:35 +02:00
const loadUserObservable = this.loadUserAndSettings()
if (this.loadOnInit === true) {
loadUserObservable.subscribe(() => this.loadMoreVideos())
}
2020-06-16 11:00:35 +02:00
this.userService.listenAnonymousUpdate()
.pipe(switchMap(() => this.loadUserAndSettings()))
.subscribe(() => {
if (this.hasDoneFirstQuery) this.reloadVideos()
2020-06-16 11:00:35 +02:00
})
2020-06-11 14:33:33 +02:00
// Display avatar in mobile view
if (this.screenService.isInMobileView()) {
this.displayOptions.avatar = true
}
2017-10-31 11:52:52 +01:00
}
2018-03-19 18:00:31 +01:00
ngOnDestroy () {
if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
}
2019-03-21 16:49:46 +01:00
disableForReuse () {
this.disabled = true
2018-09-20 14:21:57 +02:00
}
2019-03-21 16:49:46 +01:00
enabledForReuse () {
this.disabled = false
2018-09-20 14:21:57 +02:00
}
2019-03-21 16:49:46 +01:00
videoById (index: number, video: Video) {
return video.id
2017-12-01 16:17:32 +01:00
}
onNearOfBottom () {
2019-03-21 16:49:46 +01:00
if (this.disabled) return
2017-12-01 16:17:32 +01:00
// No more results
if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
2018-02-13 14:11:05 +01:00
2019-03-21 16:49:46 +01:00
this.pagination.currentPage += 1
2018-09-17 17:36:46 +02:00
2019-03-21 16:49:46 +01:00
this.setScrollRouteParams()
2018-09-17 17:36:46 +02:00
2019-03-21 16:49:46 +01:00
this.loadMoreVideos()
}
2017-10-31 11:52:52 +01:00
loadMoreVideos (reset = false) {
2019-07-22 15:40:13 +02:00
this.getVideosObservable(this.pagination.currentPage).subscribe(
({ data }) => {
this.hasDoneFirstQuery = true
this.lastQueryLength = data.length
if (reset) this.videos = []
2019-07-22 15:40:13 +02:00
this.videos = this.videos.concat(data)
2019-04-04 10:44:18 +02:00
2019-05-31 11:14:38 +02:00
if (this.groupByDate) this.buildGroupedDateLabels()
2019-04-04 10:44:18 +02:00
this.onMoreVideos()
2019-08-02 14:49:25 +02:00
this.onDataSubject.next(data)
2017-10-31 11:52:52 +01:00
},
2019-12-27 13:10:50 +01:00
error => {
const message = this.i18n('Cannot load more videos. Try again later.')
console.error(message, { error })
this.notifier.error(message)
}
2019-03-21 16:49:46 +01:00
)
2017-12-01 16:17:32 +01:00
}
2019-03-21 16:49:46 +01:00
reloadVideos () {
this.pagination.currentPage = 1
this.loadMoreVideos(true)
2017-10-31 11:52:52 +01:00
}
2019-03-21 16:49:46 +01:00
toggleModerationDisplay () {
throw new Error('toggleModerationDisplay is not implemented')
2017-10-31 11:52:52 +01:00
}
2019-04-05 10:52:27 +02:00
removeVideoFromArray (video: Video) {
this.videos = this.videos.filter(v => v.id !== video.id)
}
2019-05-31 11:14:38 +02:00
buildGroupedDateLabels () {
let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
for (const video of this.videos) {
const publishedDate = video.publishedAt
2019-06-04 10:15:18 +02:00
if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
if (currentGroupedDate === GroupDate.TODAY) continue
2019-05-31 11:14:38 +02:00
currentGroupedDate = GroupDate.TODAY
this.groupedDates[ video.id ] = currentGroupedDate
continue
}
2019-06-04 10:15:18 +02:00
if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
if (currentGroupedDate === GroupDate.YESTERDAY) continue
2019-05-31 11:14:38 +02:00
currentGroupedDate = GroupDate.YESTERDAY
this.groupedDates[ video.id ] = currentGroupedDate
continue
}
2019-06-04 10:28:19 +02:00
if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
if (currentGroupedDate === GroupDate.LAST_WEEK) continue
2019-06-04 10:15:18 +02:00
2019-06-04 10:28:19 +02:00
currentGroupedDate = GroupDate.LAST_WEEK
2019-05-31 11:14:38 +02:00
this.groupedDates[ video.id ] = currentGroupedDate
continue
}
2019-06-04 10:28:19 +02:00
if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
if (currentGroupedDate === GroupDate.LAST_MONTH) continue
2019-06-04 10:15:18 +02:00
2019-06-04 10:28:19 +02:00
currentGroupedDate = GroupDate.LAST_MONTH
2019-05-31 11:14:38 +02:00
this.groupedDates[ video.id ] = currentGroupedDate
continue
}
2019-06-04 10:15:18 +02:00
if (currentGroupedDate <= GroupDate.OLDER) {
if (currentGroupedDate === GroupDate.OLDER) continue
2019-05-31 11:14:38 +02:00
currentGroupedDate = GroupDate.OLDER
this.groupedDates[ video.id ] = currentGroupedDate
}
}
}
getCurrentGroupedDateLabel (video: Video) {
if (this.groupByDate === false) return undefined
return this.groupedDateLabels[this.groupedDates[video.id]]
}
2019-04-04 10:44:18 +02:00
// On videos hook for children that want to do something
protected onMoreVideos () { /* empty */ }
2017-10-31 11:52:52 +01:00
protected loadRouteParams (routeParams: { [ key: string ]: any }) {
2019-03-21 16:49:46 +01:00
this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
this.categoryOneOf = routeParams[ 'categoryOneOf' ]
this.angularState = routeParams[ 'a-state' ]
2018-02-13 14:11:05 +01:00
}
2018-03-19 16:18:41 +01:00
private calcPageSizes () {
2019-03-21 16:49:46 +01:00
if (this.screenService.isInMobileView()) {
2018-03-19 16:18:41 +01:00
this.pagination.itemsPerPage = 5
}
2019-03-21 16:49:46 +01:00
}
2018-03-19 16:18:41 +01:00
2019-03-21 16:49:46 +01:00
private setScrollRouteParams () {
// Already set
if (this.angularState) return
2018-03-19 16:18:41 +01:00
2019-03-21 16:49:46 +01:00
this.angularState = 42
2018-03-19 16:18:41 +01:00
2019-03-21 16:49:46 +01:00
const queryParams = {
'a-state': this.angularState,
categoryOneOf: this.categoryOneOf
2018-03-19 18:00:31 +01:00
}
2018-03-19 16:18:41 +01:00
2019-03-21 16:49:46 +01:00
let path = this.router.url
2019-12-18 15:31:54 +01:00
if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
2019-03-21 16:49:46 +01:00
this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
2018-03-19 16:18:41 +01:00
}
2020-06-16 11:00:35 +02:00
private loadUserAndSettings () {
return this.userService.getAnonymousOrLoggedUser()
.pipe(tap(user => {
this.userMiniature = user
2020-06-16 11:00:35 +02:00
if (!this.useUserVideoPreferences) return
2020-06-16 11:00:35 +02:00
this.languageOneOf = user.videoLanguages
this.nsfwPolicy = user.nsfwPolicy
}))
}
2017-10-31 11:52:52 +01:00
}