]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
replace title with video name in moderation listings
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
CommitLineData
d3217560 1import { debounceTime, first, tap, throttleTime } 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'
440d39c5 6import { ComponentPaginationLight } 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'
440d39c5 16import { ServerConfig } from '@shared/models'
be27ef3b 17import { GlobalIconName } from '@app/shared/images/global-icon.component'
d3217560
RK
18import { UserService, User } from '../users'
19import { LocalStorageService } from '../misc/storage.service'
34c7f429
C
20
21enum GroupDate {
22 UNKNOWN = 0,
23 TODAY = 1,
24 YESTERDAY = 2,
93aa8552
C
25 LAST_WEEK = 3,
26 LAST_MONTH = 4,
34c7f429
C
27 OLDER = 5
28}
0cd4344f 29
489290b8 30export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
440d39c5 31 pagination: ComponentPaginationLight = {
fd45e8f4 32 currentPage: 1,
440d39c5 33 itemsPerPage: 25
fd45e8f4 34 }
136cce4d 35 sort: VideoSortField = '-publishedAt'
489290b8 36
d59cba29 37 categoryOneOf?: number
3caf77d3 38 languageOneOf?: string[]
136cce4d 39 defaultSort: VideoSortField = '-publishedAt'
489290b8 40
c199c427 41 syndicationItems: Syndication[] = []
244e76a5 42
f3aaa9a9 43 loadOnInit = true
3caf77d3 44 useUserVideoLanguagePreferences = false
22a16e36 45 ownerDisplayType: OwnerDisplayType = 'account'
017c3dca 46 displayModerationBlock = false
9b4b15f9 47 titleTooltip: string
3a0fb65c 48 displayVideoActions = true
34c7f429 49 groupByDate = false
fd45e8f4 50
3caf77d3 51 videos: Video[] = []
440d39c5 52 hasDoneFirstQuery = false
489290b8 53 disabled = false
9af61e84 54
abf325b4
C
55 displayOptions: MiniatureDisplayOptions = {
56 date: true,
57 views: true,
58 by: true,
cf78883c 59 avatar: false,
abf325b4
C
60 privacyLabel: true,
61 privacyText: false,
62 state: false,
63 blacklistInfo: false
64 }
65
13adf228
RK
66 actions: {
67 routerLink: string
be27ef3b 68 iconName: GlobalIconName
13adf228
RK
69 label: string
70 }[] = []
71
ad453580
C
72 onDataSubject = new Subject<any[]>()
73
ba430d75
C
74 protected serverConfig: ServerConfig
75
f8b2c1b4 76 protected abstract notifier: Notifier
b2731bff 77 protected abstract authService: AuthService
d3217560 78 protected abstract userService: UserService
b2731bff 79 protected abstract route: ActivatedRoute
489290b8 80 protected abstract serverService: ServerService
bbe0f064 81 protected abstract screenService: ScreenService
d3217560 82 protected abstract storageService: LocalStorageService
489290b8 83 protected abstract router: Router
34c7f429 84 protected abstract i18n: I18n
9bf9d2a5 85 abstract titlePage: string
c88593f7 86
9af61e84 87 private resizeSubscription: Subscription
489290b8
C
88 private angularState: number
89
34c7f429
C
90 private groupedDateLabels: { [id in GroupDate]: string }
91 private groupedDates: { [id: number]: GroupDate } = {}
92
440d39c5
C
93 private lastQueryLength: number
94
95 abstract getVideosObservable (page: number): Observable<{ data: Video[] }>
9af61e84 96
c199c427 97 abstract generateSyndicationList (): void
fd45e8f4 98
b2731bff
C
99 get user () {
100 return this.authService.getUser()
101 }
102
fd45e8f4 103 ngOnInit () {
ba430d75
C
104 this.serverConfig = this.serverService.getTmpConfig()
105 this.serverService.getConfig()
106 .subscribe(config => this.serverConfig = config)
107
34c7f429
C
108 this.groupedDateLabels = {
109 [GroupDate.UNKNOWN]: null,
110 [GroupDate.TODAY]: this.i18n('Today'),
111 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
93aa8552
C
112 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
113 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
34c7f429
C
114 [GroupDate.OLDER]: this.i18n('Older')
115 }
116
fd45e8f4 117 // Subscribe to route changes
5b5e333f 118 const routeParams = this.route.snapshot.queryParams
2bbb3412 119 this.loadRouteParams(routeParams)
a2b817d3 120
9af61e84 121 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 122 .pipe(debounceTime(500))
6194c1b4 123 .subscribe(() => this.calcPageSizes())
3290f37c 124
6194c1b4 125 this.calcPageSizes()
3caf77d3
C
126
127 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
128
129 if (this.loadOnInit === true) {
130 loadUserObservable.subscribe(() => this.loadMoreVideos())
131 }
d3217560
RK
132
133 this.storageService.watch([
134 User.KEYS.NSFW_POLICY,
135 User.KEYS.VIDEO_LANGUAGES
136 ]).pipe(throttleTime(200)).subscribe(
137 () => {
138 this.loadUserVideoLanguagesIfNeeded()
139 if (this.hasDoneFirstQuery) this.reloadVideos()
140 }
141 )
cf78883c
C
142
143 // Display avatar in mobile view
144 if (this.screenService.isInMobileView()) {
145 this.displayOptions.avatar = true
146 }
fd45e8f4
C
147 }
148
9af61e84
C
149 ngOnDestroy () {
150 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
151 }
152
489290b8
C
153 disableForReuse () {
154 this.disabled = true
89724816
C
155 }
156
489290b8
C
157 enabledForReuse () {
158 this.disabled = false
89724816
C
159 }
160
489290b8
C
161 videoById (index: number, video: Video) {
162 return video.id
2bbb3412
C
163 }
164
165 onNearOfBottom () {
489290b8 166 if (this.disabled) return
2bbb3412 167
440d39c5
C
168 // No more results
169 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
0cd4344f 170
489290b8 171 this.pagination.currentPage += 1
a8ecc6f6 172
489290b8 173 this.setScrollRouteParams()
a8ecc6f6 174
489290b8
C
175 this.loadMoreVideos()
176 }
fd45e8f4 177
9d45db29 178 loadMoreVideos (reset = false) {
93cae479 179 this.getVideosObservable(this.pagination.currentPage).subscribe(
440d39c5
C
180 ({ data }) => {
181 this.hasDoneFirstQuery = true
182 this.lastQueryLength = data.length
183
9d45db29 184 if (reset) this.videos = []
93cae479 185 this.videos = this.videos.concat(data)
693263e9 186
34c7f429
C
187 if (this.groupByDate) this.buildGroupedDateLabels()
188
693263e9 189 this.onMoreVideos()
ad453580
C
190
191 this.onDataSubject.next(data)
fd45e8f4 192 },
017c3dca 193
f27a885a
C
194 error => {
195 const message = this.i18n('Cannot load more videos. Try again later.')
196
197 console.error(message, { error })
198 this.notifier.error(message)
199 }
489290b8 200 )
2bbb3412
C
201 }
202
489290b8
C
203 reloadVideos () {
204 this.pagination.currentPage = 1
9d45db29 205 this.loadMoreVideos(true)
fd45e8f4
C
206 }
207
489290b8
C
208 toggleModerationDisplay () {
209 throw new Error('toggleModerationDisplay is not implemented')
fd45e8f4
C
210 }
211
3a0fb65c
C
212 removeVideoFromArray (video: Video) {
213 this.videos = this.videos.filter(v => v.id !== video.id)
214 }
215
34c7f429
C
216 buildGroupedDateLabels () {
217 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
218
219 for (const video of this.videos) {
220 const publishedDate = video.publishedAt
221
4e0c1793
C
222 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
223 if (currentGroupedDate === GroupDate.TODAY) continue
224
34c7f429
C
225 currentGroupedDate = GroupDate.TODAY
226 this.groupedDates[ video.id ] = currentGroupedDate
227 continue
228 }
229
4e0c1793
C
230 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
231 if (currentGroupedDate === GroupDate.YESTERDAY) continue
232
34c7f429
C
233 currentGroupedDate = GroupDate.YESTERDAY
234 this.groupedDates[ video.id ] = currentGroupedDate
235 continue
236 }
237
93aa8552
C
238 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
239 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
4e0c1793 240
93aa8552 241 currentGroupedDate = GroupDate.LAST_WEEK
34c7f429
C
242 this.groupedDates[ video.id ] = currentGroupedDate
243 continue
244 }
245
93aa8552
C
246 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
247 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
4e0c1793 248
93aa8552 249 currentGroupedDate = GroupDate.LAST_MONTH
34c7f429
C
250 this.groupedDates[ video.id ] = currentGroupedDate
251 continue
252 }
253
4e0c1793
C
254 if (currentGroupedDate <= GroupDate.OLDER) {
255 if (currentGroupedDate === GroupDate.OLDER) continue
256
34c7f429
C
257 currentGroupedDate = GroupDate.OLDER
258 this.groupedDates[ video.id ] = currentGroupedDate
259 }
260 }
261 }
262
263 getCurrentGroupedDateLabel (video: Video) {
264 if (this.groupByDate === false) return undefined
265
266 return this.groupedDateLabels[this.groupedDates[video.id]]
267 }
268
693263e9
C
269 // On videos hook for children that want to do something
270 protected onMoreVideos () { /* empty */ }
271
fd45e8f4 272 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
489290b8
C
273 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
274 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
275 this.angularState = routeParams[ 'a-state' ]
0cd4344f 276 }
6194c1b4
C
277
278 private calcPageSizes () {
489290b8 279 if (this.screenService.isInMobileView()) {
6194c1b4 280 this.pagination.itemsPerPage = 5
6194c1b4 281 }
489290b8 282 }
6194c1b4 283
489290b8
C
284 private setScrollRouteParams () {
285 // Already set
286 if (this.angularState) return
6194c1b4 287
489290b8 288 this.angularState = 42
6194c1b4 289
489290b8
C
290 const queryParams = {
291 'a-state': this.angularState,
292 categoryOneOf: this.categoryOneOf
9af61e84 293 }
6194c1b4 294
489290b8 295 let path = this.router.url
ba430d75 296 if (!path || path === '/') path = this.serverConfig.instance.defaultClientRoute
489290b8
C
297
298 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
6194c1b4 299 }
3caf77d3
C
300
301 private loadUserVideoLanguagesIfNeeded () {
d3217560
RK
302 if (!this.useUserVideoLanguagePreferences) {
303 return of(true)
304 }
305
306 if (!this.authService.isLoggedIn()) {
307 this.languageOneOf = this.userService.getAnonymousUser().videoLanguages
3caf77d3
C
308 return of(true)
309 }
310
311 return this.authService.userInformationLoaded
312 .pipe(
313 first(),
314 tap(() => this.languageOneOf = this.user.videoLanguages)
315 )
316 }
fd45e8f4 317}