]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Update CONTRIBUTING.md
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-videos / my-account-videos.component.ts
CommitLineData
489290b8
C
1import { concat, Observable } from 'rxjs'
2import { tap, toArray } from 'rxjs/operators'
3import { Component, Inject, LOCALE_ID, OnDestroy, OnInit, ViewChild } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
0cd4344f
C
5import { immutableAssign } from '@app/shared/misc/utils'
6import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
489290b8 7import { Notifier, ServerService } from '@app/core'
b2731bff 8import { AuthService } from '../../core/auth'
332542bc 9import { ConfirmService } from '../../core/confirm'
be447678 10import { AbstractVideoList } from '../../shared/video/abstract-video-list'
332542bc 11import { Video } from '../../shared/video/video.model'
202f6b6c 12import { VideoService } from '../../shared/video/video.service'
b1d40cff 13import { I18n } from '@ngx-translate/i18n-polyfill'
bbe0f064
C
14import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
15import { ScreenService } from '@app/shared/misc/screen.service'
74d63469 16import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
e2409062 17import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
202f6b6c
C
18
19@Component({
20 selector: 'my-account-videos',
4bb6886d
C
21 templateUrl: './my-account-videos.component.html',
22 styleUrls: [ './my-account-videos.component.scss' ]
202f6b6c 23})
4bb6886d 24export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
489290b8
C
25 @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
26
b1d40cff 27 titlePage: string
ce0e281d 28 checkedVideos: { [ id: number ]: boolean } = {}
0cd4344f 29 pagination: ComponentPagination = {
234b535d 30 currentPage: 1,
2e78e268 31 itemsPerPage: 5,
234b535d
C
32 totalItems: null
33 }
e2409062
C
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 }
202f6b6c 43
b1d40cff
C
44 constructor (
45 protected router: Router,
489290b8 46 protected serverService: ServerService,
b1d40cff
C
47 protected route: ActivatedRoute,
48 protected authService: AuthService,
f8b2c1b4 49 protected notifier: Notifier,
bbe0f064 50 protected screenService: ScreenService,
489290b8 51 private i18n: I18n,
308c4275 52 private confirmService: ConfirmService,
bbe0f064
C
53 private videoService: VideoService,
54 @Inject(LOCALE_ID) private localeId: string
b1d40cff 55 ) {
202f6b6c 56 super()
b1d40cff
C
57
58 this.titlePage = this.i18n('My videos')
202f6b6c
C
59 }
60
61 ngOnInit () {
62 super.ngOnInit()
63 }
64
9af61e84
C
65 ngOnDestroy () {
66 super.ngOnDestroy()
67 }
68
ce0e281d
C
69 abortSelectionMode () {
70 this.checkedVideos = {}
71 }
72
73 isInSelectionMode () {
c199c427 74 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
ce0e281d
C
75 }
76
0cd4344f
C
77 getVideosObservable (page: number) {
78 const newPagination = immutableAssign(this.pagination, { currentPage: page })
79
80 return this.videoService.getMyVideos(newPagination, this.sort)
202f6b6c 81 }
332542bc 82
244e76a5
RK
83 generateSyndicationList () {
84 throw new Error('Method not implemented.')
85 }
86
1f30a185 87 async deleteSelectedVideos () {
ce0e281d 88 const toDeleteVideosIds = Object.keys(this.checkedVideos)
c199c427 89 .filter(k => this.checkedVideos[ k ] === true)
2186386c 90 .map(k => parseInt(k, 10))
ce0e281d 91
2186386c
C
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 )
1f30a185
C
96 if (res === false) return
97
98 const observables: Observable<any>[] = []
99 for (const videoId of toDeleteVideosIds) {
2186386c 100 const o = this.videoService.removeVideo(videoId)
489290b8 101 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
102
103 observables.push(o)
104 }
105
489290b8
C
106 concat(...observables)
107 .pipe(toArray())
1f30a185 108 .subscribe(
489290b8 109 () => {
f8b2c1b4 110 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
2186386c 111
06be7ed0 112 this.abortSelectionMode()
1f30a185
C
113 },
114
f8b2c1b4 115 err => this.notifier.error(err.message)
1f30a185 116 )
ce0e281d
C
117 }
118
1f30a185 119 async deleteVideo (video: Video) {
2186386c
C
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 )
1f30a185
C
124 if (res === false) return
125
126 this.videoService.removeVideo(video.id)
2186386c 127 .subscribe(
f8b2c1b4
C
128 () => {
129 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
2186386c
C
130 this.reloadVideos()
131 },
132
f8b2c1b4 133 error => this.notifier.error(error.message)
2186386c
C
134 )
135 }
1f30a185 136
74d63469
GR
137 changeOwnership (event: Event, video: Video) {
138 event.preventDefault()
139 this.videoChangeOwnershipModal.show(video)
140 }
141
2186386c 142 getStateLabel (video: Video) {
bbe0f064
C
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')
ed31c059
C
154 } else if (video.state.id === VideoState.TO_IMPORT) {
155 suffix = this.i18n('To import')
bbe0f064
C
156 } else {
157 return ''
158 }
2186386c 159
bbe0f064 160 return ' - ' + suffix
332542bc 161 }
ce0e281d 162
489290b8
C
163 private removeVideoFromArray (id: number) {
164 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 165 }
202f6b6c 166}