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