Merge branch 'master' into develop
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { concat, Observable } from 'rxjs'
2 import { tap, toArray } from 'rxjs/operators'
3 import { Component, ViewChild } 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 DisableForReuseHook {
26   @ViewChild('videosSelection') videosSelection: VideosSelectionComponent
27   @ViewChild('videoChangeOwnershipModal') 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   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 i18n: I18n,
56     private confirmService: ConfirmService,
57     private videoService: VideoService
58   ) {
59     this.titlePage = this.i18n('My videos')
60   }
61
62   disableForReuse () {
63     this.videosSelection.disableForReuse()
64   }
65
66   enabledForReuse () {
67     this.videosSelection.enabledForReuse()
68   }
69
70   getVideosObservable (page: number, sort: VideoSortField) {
71     const newPagination = immutableAssign(this.pagination, { currentPage: page })
72
73     return this.videoService.getMyVideos(newPagination, sort)
74   }
75
76   async deleteSelectedVideos () {
77     const toDeleteVideosIds = Object.keys(this.selection)
78                                     .filter(k => this.selection[ k ] === true)
79                                     .map(k => parseInt(k, 10))
80
81     const res = await this.confirmService.confirm(
82       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
83       this.i18n('Delete')
84     )
85     if (res === false) return
86
87     const observables: Observable<any>[] = []
88     for (const videoId of toDeleteVideosIds) {
89       const o = this.videoService.removeVideo(videoId)
90                     .pipe(tap(() => this.removeVideoFromArray(videoId)))
91
92       observables.push(o)
93     }
94
95     concat(...observables)
96       .pipe(toArray())
97       .subscribe(
98         () => {
99           this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
100
101           this.selection = {}
102         },
103
104         err => this.notifier.error(err.message)
105       )
106   }
107
108   async deleteVideo (video: Video) {
109     const res = await this.confirmService.confirm(
110       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
111       this.i18n('Delete')
112     )
113     if (res === false) return
114
115     this.videoService.removeVideo(video.id)
116         .subscribe(
117           () => {
118             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
119             this.removeVideoFromArray(video.id)
120           },
121
122           error => this.notifier.error(error.message)
123         )
124   }
125
126   changeOwnership (event: Event, video: Video) {
127     event.preventDefault()
128     this.videoChangeOwnershipModal.show(video)
129   }
130
131   private removeVideoFromArray (id: number) {
132     this.videos = this.videos.filter(v => v.id !== id)
133   }
134 }