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