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