]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { debounceTime, first, tap } from 'rxjs/operators'
2import { OnDestroy, OnInit } from '@angular/core'
3import { ActivatedRoute, Router } from '@angular/router'
4import { fromEvent, Observable, of, Subject, Subscription } from 'rxjs'
5import { AuthService } from '../../core/auth'
6import { ComponentPaginationLight } from '../rest/component-pagination.model'
7import { VideoSortField } from './sort-field.type'
8import { Video } from './video.model'
9import { ScreenService } from '@app/shared/misc/screen.service'
10import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component'
11import { Syndication } from '@app/shared/video/syndication.model'
12import { Notifier, ServerService } from '@app/core'
13import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
14import { I18n } from '@ngx-translate/i18n-polyfill'
15import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
16import { ServerConfig } from '@shared/models'
17
18enum GroupDate {
19 UNKNOWN = 0,
20 TODAY = 1,
21 YESTERDAY = 2,
22 LAST_WEEK = 3,
23 LAST_MONTH = 4,
24 OLDER = 5
25}
26
27export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
28 pagination: ComponentPaginationLight = {
29 currentPage: 1,
30 itemsPerPage: 25
31 }
32 sort: VideoSortField = '-publishedAt'
33
34 categoryOneOf?: number
35 languageOneOf?: string[]
36 defaultSort: VideoSortField = '-publishedAt'
37
38 syndicationItems: Syndication[] = []
39
40 loadOnInit = true
41 useUserVideoLanguagePreferences = false
42 ownerDisplayType: OwnerDisplayType = 'account'
43 displayModerationBlock = false
44 titleTooltip: string
45 displayVideoActions = true
46 groupByDate = false
47
48 videos: Video[] = []
49 hasDoneFirstQuery = false
50 disabled = false
51
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
62 actions: {
63 routerLink: string
64 iconName: string
65 label: string
66 }[] = []
67
68 onDataSubject = new Subject<any[]>()
69
70 protected serverConfig: ServerConfig
71
72 protected abstract notifier: Notifier
73 protected abstract authService: AuthService
74 protected abstract route: ActivatedRoute
75 protected abstract serverService: ServerService
76 protected abstract screenService: ScreenService
77 protected abstract router: Router
78 protected abstract i18n: I18n
79 abstract titlePage: string
80
81 private resizeSubscription: Subscription
82 private angularState: number
83
84 private groupedDateLabels: { [id in GroupDate]: string }
85 private groupedDates: { [id: number]: GroupDate } = {}
86
87 private lastQueryLength: number
88
89 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
90
91 abstract generateSyndicationList (): void
92
93 get user () {
94 return this.authService.getUser()
95 }
96
97 ngOnInit () {
98 this.serverConfig = this.serverService.getTmpConfig()
99 this.serverService.getConfig()
100 .subscribe(config => this.serverConfig = config)
101
102 this.groupedDateLabels = {
103 [GroupDate.UNKNOWN]: null,
104 [GroupDate.TODAY]: this.i18n('Today'),
105 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
106 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
107 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
108 [GroupDate.OLDER]: this.i18n('Older')
109 }
110
111 // Subscribe to route changes
112 const routeParams = this.route.snapshot.queryParams
113 this.loadRouteParams(routeParams)
114
115 this.resizeSubscription = fromEvent(window, 'resize')
116 .pipe(debounceTime(500))
117 .subscribe(() => this.calcPageSizes())
118
119 this.calcPageSizes()
120
121 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
122
123 if (this.loadOnInit === true) {
124 loadUserObservable.subscribe(() => this.loadMoreVideos())
125 }
126 }
127
128 ngOnDestroy () {
129 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
130 }
131
132 disableForReuse () {
133 this.disabled = true
134 }
135
136 enabledForReuse () {
137 this.disabled = false
138 }
139
140 videoById (index: number, video: Video) {
141 return video.id
142 }
143
144 onNearOfBottom () {
145 if (this.disabled) return
146
147 // No more results
148 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
149
150 this.pagination.currentPage += 1
151
152 this.setScrollRouteParams()
153
154 this.loadMoreVideos()
155 }
156
157 loadMoreVideos (reset = false) {
158 this.getVideosObservable(this.pagination.currentPage).subscribe(
159 ({ data }) => {
160 this.hasDoneFirstQuery = true
161 this.lastQueryLength = data.length
162
163 if (reset) this.videos = []
164 this.videos = this.videos.concat(data)
165
166 if (this.groupByDate) this.buildGroupedDateLabels()
167
168 this.onMoreVideos()
169
170 this.onDataSubject.next(data)
171 },
172
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 }
179 )
180 }
181
182 reloadVideos () {
183 this.pagination.currentPage = 1
184 this.loadMoreVideos(true)
185 }
186
187 toggleModerationDisplay () {
188 throw new Error('toggleModerationDisplay is not implemented')
189 }
190
191 removeVideoFromArray (video: Video) {
192 this.videos = this.videos.filter(v => v.id !== video.id)
193 }
194
195 buildGroupedDateLabels () {
196 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
197
198 for (const video of this.videos) {
199 const publishedDate = video.publishedAt
200
201 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
202 if (currentGroupedDate === GroupDate.TODAY) continue
203
204 currentGroupedDate = GroupDate.TODAY
205 this.groupedDates[ video.id ] = currentGroupedDate
206 continue
207 }
208
209 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
210 if (currentGroupedDate === GroupDate.YESTERDAY) continue
211
212 currentGroupedDate = GroupDate.YESTERDAY
213 this.groupedDates[ video.id ] = currentGroupedDate
214 continue
215 }
216
217 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
218 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
219
220 currentGroupedDate = GroupDate.LAST_WEEK
221 this.groupedDates[ video.id ] = currentGroupedDate
222 continue
223 }
224
225 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
226 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
227
228 currentGroupedDate = GroupDate.LAST_MONTH
229 this.groupedDates[ video.id ] = currentGroupedDate
230 continue
231 }
232
233 if (currentGroupedDate <= GroupDate.OLDER) {
234 if (currentGroupedDate === GroupDate.OLDER) continue
235
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
248 // On videos hook for children that want to do something
249 protected onMoreVideos () { /* empty */ }
250
251 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
252 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
253 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
254 this.angularState = routeParams[ 'a-state' ]
255 }
256
257 private calcPageSizes () {
258 if (this.screenService.isInMobileView()) {
259 this.pagination.itemsPerPage = 5
260 }
261 }
262
263 private setScrollRouteParams () {
264 // Already set
265 if (this.angularState) return
266
267 this.angularState = 42
268
269 const queryParams = {
270 'a-state': this.angularState,
271 categoryOneOf: this.categoryOneOf
272 }
273
274 let path = this.router.url
275 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
276
277 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
278 }
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 }
291}