]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-videos/my-account-videos.component.ts
Refractor notification service
[github/Chocobozzz/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, ViewChild } 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 { Notifier } from '@app/core'
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 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.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 titlePage: string
26 currentRoute = '/my-account/videos'
27 checkedVideos: { [ id: number ]: boolean } = {}
28 pagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 5,
31 totalItems: null
32 }
33
34 protected baseVideoWidth = -1
35 protected baseVideoHeight = 155
36
37 @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
38
39 constructor (
40 protected router: Router,
41 protected route: ActivatedRoute,
42 protected authService: AuthService,
43 protected notifier: Notifier,
44 protected location: Location,
45 protected screenService: ScreenService,
46 protected i18n: I18n,
47 private confirmService: ConfirmService,
48 private videoService: VideoService,
49 @Inject(LOCALE_ID) private localeId: string
50 ) {
51 super()
52
53 this.titlePage = this.i18n('My videos')
54 }
55
56 ngOnInit () {
57 super.ngOnInit()
58 }
59
60 ngOnDestroy () {
61 super.ngOnDestroy()
62 }
63
64 abortSelectionMode () {
65 this.checkedVideos = {}
66 }
67
68 isInSelectionMode () {
69 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
70 }
71
72 getVideosObservable (page: number) {
73 const newPagination = immutableAssign(this.pagination, { currentPage: page })
74
75 return this.videoService.getMyVideos(newPagination, this.sort)
76 }
77
78 generateSyndicationList () {
79 throw new Error('Method not implemented.')
80 }
81
82 async deleteSelectedVideos () {
83 const toDeleteVideosIds = Object.keys(this.checkedVideos)
84 .filter(k => this.checkedVideos[ k ] === true)
85 .map(k => parseInt(k, 10))
86
87 const res = await this.confirmService.confirm(
88 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
89 this.i18n('Delete')
90 )
91 if (res === false) return
92
93 const observables: Observable<any>[] = []
94 for (const videoId of toDeleteVideosIds) {
95 const o = this.videoService.removeVideo(videoId)
96 .pipe(tap(() => this.spliceVideosById(videoId)))
97
98 observables.push(o)
99 }
100
101 observableFrom(observables)
102 .pipe(concatAll())
103 .subscribe(
104 res => {
105 this.notifier.success(this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }))
106
107 this.abortSelectionMode()
108 this.reloadVideos()
109 },
110
111 err => this.notifier.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 () => {
125 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: video.name }))
126 this.reloadVideos()
127 },
128
129 error => this.notifier.error(error.message)
130 )
131 }
132
133 changeOwnership (event: Event, video: Video) {
134 event.preventDefault()
135 this.videoChangeOwnershipModal.show(video)
136 }
137
138 getStateLabel (video: Video) {
139 let suffix: string
140
141 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
142 suffix = this.i18n('Published')
143 } else if (video.scheduledUpdate) {
144 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
145 suffix = this.i18n('Publication scheduled on ') + updateAt
146 } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
147 suffix = this.i18n('Waiting transcoding')
148 } else if (video.state.id === VideoState.TO_TRANSCODE) {
149 suffix = this.i18n('To transcode')
150 } else if (video.state.id === VideoState.TO_IMPORT) {
151 suffix = this.i18n('To import')
152 } else {
153 return ''
154 }
155
156 return ' - ' + suffix
157 }
158
159 protected buildVideoHeight () {
160 // In account videos, the video height is fixed
161 return this.baseVideoHeight
162 }
163
164 private spliceVideosById (id: number) {
165 for (const key of Object.keys(this.loadedPages)) {
166 const videos: Video[] = this.loadedPages[ key ]
167 const index = videos.findIndex(v => v.id === id)
168
169 if (index !== -1) {
170 videos.splice(index, 1)
171 return
172 }
173 }
174 }
175 }