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