]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/abstract-video-list.ts
Disable webtorrent support in client
[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, Subject, 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 import { ResultList } from '@shared/models'
17
18 enum GroupDate {
19 UNKNOWN = 0,
20 TODAY = 1,
21 YESTERDAY = 2,
22 LAST_WEEK = 3,
23 LAST_MONTH = 4,
24 OLDER = 5
25 }
26
27 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
28 pagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 25,
31 totalItems: null
32 }
33 sort: VideoSortField = '-publishedAt'
34
35 categoryOneOf?: number
36 languageOneOf?: string[]
37 defaultSort: VideoSortField = '-publishedAt'
38
39 syndicationItems: Syndication[] = []
40
41 loadOnInit = true
42 useUserVideoLanguagePreferences = false
43 ownerDisplayType: OwnerDisplayType = 'account'
44 displayModerationBlock = false
45 titleTooltip: string
46 displayVideoActions = true
47 groupByDate = false
48
49 videos: Video[] = []
50 disabled = false
51
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
62 onDataSubject = new Subject<any[]>()
63
64 protected abstract notifier: Notifier
65 protected abstract authService: AuthService
66 protected abstract route: ActivatedRoute
67 protected abstract serverService: ServerService
68 protected abstract screenService: ScreenService
69 protected abstract router: Router
70 protected abstract i18n: I18n
71 abstract titlePage: string
72
73 private resizeSubscription: Subscription
74 private angularState: number
75
76 private groupedDateLabels: { [id in GroupDate]: string }
77 private groupedDates: { [id: number]: GroupDate } = {}
78
79 abstract getVideosObservable (page: number): Observable<ResultList<Video>>
80
81 abstract generateSyndicationList (): void
82
83 get user () {
84 return this.authService.getUser()
85 }
86
87 ngOnInit () {
88 this.groupedDateLabels = {
89 [GroupDate.UNKNOWN]: null,
90 [GroupDate.TODAY]: this.i18n('Today'),
91 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
92 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
93 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
94 [GroupDate.OLDER]: this.i18n('Older')
95 }
96
97 // Subscribe to route changes
98 const routeParams = this.route.snapshot.queryParams
99 this.loadRouteParams(routeParams)
100
101 this.resizeSubscription = fromEvent(window, 'resize')
102 .pipe(debounceTime(500))
103 .subscribe(() => this.calcPageSizes())
104
105 this.calcPageSizes()
106
107 const loadUserObservable = this.loadUserVideoLanguagesIfNeeded()
108
109 if (this.loadOnInit === true) {
110 loadUserObservable.subscribe(() => this.loadMoreVideos())
111 }
112 }
113
114 ngOnDestroy () {
115 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
116 }
117
118 disableForReuse () {
119 this.disabled = true
120 }
121
122 enabledForReuse () {
123 this.disabled = false
124 }
125
126 videoById (index: number, video: Video) {
127 return video.id
128 }
129
130 onNearOfBottom () {
131 if (this.disabled) return
132
133 // Last page
134 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
135
136 this.pagination.currentPage += 1
137
138 this.setScrollRouteParams()
139
140 this.loadMoreVideos()
141 }
142
143 loadMoreVideos () {
144 this.getVideosObservable(this.pagination.currentPage).subscribe(
145 ({ data, total }) => {
146 this.pagination.totalItems = total
147 this.videos = this.videos.concat(data)
148
149 if (this.groupByDate) this.buildGroupedDateLabels()
150
151 this.onMoreVideos()
152
153 this.onDataSubject.next(data)
154 },
155
156 error => this.notifier.error(error.message)
157 )
158 }
159
160 reloadVideos () {
161 this.pagination.currentPage = 1
162 this.videos = []
163 this.loadMoreVideos()
164 }
165
166 toggleModerationDisplay () {
167 throw new Error('toggleModerationDisplay is not implemented')
168 }
169
170 removeVideoFromArray (video: Video) {
171 this.videos = this.videos.filter(v => v.id !== video.id)
172 }
173
174 buildGroupedDateLabels () {
175 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
176
177 for (const video of this.videos) {
178 const publishedDate = video.publishedAt
179
180 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
181 if (currentGroupedDate === GroupDate.TODAY) continue
182
183 currentGroupedDate = GroupDate.TODAY
184 this.groupedDates[ video.id ] = currentGroupedDate
185 continue
186 }
187
188 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
189 if (currentGroupedDate === GroupDate.YESTERDAY) continue
190
191 currentGroupedDate = GroupDate.YESTERDAY
192 this.groupedDates[ video.id ] = currentGroupedDate
193 continue
194 }
195
196 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
197 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
198
199 currentGroupedDate = GroupDate.LAST_WEEK
200 this.groupedDates[ video.id ] = currentGroupedDate
201 continue
202 }
203
204 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
205 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
206
207 currentGroupedDate = GroupDate.LAST_MONTH
208 this.groupedDates[ video.id ] = currentGroupedDate
209 continue
210 }
211
212 if (currentGroupedDate <= GroupDate.OLDER) {
213 if (currentGroupedDate === GroupDate.OLDER) continue
214
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
227 // On videos hook for children that want to do something
228 protected onMoreVideos () { /* empty */ }
229
230 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
231 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
232 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
233 this.angularState = routeParams[ 'a-state' ]
234 }
235
236 private calcPageSizes () {
237 if (this.screenService.isInMobileView()) {
238 this.pagination.itemsPerPage = 5
239 }
240 }
241
242 private setScrollRouteParams () {
243 // Already set
244 if (this.angularState) return
245
246 this.angularState = 42
247
248 const queryParams = {
249 'a-state': this.angularState,
250 categoryOneOf: this.categoryOneOf
251 }
252
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' })
257 }
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 }
270 }