]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-videos/my-videos.component.ts
Implement two factor in client
[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 6import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
eaa52952 7import { prepareIcu, 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'
384ba8b7
C
11import {
12 MiniatureDisplayOptions,
13 SelectionType,
14 VideoActionsDisplayType,
15 VideosSelectionComponent
16} from '@app/shared/shared-video-miniature'
22e90922 17import { VideoChannel, VideoSortField } from '@shared/models'
d846d99c 18import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
202f6b6c
C
19
20@Component({
17119e4a
C
21 templateUrl: './my-videos.component.html',
22 styleUrls: [ './my-videos.component.scss' ]
202f6b6c 23})
2e46eb97 24export class MyVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
25 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
26 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
d846d99c 27 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
489290b8 28
b1d40cff 29 titlePage: string
693263e9 30 selection: SelectionType = {}
0cd4344f 31 pagination: ComponentPagination = {
234b535d 32 currentPage: 1,
1d904806 33 itemsPerPage: 10,
234b535d
C
34 totalItems: null
35 }
e2409062
C
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
37 date: true,
38 views: true,
c4a6f790 39 by: true,
e2409062
C
40 privacyLabel: false,
41 privacyText: true,
42 state: true,
3fca30a7
C
43 blacklistInfo: true,
44 forceChannelInBy: true
e2409062 45 }
384ba8b7
C
46 videoDropdownDisplayOptions: VideoActionsDisplayType = {
47 playlist: false,
48 download: false,
49 update: false,
50 blacklist: false,
51 delete: true,
52 report: false,
53 duplicate: false,
54 mute: false,
26490335 55 liveInfo: true,
384ba8b7
C
56 removeFiles: false,
57 transcoding: false,
58 studio: true,
59 stats: true
60 }
c4a6f790 61
384ba8b7 62 moreVideoActions: DropdownAction<{ video: Video }>[][] = []
d846d99c 63
693263e9
C
64 videos: Video[] = []
65 getVideosObservableFunction = this.getVideosObservable.bind(this)
2e46eb97 66
8e286cdc 67 sort: VideoSortField = '-publishedAt'
202f6b6c 68
241609f1
C
69 user: User
70
60ab5b99 71 inputFilters: AdvancedInputFilter[] = []
1fd61899 72
dd24f1bb
C
73 disabled = false
74
2e46eb97 75 private search: string
978c87e7 76 private userChannels: VideoChannel[] = []
2e46eb97 77
b1d40cff
C
78 constructor (
79 protected router: Router,
489290b8 80 protected serverService: ServerService,
b1d40cff
C
81 protected route: ActivatedRoute,
82 protected authService: AuthService,
f8b2c1b4 83 protected notifier: Notifier,
bbe0f064 84 protected screenService: ScreenService,
308c4275 85 private confirmService: ConfirmService,
693263e9 86 private videoService: VideoService
b1d40cff 87 ) {
66357162 88 this.titlePage = $localize`My videos`
202f6b6c
C
89 }
90
bf64ed41 91 ngOnInit () {
d846d99c
C
92 this.buildActions()
93
241609f1 94 this.user = this.authService.getUser()
ca44cb36
C
95
96 if (this.route.snapshot.queryParams['search']) {
97 this.search = this.route.snapshot.queryParams['search']
98 }
978c87e7
C
99
100 this.authService.userInformationLoaded.subscribe(() => {
101 this.user = this.authService.getUser()
102 this.userChannels = this.user.videoChannels
103
104 const channelFilters = this.userChannels.map(c => {
105 return {
dd6d2a7c 106 value: 'channel:' + c.name,
978c87e7
C
107 label: c.name
108 }
109 })
110
111 this.inputFilters = [
112 {
113 title: $localize`Advanced filters`,
114 children: [
115 {
dd6d2a7c 116 value: 'isLive:true',
978c87e7
C
117 label: $localize`Only live videos`
118 }
119 ]
120 },
121
122 {
123 title: $localize`Channel filters`,
124 children: channelFilters
125 }
126 ]
127 })
bf64ed41
RK
128 }
129
2e46eb97
C
130 onSearch (search: string) {
131 this.search = search
132 this.reloadData()
4f5d0459
RK
133 }
134
2e46eb97 135 reloadData () {
1fd61899 136 this.videosSelection.reloadVideos()
bf64ed41
RK
137 }
138
8e286cdc
RK
139 onChangeSortColumn () {
140 this.videosSelection.reloadVideos()
141 }
142
693263e9 143 disableForReuse () {
dd24f1bb 144 this.disabled = true
9af61e84
C
145 }
146
693263e9 147 enabledForReuse () {
dd24f1bb 148 this.disabled = false
ce0e281d
C
149 }
150
0df302ca 151 getVideosObservable (page: number) {
0cd4344f
C
152 const newPagination = immutableAssign(this.pagination, { currentPage: page })
153
978c87e7
C
154 return this.videoService.getMyVideos({
155 videoPagination: newPagination,
156 sort: this.sort,
157 userChannels: this.userChannels,
158 search: this.search
159 })
aa0f1963
RK
160 .pipe(
161 tap(res => this.pagination.totalItems = res.total)
162 )
244e76a5
RK
163 }
164
1f30a185 165 async deleteSelectedVideos () {
693263e9 166 const toDeleteVideosIds = Object.keys(this.selection)
9df52d66 167 .filter(k => this.selection[k] === true)
2186386c 168 .map(k => parseInt(k, 10))
ce0e281d 169
2186386c 170 const res = await this.confirmService.confirm(
eaa52952
C
171 prepareIcu($localize`Do you really want to delete {length, plural, =1 {this video} other {{length} videos}}?`)(
172 { length: toDeleteVideosIds.length },
173 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`
174 ),
66357162 175 $localize`Delete`
2186386c 176 )
1f30a185
C
177 if (res === false) return
178
179 const observables: Observable<any>[] = []
180 for (const videoId of toDeleteVideosIds) {
2186386c 181 const o = this.videoService.removeVideo(videoId)
489290b8 182 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
183
184 observables.push(o)
185 }
186
489290b8
C
187 concat(...observables)
188 .pipe(toArray())
1378c0d3
C
189 .subscribe({
190 next: () => {
eaa52952
C
191 this.notifier.success(
192 prepareIcu($localize`{length, plural, =1 {Video has been deleted} other {{length} videos have been deleted}}`)(
193 { length: toDeleteVideosIds.length },
194 $localize`${toDeleteVideosIds.length} have been deleted.`
195 )
196 )
197
693263e9 198 this.selection = {}
1f30a185
C
199 },
200
1378c0d3
C
201 error: err => this.notifier.error(err.message)
202 })
ce0e281d
C
203 }
204
384ba8b7
C
205 onVideoRemoved (video: Video) {
206 this.removeVideoFromArray(video.id)
2186386c 207 }
1f30a185 208
d846d99c 209 changeOwnership (video: Video) {
74d63469
GR
210 this.videoChangeOwnershipModal.show(video)
211 }
212
489290b8
C
213 private removeVideoFromArray (id: number) {
214 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 215 }
d846d99c
C
216
217 private buildActions () {
384ba8b7
C
218 this.moreVideoActions = [
219 [
220 {
221 label: $localize`Change ownership`,
222 handler: ({ video }) => this.changeOwnership(video),
223 iconName: 'ownership-change'
224 }
225 ]
d846d99c
C
226 ]
227 }
202f6b6c 228}