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