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