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