]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
Fix npm run dev script
[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'
4635f59d 6import { ComponentPagination } 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'
ba430d75 16import { ResultList, 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 {
4635f59d 28 pagination: ComponentPagination = {
fd45e8f4 29 currentPage: 1,
489290b8 30 itemsPerPage: 25,
fd45e8f4
C
31 totalItems: null
32 }
136cce4d 33 sort: VideoSortField = '-publishedAt'
489290b8 34
d59cba29 35 categoryOneOf?: number
3caf77d3 36 languageOneOf?: string[]
136cce4d 37 defaultSort: VideoSortField = '-publishedAt'
489290b8 38
c199c427 39 syndicationItems: Syndication[] = []
244e76a5 40
f3aaa9a9 41 loadOnInit = true
3caf77d3 42 useUserVideoLanguagePreferences = false
22a16e36 43 ownerDisplayType: OwnerDisplayType = 'account'
017c3dca 44 displayModerationBlock = false
9b4b15f9 45 titleTooltip: string
3a0fb65c 46 displayVideoActions = true
34c7f429 47 groupByDate = false
fd45e8f4 48
3caf77d3 49 videos: Video[] = []
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
93cae479 87 abstract getVideosObservable (page: number): Observable<ResultList<Video>>
9af61e84 88
c199c427 89 abstract generateSyndicationList (): void
fd45e8f4 90
b2731bff
C
91 get user () {
92 return this.authService.getUser()
93 }
94
fd45e8f4 95 ngOnInit () {
ba430d75
C
96 this.serverConfig = this.serverService.getTmpConfig()
97 this.serverService.getConfig()
98 .subscribe(config => this.serverConfig = config)
99
34c7f429
C
100 this.groupedDateLabels = {
101 [GroupDate.UNKNOWN]: null,
102 [GroupDate.TODAY]: this.i18n('Today'),
103 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
93aa8552
C
104 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
105 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
34c7f429
C
106 [GroupDate.OLDER]: this.i18n('Older')
107 }
108
fd45e8f4 109 // Subscribe to route changes
5b5e333f 110 const routeParams = this.route.snapshot.queryParams
2bbb3412 111 this.loadRouteParams(routeParams)
a2b817d3 112
9af61e84 113 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 114 .pipe(debounceTime(500))
6194c1b4 115 .subscribe(() => this.calcPageSizes())
3290f37c 116
6194c1b4 117 this.calcPageSizes()
3caf77d3
C
118
119 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
120
121 if (this.loadOnInit === true) {
122 loadUserObservable.subscribe(() => this.loadMoreVideos())
123 }
fd45e8f4
C
124 }
125
9af61e84
C
126 ngOnDestroy () {
127 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
128 }
129
489290b8
C
130 disableForReuse () {
131 this.disabled = true
89724816
C
132 }
133
489290b8
C
134 enabledForReuse () {
135 this.disabled = false
89724816
C
136 }
137
489290b8
C
138 videoById (index: number, video: Video) {
139 return video.id
2bbb3412
C
140 }
141
142 onNearOfBottom () {
489290b8 143 if (this.disabled) return
2bbb3412 144
489290b8
C
145 // Last page
146 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
0cd4344f 147
489290b8 148 this.pagination.currentPage += 1
a8ecc6f6 149
489290b8 150 this.setScrollRouteParams()
a8ecc6f6 151
489290b8
C
152 this.loadMoreVideos()
153 }
fd45e8f4 154
9d45db29 155 loadMoreVideos (reset = false) {
93cae479
C
156 this.getVideosObservable(this.pagination.currentPage).subscribe(
157 ({ data, total }) => {
158 this.pagination.totalItems = total
9d45db29 159 if (reset) this.videos = []
93cae479 160 this.videos = this.videos.concat(data)
693263e9 161
34c7f429
C
162 if (this.groupByDate) this.buildGroupedDateLabels()
163
693263e9 164 this.onMoreVideos()
ad453580
C
165
166 this.onDataSubject.next(data)
fd45e8f4 167 },
017c3dca 168
f27a885a
C
169 error => {
170 const message = this.i18n('Cannot load more videos. Try again later.')
171
172 console.error(message, { error })
173 this.notifier.error(message)
174 }
489290b8 175 )
2bbb3412
C
176 }
177
489290b8
C
178 reloadVideos () {
179 this.pagination.currentPage = 1
9d45db29 180 this.loadMoreVideos(true)
fd45e8f4
C
181 }
182
489290b8
C
183 toggleModerationDisplay () {
184 throw new Error('toggleModerationDisplay is not implemented')
fd45e8f4
C
185 }
186
3a0fb65c
C
187 removeVideoFromArray (video: Video) {
188 this.videos = this.videos.filter(v => v.id !== video.id)
189 }
190
34c7f429
C
191 buildGroupedDateLabels () {
192 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
193
194 for (const video of this.videos) {
195 const publishedDate = video.publishedAt
196
4e0c1793
C
197 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
198 if (currentGroupedDate === GroupDate.TODAY) continue
199
34c7f429
C
200 currentGroupedDate = GroupDate.TODAY
201 this.groupedDates[ video.id ] = currentGroupedDate
202 continue
203 }
204
4e0c1793
C
205 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
206 if (currentGroupedDate === GroupDate.YESTERDAY) continue
207
34c7f429
C
208 currentGroupedDate = GroupDate.YESTERDAY
209 this.groupedDates[ video.id ] = currentGroupedDate
210 continue
211 }
212
93aa8552
C
213 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
214 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
4e0c1793 215
93aa8552 216 currentGroupedDate = GroupDate.LAST_WEEK
34c7f429
C
217 this.groupedDates[ video.id ] = currentGroupedDate
218 continue
219 }
220
93aa8552
C
221 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
222 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
4e0c1793 223
93aa8552 224 currentGroupedDate = GroupDate.LAST_MONTH
34c7f429
C
225 this.groupedDates[ video.id ] = currentGroupedDate
226 continue
227 }
228
4e0c1793
C
229 if (currentGroupedDate <= GroupDate.OLDER) {
230 if (currentGroupedDate === GroupDate.OLDER) continue
231
34c7f429
C
232 currentGroupedDate = GroupDate.OLDER
233 this.groupedDates[ video.id ] = currentGroupedDate
234 }
235 }
236 }
237
238 getCurrentGroupedDateLabel (video: Video) {
239 if (this.groupByDate === false) return undefined
240
241 return this.groupedDateLabels[this.groupedDates[video.id]]
242 }
243
693263e9
C
244 // On videos hook for children that want to do something
245 protected onMoreVideos () { /* empty */ }
246
fd45e8f4 247 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
489290b8
C
248 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
249 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
250 this.angularState = routeParams[ 'a-state' ]
0cd4344f 251 }
6194c1b4
C
252
253 private calcPageSizes () {
489290b8 254 if (this.screenService.isInMobileView()) {
6194c1b4 255 this.pagination.itemsPerPage = 5
6194c1b4 256 }
489290b8 257 }
6194c1b4 258
489290b8
C
259 private setScrollRouteParams () {
260 // Already set
261 if (this.angularState) return
6194c1b4 262
489290b8 263 this.angularState = 42
6194c1b4 264
489290b8
C
265 const queryParams = {
266 'a-state': this.angularState,
267 categoryOneOf: this.categoryOneOf
9af61e84 268 }
6194c1b4 269
489290b8 270 let path = this.router.url
ba430d75 271 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
489290b8
C
272
273 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
6194c1b4 274 }
3caf77d3
C
275
276 private loadUserVideoLanguagesIfNeeded () {
277 if (!this.authService.isLoggedIn() || !this.useUserVideoLanguagePreferences) {
278 return of(true)
279 }
280
281 return this.authService.userInformationLoaded
282 .pipe(
283 first(),
284 tap(() => this.languageOneOf = this.user.videoLanguages)
285 )
286 }
fd45e8f4 287}