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