]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-videos/my-videos.component.ts
Fix NSFW policy in my videos list
[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, User, UserService } 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 user: User
49
50 constructor (
51 protected router: Router,
52 protected serverService: ServerService,
53 protected route: ActivatedRoute,
54 protected authService: AuthService,
55 protected notifier: Notifier,
56 protected screenService: ScreenService,
57 private confirmService: ConfirmService,
58 private videoService: VideoService
59 ) {
60 this.titlePage = $localize`My videos`
61 }
62
63 ngOnInit () {
64 this.buildActions()
65
66 this.user = this.authService.getUser()
67
68 this.videosSearchChanged
69 .pipe(debounceTime(500))
70 .subscribe(() => {
71 this.videosSelection.reloadVideos()
72 })
73 }
74
75 resetSearch () {
76 this.videosSearch = ''
77 this.onVideosSearchChanged()
78 }
79
80 onVideosSearchChanged () {
81 this.videosSearchChanged.next()
82 }
83
84 disableForReuse () {
85 this.videosSelection.disableForReuse()
86 }
87
88 enabledForReuse () {
89 this.videosSelection.enabledForReuse()
90 }
91
92 getVideosObservable (page: number, sort: VideoSortField) {
93 const newPagination = immutableAssign(this.pagination, { currentPage: page })
94
95 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
96 .pipe(
97 tap(res => this.pagination.totalItems = res.total)
98 )
99 }
100
101 async deleteSelectedVideos () {
102 const toDeleteVideosIds = Object.keys(this.selection)
103 .filter(k => this.selection[ k ] === true)
104 .map(k => parseInt(k, 10))
105
106 const res = await this.confirmService.confirm(
107 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
108 $localize`Delete`
109 )
110 if (res === false) return
111
112 const observables: Observable<any>[] = []
113 for (const videoId of toDeleteVideosIds) {
114 const o = this.videoService.removeVideo(videoId)
115 .pipe(tap(() => this.removeVideoFromArray(videoId)))
116
117 observables.push(o)
118 }
119
120 concat(...observables)
121 .pipe(toArray())
122 .subscribe(
123 () => {
124 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
125 this.selection = {}
126 },
127
128 err => this.notifier.error(err.message)
129 )
130 }
131
132 async deleteVideo (video: Video) {
133 const res = await this.confirmService.confirm(
134 $localize`Do you really want to delete ${video.name}?`,
135 $localize`Delete`
136 )
137 if (res === false) return
138
139 this.videoService.removeVideo(video.id)
140 .subscribe(
141 () => {
142 this.notifier.success($localize`Video ${video.name} deleted.`)
143 this.removeVideoFromArray(video.id)
144 },
145
146 error => this.notifier.error(error.message)
147 )
148 }
149
150 changeOwnership (video: Video) {
151 this.videoChangeOwnershipModal.show(video)
152 }
153
154 displayLiveInformation (video: Video) {
155 this.liveStreamInformationModal.show(video)
156 }
157
158 private removeVideoFromArray (id: number) {
159 this.videos = this.videos.filter(v => v.id !== id)
160 }
161
162 private buildActions () {
163 this.videoActions = [
164 {
165 label: $localize`Display live information`,
166 handler: ({ video }) => this.displayLiveInformation(video),
167 isDisplayed: ({ video }) => video.isLive,
168 iconName: 'live'
169 },
170 {
171 label: $localize`Change ownership`,
172 handler: ({ video }) => this.changeOwnership(video),
173 iconName: 'ownership-change'
174 },
175 {
176 label: $localize`Delete`,
177 handler: ({ video }) => this.deleteVideo(video),
178 iconName: 'delete'
179 }
180 ]
181 }
182 }