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