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