]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/abstract-video-list.ts
Fix NSFW policy on account/channel videos
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / abstract-video-list.ts
CommitLineData
07f81d9d 1import { fromEvent, Observable, ReplaySubject, Subject, Subscription } from 'rxjs'
67ed6552 2import { debounceTime, switchMap, tap } from 'rxjs/operators'
5bcbcbe3
RK
3import {
4 AfterContentInit,
5 ComponentFactoryResolver,
6 Directive,
7 Injector,
8 OnDestroy,
9 OnInit,
10 Type,
11 ViewChild,
12 ViewContainerRef
13} from '@angular/core'
fd45e8f4 14import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
15import {
16 AuthService,
17 ComponentPaginationLight,
18 LocalStorageService,
19 Notifier,
20 ScreenService,
21 ServerService,
22 User,
23 UserService
24} from '@app/core'
489290b8 25import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
67ed6552 26import { GlobalIconName } from '@app/shared/shared-icons'
4166caab 27import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils/miscs/date'
0aa52e17 28import { ServerConfig, UserRight, VideoFilter, VideoSortField } from '@shared/models'
5c20a455 29import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
67ed6552
C
30import { Syndication, Video } from '../shared-main'
31import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component'
5bcbcbe3 32import { GenericHeaderComponent, VideoListHeaderComponent } from './video-list-header.component'
34c7f429
C
33
34enum GroupDate {
35 UNKNOWN = 0,
36 TODAY = 1,
37 YESTERDAY = 2,
4166caab
C
38 THIS_WEEK = 3,
39 THIS_MONTH = 4,
40 LAST_MONTH = 5,
41 OLDER = 6
34c7f429 42}
0cd4344f 43
583eb04b 44@Directive()
a02b93ce 45// tslint:disable-next-line: directive-class-suffix
5bcbcbe3
RK
46export abstract class AbstractVideoList implements OnInit, OnDestroy, AfterContentInit, DisableForReuseHook {
47 @ViewChild('videoListHeader', { static: true, read: ViewContainerRef }) videoListHeader: ViewContainerRef
48
49 HeaderComponent: Type<GenericHeaderComponent> = VideoListHeaderComponent
50 headerComponentInjector: Injector
51
440d39c5 52 pagination: ComponentPaginationLight = {
fd45e8f4 53 currentPage: 1,
440d39c5 54 itemsPerPage: 25
fd45e8f4 55 }
136cce4d 56 sort: VideoSortField = '-publishedAt'
489290b8 57
5c20a455 58 categoryOneOf?: number[]
3caf77d3 59 languageOneOf?: string[]
5c20a455 60 nsfwPolicy?: NSFWPolicyType
136cce4d 61 defaultSort: VideoSortField = '-publishedAt'
489290b8 62
c199c427 63 syndicationItems: Syndication[] = []
244e76a5 64
f3aaa9a9 65 loadOnInit = true
07f81d9d
C
66 loadUserVideoPreferences = false
67
22a16e36 68 ownerDisplayType: OwnerDisplayType = 'account'
017c3dca 69 displayModerationBlock = false
9b4b15f9 70 titleTooltip: string
3a0fb65c 71 displayVideoActions = true
34c7f429 72 groupByDate = false
fd45e8f4 73
3caf77d3 74 videos: Video[] = []
440d39c5 75 hasDoneFirstQuery = false
489290b8 76 disabled = false
9af61e84 77
abf325b4
C
78 displayOptions: MiniatureDisplayOptions = {
79 date: true,
80 views: true,
81 by: true,
cf78883c 82 avatar: false,
abf325b4
C
83 privacyLabel: true,
84 privacyText: false,
85 state: false,
86 blacklistInfo: false
87 }
88
13adf228 89 actions: {
be27ef3b 90 iconName: GlobalIconName
13adf228 91 label: string
afff310e
RK
92 justIcon?: boolean
93 routerLink?: string
7af5ded4
C
94 href?: string
95 click?: (e: Event) => void
13adf228
RK
96 }[] = []
97
ad453580
C
98 onDataSubject = new Subject<any[]>()
99
5c20a455
C
100 userMiniature: User
101
07f81d9d
C
102 protected onUserLoadedSubject = new ReplaySubject<void>(1)
103
ba430d75
C
104 protected serverConfig: ServerConfig
105
f8b2c1b4 106 protected abstract notifier: Notifier
b2731bff 107 protected abstract authService: AuthService
d3217560 108 protected abstract userService: UserService
b2731bff 109 protected abstract route: ActivatedRoute
489290b8 110 protected abstract serverService: ServerService
bbe0f064 111 protected abstract screenService: ScreenService
d3217560 112 protected abstract storageService: LocalStorageService
489290b8 113 protected abstract router: Router
5bcbcbe3 114 protected abstract cfr: ComponentFactoryResolver
9bf9d2a5 115 abstract titlePage: string
c88593f7 116
9af61e84 117 private resizeSubscription: Subscription
489290b8
C
118 private angularState: number
119
34c7f429
C
120 private groupedDateLabels: { [id in GroupDate]: string }
121 private groupedDates: { [id: number]: GroupDate } = {}
122
440d39c5
C
123 private lastQueryLength: number
124
125 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
9af61e84 126
c199c427 127 abstract generateSyndicationList (): void
fd45e8f4
C
128
129 ngOnInit () {
ba430d75
C
130 this.serverConfig = this.serverService.getTmpConfig()
131 this.serverService.getConfig()
132 .subscribe(config => this.serverConfig = config)
133
34c7f429
C
134 this.groupedDateLabels = {
135 [GroupDate.UNKNOWN]: null,
66357162
C
136 [GroupDate.TODAY]: $localize`Today`,
137 [GroupDate.YESTERDAY]: $localize`Yesterday`,
4166caab
C
138 [GroupDate.THIS_WEEK]: $localize`This week`,
139 [GroupDate.THIS_MONTH]: $localize`This month`,
66357162
C
140 [GroupDate.LAST_MONTH]: $localize`Last month`,
141 [GroupDate.OLDER]: $localize`Older`
34c7f429
C
142 }
143
fd45e8f4 144 // Subscribe to route changes
5b5e333f 145 const routeParams = this.route.snapshot.queryParams
2bbb3412 146 this.loadRouteParams(routeParams)
a2b817d3 147
9af61e84 148 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 149 .pipe(debounceTime(500))
6194c1b4 150 .subscribe(() => this.calcPageSizes())
3290f37c 151
6194c1b4 152 this.calcPageSizes()
3caf77d3 153
5c20a455 154 const loadUserObservable = this.loadUserAndSettings()
07f81d9d
C
155 loadUserObservable.subscribe(() => {
156 this.onUserLoadedSubject.next()
3caf77d3 157
07f81d9d
C
158 if (this.loadOnInit === true) this.loadMoreVideos()
159 })
d3217560 160
5c20a455
C
161 this.userService.listenAnonymousUpdate()
162 .pipe(switchMap(() => this.loadUserAndSettings()))
163 .subscribe(() => {
d3217560 164 if (this.hasDoneFirstQuery) this.reloadVideos()
5c20a455 165 })
cf78883c
C
166
167 // Display avatar in mobile view
168 if (this.screenService.isInMobileView()) {
169 this.displayOptions.avatar = true
170 }
fd45e8f4
C
171 }
172
9af61e84
C
173 ngOnDestroy () {
174 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
175 }
176
5bcbcbe3
RK
177 ngAfterContentInit () {
178 if (this.videoListHeader) {
179 // some components don't use the header: they use their own template, like my-history.component.html
180 this.setHeader.apply(this, [ this.HeaderComponent, this.headerComponentInjector ])
181 }
182 }
183
489290b8
C
184 disableForReuse () {
185 this.disabled = true
89724816
C
186 }
187
489290b8
C
188 enabledForReuse () {
189 this.disabled = false
89724816
C
190 }
191
489290b8
C
192 videoById (index: number, video: Video) {
193 return video.id
2bbb3412
C
194 }
195
196 onNearOfBottom () {
489290b8 197 if (this.disabled) return
2bbb3412 198
440d39c5
C
199 // No more results
200 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
0cd4344f 201
489290b8 202 this.pagination.currentPage += 1
a8ecc6f6 203
489290b8 204 this.setScrollRouteParams()
a8ecc6f6 205
489290b8
C
206 this.loadMoreVideos()
207 }
fd45e8f4 208
9d45db29 209 loadMoreVideos (reset = false) {
93cae479 210 this.getVideosObservable(this.pagination.currentPage).subscribe(
440d39c5
C
211 ({ data }) => {
212 this.hasDoneFirstQuery = true
213 this.lastQueryLength = data.length
214
9d45db29 215 if (reset) this.videos = []
93cae479 216 this.videos = this.videos.concat(data)
693263e9 217
34c7f429
C
218 if (this.groupByDate) this.buildGroupedDateLabels()
219
693263e9 220 this.onMoreVideos()
ad453580
C
221
222 this.onDataSubject.next(data)
fd45e8f4 223 },
017c3dca 224
f27a885a 225 error => {
66357162 226 const message = $localize`Cannot load more videos. Try again later.`
f27a885a
C
227
228 console.error(message, { error })
229 this.notifier.error(message)
230 }
489290b8 231 )
2bbb3412
C
232 }
233
489290b8
C
234 reloadVideos () {
235 this.pagination.currentPage = 1
9d45db29 236 this.loadMoreVideos(true)
fd45e8f4
C
237 }
238
3a0fb65c
C
239 removeVideoFromArray (video: Video) {
240 this.videos = this.videos.filter(v => v.id !== video.id)
241 }
242
34c7f429
C
243 buildGroupedDateLabels () {
244 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
245
4166caab
C
246 const periods = [
247 {
248 value: GroupDate.TODAY,
249 validator: (d: Date) => isToday(d)
250 },
251 {
252 value: GroupDate.YESTERDAY,
253 validator: (d: Date) => isYesterday(d)
254 },
255 {
256 value: GroupDate.THIS_WEEK,
257 validator: (d: Date) => isLastWeek(d)
258 },
259 {
260 value: GroupDate.THIS_MONTH,
261 validator: (d: Date) => isThisMonth(d)
262 },
263 {
264 value: GroupDate.LAST_MONTH,
265 validator: (d: Date) => isLastMonth(d)
266 },
267 {
268 value: GroupDate.OLDER,
269 validator: () => true
34c7f429 270 }
4166caab 271 ]
34c7f429 272
4166caab
C
273 for (const video of this.videos) {
274 const publishedDate = video.publishedAt
34c7f429 275
4166caab
C
276 for (let i = 0; i < periods.length; i++) {
277 const period = periods[i]
4e0c1793 278
4166caab 279 if (currentGroupedDate <= period.value && period.validator(publishedDate)) {
34c7f429 280
4166caab
C
281 if (currentGroupedDate !== period.value) {
282 currentGroupedDate = period.value
283 this.groupedDates[ video.id ] = currentGroupedDate
284 }
4e0c1793 285
4166caab
C
286 break
287 }
34c7f429
C
288 }
289 }
290 }
291
292 getCurrentGroupedDateLabel (video: Video) {
293 if (this.groupByDate === false) return undefined
294
295 return this.groupedDateLabels[this.groupedDates[video.id]]
296 }
297
0aa52e17 298 toggleModerationDisplay () {
5bcbcbe3
RK
299 throw new Error('toggleModerationDisplay ' + $localize`function is not implemented`)
300 }
301
302 setHeader (
303 t: Type<any> = this.HeaderComponent,
304 i: Injector = this.headerComponentInjector
305 ) {
306 const injector = i || Injector.create({
307 providers: [{
308 provide: 'data',
309 useValue: {
310 titlePage: this.titlePage,
311 titleTooltip: this.titleTooltip
312 }
313 }]
314 })
315 const viewContainerRef = this.videoListHeader
316 viewContainerRef.clear()
317
318 const componentFactory = this.cfr.resolveComponentFactory(t)
319 viewContainerRef.createComponent(componentFactory, 0, injector)
0aa52e17
C
320 }
321
693263e9
C
322 // On videos hook for children that want to do something
323 protected onMoreVideos () { /* empty */ }
324
fd45e8f4 325 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
489290b8
C
326 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
327 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
328 this.angularState = routeParams[ 'a-state' ]
0cd4344f 329 }
6194c1b4 330
0aa52e17
C
331 protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) {
332 if (base === 'local') {
333 return existing === 'local'
334 ? 'all-local' as 'all-local'
335 : 'local' as 'local'
336 }
337
338 return existing === 'all'
339 ? null
340 : 'all'
341 }
342
343 protected enableAllFilterIfPossible () {
344 if (!this.authService.isLoggedIn()) return
345
346 this.authService.userInformationLoaded
347 .subscribe(() => {
348 const user = this.authService.getUser()
349 this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS)
350 })
351 }
352
6194c1b4 353 private calcPageSizes () {
489290b8 354 if (this.screenService.isInMobileView()) {
6194c1b4 355 this.pagination.itemsPerPage = 5
6194c1b4 356 }
489290b8 357 }
6194c1b4 358
489290b8
C
359 private setScrollRouteParams () {
360 // Already set
361 if (this.angularState) return
6194c1b4 362
489290b8 363 this.angularState = 42
6194c1b4 364
489290b8
C
365 const queryParams = {
366 'a-state': this.angularState,
367 categoryOneOf: this.categoryOneOf
9af61e84 368 }
6194c1b4 369
fb7b009d 370 let path = this.getUrlWithoutParams()
ba430d75 371 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
489290b8
C
372
373 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
6194c1b4 374 }
3caf77d3 375
5c20a455
C
376 private loadUserAndSettings () {
377 return this.userService.getAnonymousOrLoggedUser()
378 .pipe(tap(user => {
379 this.userMiniature = user
d3217560 380
07f81d9d 381 if (!this.loadUserVideoPreferences) return
3caf77d3 382
5c20a455
C
383 this.languageOneOf = user.videoLanguages
384 this.nsfwPolicy = user.nsfwPolicy
385 }))
3caf77d3 386 }
fb7b009d
RK
387
388 private getUrlWithoutParams () {
40930fda 389 const urlTree = this.router.parseUrl(this.router.url)
fb7b009d 390 urlTree.queryParams = {}
40930fda 391
fb7b009d
RK
392 return urlTree.toString()
393 }
fd45e8f4 394}