Merge branch 'release/beta-10' into develop
[oweals/peertube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, Inject, LOCALE_ID } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { Location } from '@angular/common'
6 import { immutableAssign } from '@app/shared/misc/utils'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { NotificationsService } from 'angular2-notifications'
9 import { AuthService } from '../../core/auth'
10 import { ConfirmService } from '../../core/confirm'
11 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
12 import { Video } from '../../shared/video/video.model'
13 import { VideoService } from '../../shared/video/video.service'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
16 import { ScreenService } from '@app/shared/misc/screen.service'
17
18 @Component({
19   selector: 'my-account-videos',
20   templateUrl: './my-account-videos.component.html',
21   styleUrls: [ './my-account-videos.component.scss' ]
22 })
23 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
24   titlePage: string
25   currentRoute = '/my-account/videos'
26   checkedVideos: { [ id: number ]: boolean } = {}
27   pagination: ComponentPagination = {
28     currentPage: 1,
29     itemsPerPage: 5,
30     totalItems: null
31   }
32
33   protected baseVideoWidth = -1
34   protected baseVideoHeight = 155
35
36   constructor (
37     protected router: Router,
38     protected route: ActivatedRoute,
39     protected authService: AuthService,
40     protected notificationsService: NotificationsService,
41     protected location: Location,
42     protected screenService: ScreenService,
43     protected i18n: I18n,
44     private confirmService: ConfirmService,
45     private videoService: VideoService,
46     @Inject(LOCALE_ID) private localeId: string
47   ) {
48     super()
49
50     this.titlePage = this.i18n('My videos')
51   }
52
53   ngOnInit () {
54     super.ngOnInit()
55   }
56
57   ngOnDestroy () {
58     super.ngOnDestroy()
59   }
60
61   abortSelectionMode () {
62     this.checkedVideos = {}
63   }
64
65   isInSelectionMode () {
66     return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
67   }
68
69   getVideosObservable (page: number) {
70     const newPagination = immutableAssign(this.pagination, { currentPage: page })
71
72     return this.videoService.getMyVideos(newPagination, this.sort)
73   }
74
75   generateSyndicationList () {
76     throw new Error('Method not implemented.')
77   }
78
79   async deleteSelectedVideos () {
80     const toDeleteVideosIds = Object.keys(this.checkedVideos)
81                                     .filter(k => this.checkedVideos[ k ] === true)
82                                     .map(k => parseInt(k, 10))
83
84     const res = await this.confirmService.confirm(
85       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
86       this.i18n('Delete')
87     )
88     if (res === false) return
89
90     const observables: Observable<any>[] = []
91     for (const videoId of toDeleteVideosIds) {
92       const o = this.videoService.removeVideo(videoId)
93                     .pipe(tap(() => this.spliceVideosById(videoId)))
94
95       observables.push(o)
96     }
97
98     observableFrom(observables)
99       .pipe(concatAll())
100       .subscribe(
101         res => {
102           this.notificationsService.success(
103             this.i18n('Success'),
104             this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })
105           )
106
107           this.abortSelectionMode()
108           this.reloadVideos()
109         },
110
111         err => this.notificationsService.error(this.i18n('Error'), err.message)
112       )
113   }
114
115   async deleteVideo (video: Video) {
116     const res = await this.confirmService.confirm(
117       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
118       this.i18n('Delete')
119     )
120     if (res === false) return
121
122     this.videoService.removeVideo(video.id)
123         .subscribe(
124           status => {
125             this.notificationsService.success(
126               this.i18n('Success'),
127               this.i18n('Video {{videoName}} deleted.', { videoName: video.name })
128             )
129             this.reloadVideos()
130           },
131
132           error => this.notificationsService.error(this.i18n('Error'), error.message)
133         )
134   }
135
136   getStateLabel (video: Video) {
137     let suffix: string
138
139     if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
140       suffix = this.i18n('Published')
141     } else if (video.scheduledUpdate) {
142       const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
143       suffix = this.i18n('Publication scheduled on ') + updateAt
144     } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
145       suffix = this.i18n('Waiting transcoding')
146     } else if (video.state.id === VideoState.TO_TRANSCODE) {
147       suffix = this.i18n('To transcode')
148     } else if (video.state.id === VideoState.TO_IMPORT) {
149       suffix = this.i18n('To import')
150     } else {
151       return ''
152     }
153
154     return ' - ' + suffix
155   }
156
157   protected buildVideoHeight () {
158     // In account videos, the video height is fixed
159     return this.baseVideoHeight
160   }
161
162   private spliceVideosById (id: number) {
163     for (const key of Object.keys(this.loadedPages)) {
164       const videos = this.loadedPages[ key ]
165       const index = videos.findIndex(v => v.id === id)
166
167       if (index !== -1) {
168         videos.splice(index, 1)
169         return
170       }
171     }
172   }
173 }