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