]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
Prefer using last week/last month
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
CommitLineData
db400f44 1import { debounceTime } from 'rxjs/operators'
489290b8 2import { OnDestroy, OnInit } from '@angular/core'
fd45e8f4 3import { ActivatedRoute, Router } from '@angular/router'
db400f44 4import { fromEvent, Observable, 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'
34c7f429
C
16
17enum GroupDate {
18 UNKNOWN = 0,
19 TODAY = 1,
20 YESTERDAY = 2,
93aa8552
C
21 LAST_WEEK = 3,
22 LAST_MONTH = 4,
34c7f429
C
23 OLDER = 5
24}
0cd4344f 25
489290b8 26export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
4635f59d 27 pagination: ComponentPagination = {
fd45e8f4 28 currentPage: 1,
489290b8 29 itemsPerPage: 25,
fd45e8f4
C
30 totalItems: null
31 }
136cce4d 32 sort: VideoSortField = '-publishedAt'
489290b8 33
d59cba29 34 categoryOneOf?: number
136cce4d 35 defaultSort: VideoSortField = '-publishedAt'
489290b8 36
c199c427 37 syndicationItems: Syndication[] = []
244e76a5 38
f3aaa9a9 39 loadOnInit = true
489290b8 40 videos: Video[] = []
22a16e36 41 ownerDisplayType: OwnerDisplayType = 'account'
017c3dca 42 displayModerationBlock = false
9b4b15f9 43 titleTooltip: string
3a0fb65c 44 displayVideoActions = true
34c7f429 45 groupByDate = false
fd45e8f4 46
489290b8 47 disabled = false
9af61e84 48
abf325b4
C
49 displayOptions: MiniatureDisplayOptions = {
50 date: true,
51 views: true,
52 by: true,
53 privacyLabel: true,
54 privacyText: false,
55 state: false,
56 blacklistInfo: false
57 }
58
f8b2c1b4 59 protected abstract notifier: Notifier
b2731bff 60 protected abstract authService: AuthService
b2731bff 61 protected abstract route: ActivatedRoute
489290b8 62 protected abstract serverService: ServerService
bbe0f064 63 protected abstract screenService: ScreenService
489290b8 64 protected abstract router: Router
34c7f429 65 protected abstract i18n: I18n
9bf9d2a5 66 abstract titlePage: string
c88593f7 67
9af61e84 68 private resizeSubscription: Subscription
489290b8
C
69 private angularState: number
70
34c7f429
C
71 private groupedDateLabels: { [id in GroupDate]: string }
72 private groupedDates: { [id: number]: GroupDate } = {}
73
489290b8 74 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
9af61e84 75
c199c427 76 abstract generateSyndicationList (): void
fd45e8f4 77
b2731bff
C
78 get user () {
79 return this.authService.getUser()
80 }
81
fd45e8f4 82 ngOnInit () {
34c7f429
C
83 this.groupedDateLabels = {
84 [GroupDate.UNKNOWN]: null,
85 [GroupDate.TODAY]: this.i18n('Today'),
86 [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
93aa8552
C
87 [GroupDate.LAST_WEEK]: this.i18n('Last week'),
88 [GroupDate.LAST_MONTH]: this.i18n('Last month'),
34c7f429
C
89 [GroupDate.OLDER]: this.i18n('Older')
90 }
91
fd45e8f4 92 // Subscribe to route changes
5b5e333f 93 const routeParams = this.route.snapshot.queryParams
2bbb3412 94 this.loadRouteParams(routeParams)
a2b817d3 95
9af61e84 96 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 97 .pipe(debounceTime(500))
6194c1b4 98 .subscribe(() => this.calcPageSizes())
3290f37c 99
6194c1b4 100 this.calcPageSizes()
489290b8 101 if (this.loadOnInit === true) this.loadMoreVideos()
fd45e8f4
C
102 }
103
9af61e84
C
104 ngOnDestroy () {
105 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
106 }
107
489290b8
C
108 disableForReuse () {
109 this.disabled = true
89724816
C
110 }
111
489290b8
C
112 enabledForReuse () {
113 this.disabled = false
89724816
C
114 }
115
489290b8
C
116 videoById (index: number, video: Video) {
117 return video.id
2bbb3412
C
118 }
119
120 onNearOfBottom () {
489290b8 121 if (this.disabled) return
2bbb3412 122
489290b8
C
123 // Last page
124 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
0cd4344f 125
489290b8 126 this.pagination.currentPage += 1
a8ecc6f6 127
489290b8 128 this.setScrollRouteParams()
a8ecc6f6 129
489290b8
C
130 this.loadMoreVideos()
131 }
fd45e8f4 132
489290b8
C
133 loadMoreVideos () {
134 const observable = this.getVideosObservable(this.pagination.currentPage)
fd45e8f4
C
135
136 observable.subscribe(
137 ({ videos, totalVideos }) => {
fd45e8f4 138 this.pagination.totalItems = totalVideos
489290b8 139 this.videos = this.videos.concat(videos)
693263e9 140
34c7f429
C
141 if (this.groupByDate) this.buildGroupedDateLabels()
142
693263e9 143 this.onMoreVideos()
fd45e8f4 144 },
017c3dca 145
489290b8
C
146 error => this.notifier.error(error.message)
147 )
2bbb3412
C
148 }
149
489290b8
C
150 reloadVideos () {
151 this.pagination.currentPage = 1
152 this.videos = []
153 this.loadMoreVideos()
fd45e8f4
C
154 }
155
489290b8
C
156 toggleModerationDisplay () {
157 throw new Error('toggleModerationDisplay is not implemented')
fd45e8f4
C
158 }
159
3a0fb65c
C
160 removeVideoFromArray (video: Video) {
161 this.videos = this.videos.filter(v => v.id !== video.id)
162 }
163
34c7f429
C
164 buildGroupedDateLabels () {
165 let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
166
167 for (const video of this.videos) {
168 const publishedDate = video.publishedAt
169
4e0c1793
C
170 if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
171 if (currentGroupedDate === GroupDate.TODAY) continue
172
34c7f429
C
173 currentGroupedDate = GroupDate.TODAY
174 this.groupedDates[ video.id ] = currentGroupedDate
175 continue
176 }
177
4e0c1793
C
178 if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
179 if (currentGroupedDate === GroupDate.YESTERDAY) continue
180
34c7f429
C
181 currentGroupedDate = GroupDate.YESTERDAY
182 this.groupedDates[ video.id ] = currentGroupedDate
183 continue
184 }
185
93aa8552
C
186 if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
187 if (currentGroupedDate === GroupDate.LAST_WEEK) continue
4e0c1793 188
93aa8552 189 currentGroupedDate = GroupDate.LAST_WEEK
34c7f429
C
190 this.groupedDates[ video.id ] = currentGroupedDate
191 continue
192 }
193
93aa8552
C
194 if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
195 if (currentGroupedDate === GroupDate.LAST_MONTH) continue
4e0c1793 196
93aa8552 197 currentGroupedDate = GroupDate.LAST_MONTH
34c7f429
C
198 this.groupedDates[ video.id ] = currentGroupedDate
199 continue
200 }
201
4e0c1793
C
202 if (currentGroupedDate <= GroupDate.OLDER) {
203 if (currentGroupedDate === GroupDate.OLDER) continue
204
34c7f429
C
205 currentGroupedDate = GroupDate.OLDER
206 this.groupedDates[ video.id ] = currentGroupedDate
207 }
208 }
209 }
210
211 getCurrentGroupedDateLabel (video: Video) {
212 if (this.groupByDate === false) return undefined
213
214 return this.groupedDateLabels[this.groupedDates[video.id]]
215 }
216
693263e9
C
217 // On videos hook for children that want to do something
218 protected onMoreVideos () { /* empty */ }
219
fd45e8f4 220 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
489290b8
C
221 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
222 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
223 this.angularState = routeParams[ 'a-state' ]
0cd4344f 224 }
6194c1b4
C
225
226 private calcPageSizes () {
489290b8 227 if (this.screenService.isInMobileView()) {
6194c1b4 228 this.pagination.itemsPerPage = 5
6194c1b4 229 }
489290b8 230 }
6194c1b4 231
489290b8
C
232 private setScrollRouteParams () {
233 // Already set
234 if (this.angularState) return
6194c1b4 235
489290b8 236 this.angularState = 42
6194c1b4 237
489290b8
C
238 const queryParams = {
239 'a-state': this.angularState,
240 categoryOneOf: this.categoryOneOf
9af61e84 241 }
6194c1b4 242
489290b8
C
243 let path = this.router.url
244 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
245
246 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
6194c1b4 247 }
fd45e8f4 248}