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