]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video-actions-dropdown.component.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video-actions-dropdown.component.ts
CommitLineData
72675ebe 1import { AfterViewInit, Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
3a0fb65c
C
2import { I18n } from '@ngx-translate/i18n-polyfill'
3import { DropdownAction, DropdownButtonSize, DropdownDirection } from '@app/shared/buttons/action-dropdown.component'
4import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
5import { BlocklistService } from '@app/shared/blocklist'
6import { Video } from '@app/shared/video/video.model'
7import { VideoService } from '@app/shared/video/video.service'
8import { VideoDetails } from '@app/shared/video/video-details.model'
9import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
10import { VideoAddToPlaylistComponent } from '@app/shared/video-playlist/video-add-to-playlist.component'
11import { VideoDownloadComponent } from '@app/shared/video/modals/video-download.component'
12import { VideoReportComponent } from '@app/shared/video/modals/video-report.component'
13import { VideoBlacklistComponent } from '@app/shared/video/modals/video-blacklist.component'
14import { VideoBlacklistService } from '@app/shared/video-blacklist'
15import { ScreenService } from '@app/shared/misc/screen.service'
8ba9c205 16import { VideoCaption } from '@shared/models'
b764380a 17import { RedundancyService } from '@app/shared/video/redundancy.service'
3a0fb65c
C
18
19export type VideoActionsDisplayType = {
20 playlist?: boolean
21 download?: boolean
22 update?: boolean
23 blacklist?: boolean
24 delete?: boolean
25 report?: boolean
b764380a 26 duplicate?: boolean
3a0fb65c
C
27}
28
29@Component({
30 selector: 'my-video-actions-dropdown',
31 templateUrl: './video-actions-dropdown.component.html',
32 styleUrls: [ './video-actions-dropdown.component.scss' ]
33})
4da22f64 34export class VideoActionsDropdownComponent implements OnChanges {
f36da21e
C
35 @ViewChild('playlistDropdown', { static: false }) playlistDropdown: NgbDropdown
36 @ViewChild('playlistAdd', { static: false }) playlistAdd: VideoAddToPlaylistComponent
3a0fb65c 37
f36da21e
C
38 @ViewChild('videoDownloadModal', { static: false }) videoDownloadModal: VideoDownloadComponent
39 @ViewChild('videoReportModal', { static: false }) videoReportModal: VideoReportComponent
40 @ViewChild('videoBlacklistModal', { static: false }) videoBlacklistModal: VideoBlacklistComponent
3a0fb65c
C
41
42 @Input() video: Video | VideoDetails
8ba9c205 43 @Input() videoCaptions: VideoCaption[] = []
3a0fb65c
C
44
45 @Input() displayOptions: VideoActionsDisplayType = {
46 playlist: false,
47 download: true,
48 update: true,
49 blacklist: true,
50 delete: true,
b764380a
C
51 report: true,
52 duplicate: true
3a0fb65c 53 }
8dfceec4 54 @Input() placement = 'left'
3a0fb65c
C
55
56 @Input() label: string
57
58 @Input() buttonStyled = false
59 @Input() buttonSize: DropdownButtonSize = 'normal'
60 @Input() buttonDirection: DropdownDirection = 'vertical'
61
62 @Output() videoRemoved = new EventEmitter()
63 @Output() videoUnblacklisted = new EventEmitter()
64 @Output() videoBlacklisted = new EventEmitter()
689a4f69 65 @Output() modalOpened = new EventEmitter()
3a0fb65c
C
66
67 videoActions: DropdownAction<{ video: Video }>[][] = []
68
69 private loaded = false
70
71 constructor (
72 private authService: AuthService,
73 private notifier: Notifier,
74 private confirmService: ConfirmService,
75 private videoBlacklistService: VideoBlacklistService,
76 private serverService: ServerService,
77 private screenService: ScreenService,
78 private videoService: VideoService,
79 private blocklistService: BlocklistService,
b764380a 80 private redundancyService: RedundancyService,
3a0fb65c
C
81 private i18n: I18n
82 ) { }
83
84 get user () {
85 return this.authService.getUser()
86 }
87
88 ngOnChanges () {
1c8ddbfa
C
89 if (this.loaded) {
90 this.loaded = false
78d60e63 91 this.playlistAdd.reload()
1c8ddbfa
C
92 }
93
3a0fb65c
C
94 this.buildActions()
95 }
96
97 isUserLoggedIn () {
98 return this.authService.isLoggedIn()
99 }
100
101 loadDropdownInformation () {
102 if (!this.isUserLoggedIn() || this.loaded === true) return
103
104 this.loaded = true
105
106 if (this.displayOptions.playlist) this.playlistAdd.load()
107 }
108
109 /* Show modals */
110
111 showDownloadModal () {
689a4f69
C
112 this.modalOpened.emit()
113
8ba9c205 114 this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions)
3a0fb65c
C
115 }
116
117 showReportModal () {
689a4f69
C
118 this.modalOpened.emit()
119
3a0fb65c
C
120 this.videoReportModal.show()
121 }
122
123 showBlacklistModal () {
689a4f69
C
124 this.modalOpened.emit()
125
3a0fb65c
C
126 this.videoBlacklistModal.show()
127 }
128
129 /* Actions checker */
130
131 isVideoUpdatable () {
132 return this.video.isUpdatableBy(this.user)
133 }
134
135 isVideoRemovable () {
136 return this.video.isRemovableBy(this.user)
137 }
138
139 isVideoBlacklistable () {
140 return this.video.isBlackistableBy(this.user)
141 }
142
143 isVideoUnblacklistable () {
144 return this.video.isUnblacklistableBy(this.user)
145 }
146
72675ebe
C
147 isVideoDownloadable () {
148 return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled
149 }
150
b764380a
C
151 canVideoBeDuplicated () {
152 return this.video.canBeDuplicatedBy(this.user)
153 }
154
3a0fb65c
C
155 /* Action handlers */
156
157 async unblacklistVideo () {
158 const confirmMessage = this.i18n(
159 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
160 )
161
162 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
163 if (res === false) return
164
165 this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
166 () => {
167 this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
168
169 this.video.blacklisted = false
170 this.video.blacklistedReason = null
171
172 this.videoUnblacklisted.emit()
173 },
174
175 err => this.notifier.error(err.message)
176 )
177 }
178
179 async removeVideo () {
689a4f69
C
180 this.modalOpened.emit()
181
3a0fb65c
C
182 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
183 if (res === false) return
184
185 this.videoService.removeVideo(this.video.id)
186 .subscribe(
187 () => {
188 this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
189
190 this.videoRemoved.emit()
191 },
192
193 error => this.notifier.error(error.message)
194 )
195 }
196
b764380a
C
197 duplicateVideo () {
198 this.redundancyService.addVideoRedundancy(this.video)
199 .subscribe(
200 () => {
201 const message = this.i18n('This video will be duplicated by your instance.')
202 this.notifier.success(message)
203 },
204
205 err => this.notifier.error(err.message)
206 )
207 }
208
3a0fb65c
C
209 onVideoBlacklisted () {
210 this.videoBlacklisted.emit()
211 }
212
213 getPlaylistDropdownPlacement () {
214 if (this.screenService.isInSmallView()) {
215 return 'bottom-right'
216 }
217
218 return 'bottom-left bottom-right'
219 }
220
221 private buildActions () {
f238aec5
C
222 this.videoActions = [
223 [
3a0fb65c
C
224 {
225 label: this.i18n('Save to playlist'),
226 handler: () => this.playlistDropdown.toggle(),
f238aec5 227 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist,
3a0fb65c
C
228 iconName: 'playlist-add'
229 }
f238aec5
C
230 ],
231 [
3a0fb65c
C
232 {
233 label: this.i18n('Download'),
234 handler: () => this.showDownloadModal(),
72675ebe 235 isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
3a0fb65c
C
236 iconName: 'download'
237 },
238 {
239 label: this.i18n('Update'),
240 linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
241 iconName: 'edit',
f238aec5 242 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable()
3a0fb65c
C
243 },
244 {
245 label: this.i18n('Blacklist'),
246 handler: () => this.showBlacklistModal(),
247 iconName: 'no',
f238aec5 248 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlacklistable()
3a0fb65c
C
249 },
250 {
251 label: this.i18n('Unblacklist'),
252 handler: () => this.unblacklistVideo(),
253 iconName: 'undo',
f238aec5 254 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblacklistable()
3a0fb65c 255 },
b764380a
C
256 {
257 label: this.i18n('Duplicate (redundancy)'),
258 handler: () => this.duplicateVideo(),
259 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.duplicate && this.canVideoBeDuplicated(),
260 iconName: 'cloud-download'
261 },
3a0fb65c
C
262 {
263 label: this.i18n('Delete'),
264 handler: () => this.removeVideo(),
f238aec5 265 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(),
3a0fb65c
C
266 iconName: 'delete'
267 }
f238aec5
C
268 ],
269 [
3a0fb65c
C
270 {
271 label: this.i18n('Report'),
272 handler: () => this.showReportModal(),
f238aec5 273 isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report,
3a0fb65c
C
274 iconName: 'alert'
275 }
f238aec5
C
276 ]
277 ]
3a0fb65c
C
278 }
279}