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