]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Add ability to schedule video publication
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
CommitLineData
db400f44
C
1import { from as observableFrom, Observable } from 'rxjs'
2import { concatAll, tap } from 'rxjs/operators'
bbe0f064 3import { Component, OnDestroy, OnInit, Inject, LOCALE_ID } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
2a2c19df 5import { Location } from '@angular/common'
0cd4344f
C
6import { immutableAssign } from '@app/shared/misc/utils'
7import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
202f6b6c 8import { NotificationsService } from 'angular2-notifications'
b2731bff 9import { AuthService } from '../../core/auth'
332542bc 10import { ConfirmService } from '../../core/confirm'
be447678 11import { AbstractVideoList } from '../../shared/video/abstract-video-list'
332542bc 12import { Video } from '../../shared/video/video.model'
202f6b6c 13import { VideoService } from '../../shared/video/video.service'
b1d40cff 14import { I18n } from '@ngx-translate/i18n-polyfill'
bbe0f064
C
15import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
16import { ScreenService } from '@app/shared/misc/screen.service'
202f6b6c
C
17
18@Component({
19 selector: 'my-account-videos',
4bb6886d
C
20 templateUrl: './my-account-videos.component.html',
21 styleUrls: [ './my-account-videos.component.scss' ]
202f6b6c 22})
4bb6886d 23export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
b1d40cff 24 titlePage: string
4bb6886d 25 currentRoute = '/my-account/videos'
ce0e281d 26 checkedVideos: { [ id: number ]: boolean } = {}
0cd4344f 27 pagination: ComponentPagination = {
234b535d 28 currentPage: 1,
2e78e268 29 itemsPerPage: 5,
234b535d
C
30 totalItems: null
31 }
202f6b6c 32
9af61e84
C
33 protected baseVideoWidth = -1
34 protected baseVideoHeight = 155
35
b1d40cff
C
36 constructor (
37 protected router: Router,
38 protected route: ActivatedRoute,
39 protected authService: AuthService,
40 protected notificationsService: NotificationsService,
41 protected confirmService: ConfirmService,
42 protected location: Location,
bbe0f064 43 protected screenService: ScreenService,
b1d40cff 44 protected i18n: I18n,
bbe0f064
C
45 private videoService: VideoService,
46 @Inject(LOCALE_ID) private localeId: string
b1d40cff 47 ) {
202f6b6c 48 super()
b1d40cff
C
49
50 this.titlePage = this.i18n('My videos')
202f6b6c
C
51 }
52
53 ngOnInit () {
54 super.ngOnInit()
55 }
56
9af61e84
C
57 ngOnDestroy () {
58 super.ngOnDestroy()
59 }
60
ce0e281d
C
61 abortSelectionMode () {
62 this.checkedVideos = {}
63 }
64
65 isInSelectionMode () {
2186386c 66 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
ce0e281d
C
67 }
68
0cd4344f
C
69 getVideosObservable (page: number) {
70 const newPagination = immutableAssign(this.pagination, { currentPage: page })
71
72 return this.videoService.getMyVideos(newPagination, this.sort)
202f6b6c 73 }
332542bc 74
244e76a5
RK
75 generateSyndicationList () {
76 throw new Error('Method not implemented.')
77 }
78
1f30a185 79 async deleteSelectedVideos () {
ce0e281d 80 const toDeleteVideosIds = Object.keys(this.checkedVideos)
2186386c
C
81 .filter(k => this.checkedVideos[ k ] === true)
82 .map(k => parseInt(k, 10))
ce0e281d 83
2186386c
C
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 )
1f30a185
C
88 if (res === false) return
89
90 const observables: Observable<any>[] = []
91 for (const videoId of toDeleteVideosIds) {
2186386c 92 const o = this.videoService.removeVideo(videoId)
db400f44 93 .pipe(tap(() => this.spliceVideosById(videoId)))
1f30a185
C
94
95 observables.push(o)
96 }
97
2186386c
C
98 observableFrom(observables)
99 .pipe(concatAll())
1f30a185
C
100 .subscribe(
101 res => {
2186386c
C
102 this.notificationsService.success(
103 this.i18n('Success'),
104 this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })
105 )
106
06be7ed0
C
107 this.abortSelectionMode()
108 this.reloadVideos()
1f30a185
C
109 },
110
2186386c 111 err => this.notificationsService.error(this.i18n('Error'), err.message)
1f30a185 112 )
ce0e281d
C
113 }
114
1f30a185 115 async deleteVideo (video: Video) {
2186386c
C
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 )
1f30a185
C
120 if (res === false) return
121
122 this.videoService.removeVideo(video.id)
2186386c
C
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 }
1f30a185 135
2186386c 136 getStateLabel (video: Video) {
bbe0f064
C
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 {
149 return ''
150 }
2186386c 151
bbe0f064 152 return ' - ' + suffix
332542bc 153 }
ce0e281d 154
a86887a4
C
155 protected buildVideoHeight () {
156 // In account videos, the video height is fixed
157 return this.baseVideoHeight
158 }
159
ce0e281d 160 private spliceVideosById (id: number) {
0cd4344f 161 for (const key of Object.keys(this.loadedPages)) {
2186386c 162 const videos = this.loadedPages[ key ]
0cd4344f
C
163 const index = videos.findIndex(v => v.id === id)
164
165 if (index !== -1) {
166 videos.splice(index, 1)
167 return
168 }
169 }
ce0e281d 170 }
202f6b6c 171}