]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-videos/my-videos.component.ts
Add video filters to common video pages
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-videos / my-videos.component.ts
CommitLineData
1fd61899
C
1import { concat, Observable } from 'rxjs'
2import { tap, toArray } from 'rxjs/operators'
2e46eb97 3import { Component, OnInit, ViewChild } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
2e46eb97 5import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
67ed6552
C
6import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7import { immutableAssign } from '@app/helpers'
1fd61899 8import { AdvancedInputFilter } from '@app/shared/shared-forms'
d846d99c 9import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
f8c00564 10import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
733dbc53 11import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
67ed6552 12import { VideoSortField } from '@shared/models'
d846d99c 13import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
202f6b6c
C
14
15@Component({
17119e4a
C
16 templateUrl: './my-videos.component.html',
17 styleUrls: [ './my-videos.component.scss' ]
202f6b6c 18})
2e46eb97 19export class MyVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
20 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
21 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
d846d99c 22 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
489290b8 23
b1d40cff 24 titlePage: string
693263e9 25 selection: SelectionType = {}
0cd4344f 26 pagination: ComponentPagination = {
234b535d 27 currentPage: 1,
1d904806 28 itemsPerPage: 10,
234b535d
C
29 totalItems: null
30 }
e2409062
C
31 miniatureDisplayOptions: MiniatureDisplayOptions = {
32 date: true,
33 views: true,
c4a6f790 34 by: true,
e2409062
C
35 privacyLabel: false,
36 privacyText: true,
37 state: true,
38 blacklistInfo: true
39 }
c4a6f790 40
d846d99c
C
41 videoActions: DropdownAction<{ video: Video }>[] = []
42
693263e9
C
43 videos: Video[] = []
44 getVideosObservableFunction = this.getVideosObservable.bind(this)
2e46eb97 45
8e286cdc 46 sort: VideoSortField = '-publishedAt'
202f6b6c 47
241609f1
C
48 user: User
49
1fd61899
C
50 inputFilters: AdvancedInputFilter[] = [
51 {
9df52d66 52 queryParams: { search: 'isLive:true' },
1fd61899
C
53 label: $localize`Only live videos`
54 }
55 ]
56
dd24f1bb
C
57 disabled = false
58
2e46eb97
C
59 private search: string
60
b1d40cff
C
61 constructor (
62 protected router: Router,
489290b8 63 protected serverService: ServerService,
b1d40cff
C
64 protected route: ActivatedRoute,
65 protected authService: AuthService,
f8b2c1b4 66 protected notifier: Notifier,
bbe0f064 67 protected screenService: ScreenService,
308c4275 68 private confirmService: ConfirmService,
693263e9 69 private videoService: VideoService
b1d40cff 70 ) {
66357162 71 this.titlePage = $localize`My videos`
202f6b6c
C
72 }
73
bf64ed41 74 ngOnInit () {
d846d99c
C
75 this.buildActions()
76
241609f1 77 this.user = this.authService.getUser()
bf64ed41
RK
78 }
79
2e46eb97
C
80 onSearch (search: string) {
81 this.search = search
82 this.reloadData()
4f5d0459
RK
83 }
84
2e46eb97 85 reloadData () {
1fd61899 86 this.videosSelection.reloadVideos()
bf64ed41
RK
87 }
88
8e286cdc
RK
89 onChangeSortColumn () {
90 this.videosSelection.reloadVideos()
91 }
92
693263e9 93 disableForReuse () {
dd24f1bb 94 this.disabled = true
9af61e84
C
95 }
96
693263e9 97 enabledForReuse () {
dd24f1bb 98 this.disabled = false
ce0e281d
C
99 }
100
0df302ca 101 getVideosObservable (page: number) {
0cd4344f
C
102 const newPagination = immutableAssign(this.pagination, { currentPage: page })
103
1fd61899 104 return this.videoService.getMyVideos(newPagination, this.sort, this.search)
aa0f1963
RK
105 .pipe(
106 tap(res => this.pagination.totalItems = res.total)
107 )
244e76a5
RK
108 }
109
1f30a185 110 async deleteSelectedVideos () {
693263e9 111 const toDeleteVideosIds = Object.keys(this.selection)
9df52d66 112 .filter(k => this.selection[k] === true)
2186386c 113 .map(k => parseInt(k, 10))
ce0e281d 114
2186386c 115 const res = await this.confirmService.confirm(
66357162
C
116 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
117 $localize`Delete`
2186386c 118 )
1f30a185
C
119 if (res === false) return
120
121 const observables: Observable<any>[] = []
122 for (const videoId of toDeleteVideosIds) {
2186386c 123 const o = this.videoService.removeVideo(videoId)
489290b8 124 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
125
126 observables.push(o)
127 }
128
489290b8
C
129 concat(...observables)
130 .pipe(toArray())
1378c0d3
C
131 .subscribe({
132 next: () => {
66357162 133 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
693263e9 134 this.selection = {}
1f30a185
C
135 },
136
1378c0d3
C
137 error: err => this.notifier.error(err.message)
138 })
ce0e281d
C
139 }
140
1f30a185 141 async deleteVideo (video: Video) {
2186386c 142 const res = await this.confirmService.confirm(
66357162
C
143 $localize`Do you really want to delete ${video.name}?`,
144 $localize`Delete`
2186386c 145 )
1f30a185
C
146 if (res === false) return
147
148 this.videoService.removeVideo(video.id)
1378c0d3
C
149 .subscribe({
150 next: () => {
66357162 151 this.notifier.success($localize`Video ${video.name} deleted.`)
693263e9 152 this.removeVideoFromArray(video.id)
2186386c
C
153 },
154
1378c0d3
C
155 error: err => this.notifier.error(err.message)
156 })
2186386c 157 }
1f30a185 158
d846d99c 159 changeOwnership (video: Video) {
74d63469
GR
160 this.videoChangeOwnershipModal.show(video)
161 }
162
d846d99c
C
163 displayLiveInformation (video: Video) {
164 this.liveStreamInformationModal.show(video)
165 }
166
489290b8
C
167 private removeVideoFromArray (id: number) {
168 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 169 }
d846d99c
C
170
171 private buildActions () {
172 this.videoActions = [
173 {
174 label: $localize`Display live information`,
175 handler: ({ video }) => this.displayLiveInformation(video),
176 isDisplayed: ({ video }) => video.isLive,
177 iconName: 'live'
178 },
179 {
180 label: $localize`Change ownership`,
181 handler: ({ video }) => this.changeOwnership(video),
182 iconName: 'ownership-change'
183 },
184 {
185 label: $localize`Delete`,
186 handler: ({ video }) => this.deleteVideo(video),
187 iconName: 'delete'
188 }
189 ]
190 }
202f6b6c 191}