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