Refactor video miniatures
[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, Inject, LOCALE_ID, OnDestroy, OnInit, 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 { AbstractVideoList } from '../../shared/video/abstract-video-list'
11 import { Video } from '../../shared/video/video.model'
12 import { VideoService } from '../../shared/video/video.service'
13 import { I18n } from '@ngx-translate/i18n-polyfill'
14 import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
15 import { ScreenService } from '@app/shared/misc/screen.service'
16 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
17 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
18
19 @Component({
20   selector: 'my-account-videos',
21   templateUrl: './my-account-videos.component.html',
22   styleUrls: [ './my-account-videos.component.scss' ]
23 })
24 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
25   @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
26
27   titlePage: string
28   checkedVideos: { [ id: number ]: boolean } = {}
29   pagination: ComponentPagination = {
30     currentPage: 1,
31     itemsPerPage: 5,
32     totalItems: null
33   }
34   miniatureDisplayOptions: MiniatureDisplayOptions = {
35     date: true,
36     views: true,
37     by: false,
38     privacyLabel: false,
39     privacyText: true,
40     state: true,
41     blacklistInfo: true
42   }
43
44   constructor (
45     protected router: Router,
46     protected serverService: ServerService,
47     protected route: ActivatedRoute,
48     protected authService: AuthService,
49     protected notifier: Notifier,
50     protected screenService: ScreenService,
51     private i18n: I18n,
52     private confirmService: ConfirmService,
53     private videoService: VideoService,
54     @Inject(LOCALE_ID) private localeId: string
55   ) {
56     super()
57
58     this.titlePage = this.i18n('My videos')
59   }
60
61   ngOnInit () {
62     super.ngOnInit()
63   }
64
65   ngOnDestroy () {
66     super.ngOnDestroy()
67   }
68
69   abortSelectionMode () {
70     this.checkedVideos = {}
71   }
72
73   isInSelectionMode () {
74     return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
75   }
76
77   getVideosObservable (page: number) {
78     const newPagination = immutableAssign(this.pagination, { currentPage: page })
79
80     return this.videoService.getMyVideos(newPagination, this.sort)
81   }
82
83   generateSyndicationList () {
84     throw new Error('Method not implemented.')
85   }
86
87   async deleteSelectedVideos () {
88     const toDeleteVideosIds = Object.keys(this.checkedVideos)
89                                     .filter(k => this.checkedVideos[ k ] === true)
90                                     .map(k => parseInt(k, 10))
91
92     const res = await this.confirmService.confirm(
93       this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
94       this.i18n('Delete')
95     )
96     if (res === false) return
97
98     const observables: Observable<any>[] = []
99     for (const videoId of toDeleteVideosIds) {
100       const o = this.videoService.removeVideo(videoId)
101                     .pipe(tap(() => this.removeVideoFromArray(videoId)))
102
103       observables.push(o)
104     }
105
106     concat(...observables)
107       .pipe(toArray())
108       .subscribe(
109         () => {
110           this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
111
112           this.abortSelectionMode()
113         },
114
115         err => this.notifier.error(err.message)
116       )
117   }
118
119   async deleteVideo (video: Video) {
120     const res = await this.confirmService.confirm(
121       this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
122       this.i18n('Delete')
123     )
124     if (res === false) return
125
126     this.videoService.removeVideo(video.id)
127         .subscribe(
128           () => {
129             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
130             this.reloadVideos()
131           },
132
133           error => this.notifier.error(error.message)
134         )
135   }
136
137   changeOwnership (event: Event, video: Video) {
138     event.preventDefault()
139     this.videoChangeOwnershipModal.show(video)
140   }
141
142   getStateLabel (video: Video) {
143     let suffix: string
144
145     if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
146       suffix = this.i18n('Published')
147     } else if (video.scheduledUpdate) {
148       const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
149       suffix = this.i18n('Publication scheduled on ') + updateAt
150     } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
151       suffix = this.i18n('Waiting transcoding')
152     } else if (video.state.id === VideoState.TO_TRANSCODE) {
153       suffix = this.i18n('To transcode')
154     } else if (video.state.id === VideoState.TO_IMPORT) {
155       suffix = this.i18n('To import')
156     } else {
157       return ''
158     }
159
160     return ' - ' + suffix
161   }
162
163   private removeVideoFromArray (id: number) {
164     this.videos = this.videos.filter(v => v.id !== id)
165   }
166 }