Add manage buttons for own channels and account, video counts (#2421)
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { concat, Observable, Subject } from 'rxjs'
2 import { tap, toArray, debounceTime } from 'rxjs/operators'
3 import { Component, ViewChild, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { immutableAssign } from '@app/shared/misc/utils'
6 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
7 import { Notifier, ServerService } from '@app/core'
8 import { AuthService } from '../../core/auth'
9 import { ConfirmService } from '../../core/confirm'
10 import { Video } from '../../shared/video/video.model'
11 import { VideoService } from '../../shared/video/video.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { ScreenService } from '@app/shared/misc/screen.service'
14 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
15 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
16 import { SelectionType, VideosSelectionComponent } from '@app/shared/video/videos-selection.component'
17 import { VideoSortField } from '@app/shared/video/sort-field.type'
18 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
19
20 @Component({
21   selector: 'my-account-videos',
22   templateUrl: './my-account-videos.component.html',
23   styleUrls: [ './my-account-videos.component.scss' ]
24 })
25 export class MyAccountVideosComponent implements OnInit, DisableForReuseHook {
26   @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
27   @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
28
29   titlePage: string
30   selection: SelectionType = {}
31   pagination: ComponentPagination = {
32     currentPage: 1,
33     itemsPerPage: 5,
34     totalItems: null
35   }
36   miniatureDisplayOptions: MiniatureDisplayOptions = {
37     date: true,
38     views: true,
39     by: false,
40     privacyLabel: false,
41     privacyText: true,
42     state: true,
43     blacklistInfo: true
44   }
45   videos: Video[] = []
46   videosSearch: string
47   videosSearchChanged = new Subject<string>()
48   getVideosObservableFunction = this.getVideosObservable.bind(this)
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 i18n: I18n,
58     private confirmService: ConfirmService,
59     private videoService: VideoService
60   ) {
61     this.titlePage = this.i18n('My videos')
62   }
63
64   ngOnInit () {
65     this.videosSearchChanged
66       .pipe(
67         debounceTime(500))
68       .subscribe(() => {
69         this.videosSelection.reloadVideos()
70       })
71   }
72
73   onVideosSearchChanged () {
74     this.videosSearchChanged.next()
75   }
76
77   disableForReuse () {
78     this.videosSelection.disableForReuse()
79   }
80
81   enabledForReuse () {
82     this.videosSelection.enabledForReuse()
83   }
84
85   getVideosObservable (page: number, sort: VideoSortField) {
86     const newPagination = immutableAssign(this.pagination, { currentPage: page })
87
88     return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
89       .pipe(
90         tap(res => this.pagination.totalItems = res.total)
91       )
92   }
93
94   async deleteSelectedVideos () {
95     const toDeleteVideosIds = Object.keys(this.selection)
96                                     .filter(k => this.selection[ k ] === true)
97                                     .map(k => parseInt(k, 10))
98
99     const res = await this.confirmService.confirm(
100       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
101       this.i18n('Delete')
102     )
103     if (res === false) return
104
105     const observables: Observable<any>[] = []
106     for (const videoId of toDeleteVideosIds) {
107       const o = this.videoService.removeVideo(videoId)
108                     .pipe(tap(() => this.removeVideoFromArray(videoId)))
109
110       observables.push(o)
111     }
112
113     concat(...observables)
114       .pipe(toArray())
115       .subscribe(
116         () => {
117           this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
118
119           this.selection = {}
120         },
121
122         err => this.notifier.error(err.message)
123       )
124   }
125
126   async deleteVideo (video: Video) {
127     const res = await this.confirmService.confirm(
128       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
129       this.i18n('Delete')
130     )
131     if (res === false) return
132
133     this.videoService.removeVideo(video.id)
134         .subscribe(
135           () => {
136             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
137             this.removeVideoFromArray(video.id)
138           },
139
140           error => this.notifier.error(error.message)
141         )
142   }
143
144   changeOwnership (event: Event, video: Video) {
145     event.preventDefault()
146     this.videoChangeOwnershipModal.show(video)
147   }
148
149   private removeVideoFromArray (id: number) {
150     this.videos = this.videos.filter(v => v.id !== id)
151   }
152 }