Fix method names
[oweals/peertube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
1 import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { I18n } from '@ngx-translate/i18n-polyfill'
3 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
4 import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
5 import { UserService } from '@app/shared/users'
6 import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
7 import { User, UserRight } from '../../../../../shared/models/users'
8 import { Account } from '@app/shared/account/account.model'
9 import { BlocklistService } from '@app/shared/blocklist'
10
11 @Component({
12   selector: 'my-user-moderation-dropdown',
13   templateUrl: './user-moderation-dropdown.component.html'
14 })
15 export class UserModerationDropdownComponent implements OnChanges {
16   @ViewChild('userBanModal') userBanModal: UserBanModalComponent
17
18   @Input() user: User
19   @Input() account: Account
20
21   @Input() buttonSize: 'normal' | 'small' = 'normal'
22   @Input() placement = 'left'
23
24   @Output() userChanged = new EventEmitter()
25   @Output() userDeleted = new EventEmitter()
26
27   userActions: DropdownAction<{ user: User, account: Account }>[][] = []
28
29   constructor (
30     private authService: AuthService,
31     private notifier: Notifier,
32     private confirmService: ConfirmService,
33     private serverService: ServerService,
34     private userService: UserService,
35     private blocklistService: BlocklistService,
36     private i18n: I18n
37   ) { }
38
39   get requiresEmailVerification () {
40     return this.serverService.getConfig().signup.requiresEmailVerification
41   }
42
43   ngOnChanges () {
44     this.buildActions()
45   }
46
47   openBanUserModal (user: User) {
48     if (user.username === 'root') {
49       this.notifier.error(this.i18n('You cannot ban root.'))
50       return
51     }
52
53     this.userBanModal.openModal(user)
54   }
55
56   onUserBanned () {
57     this.userChanged.emit()
58   }
59
60   async unbanUser (user: User) {
61     const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
62     const res = await this.confirmService.confirm(message, this.i18n('Unban'))
63     if (res === false) return
64
65     this.userService.unbanUsers(user)
66         .subscribe(
67           () => {
68             this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
69
70             this.userChanged.emit()
71           },
72
73           err => this.notifier.error(err.message)
74         )
75   }
76
77   async removeUser (user: User) {
78     if (user.username === 'root') {
79       this.notifier.error(this.i18n('You cannot delete root.'))
80       return
81     }
82
83     const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
84     const res = await this.confirmService.confirm(message, this.i18n('Delete'))
85     if (res === false) return
86
87     this.userService.removeUser(user).subscribe(
88       () => {
89         this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
90         this.userDeleted.emit()
91       },
92
93       err => this.notifier.error(err.message)
94     )
95   }
96
97   setEmailAsVerified (user: User) {
98     this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
99       () => {
100         this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
101
102         this.userChanged.emit()
103       },
104
105       err => this.notifier.error(err.message)
106     )
107   }
108
109   blockAccountByUser (account: Account) {
110     this.blocklistService.blockAccountByUser(account)
111         .subscribe(
112           () => {
113             this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
114
115             this.account.mutedByUser = true
116             this.userChanged.emit()
117           },
118
119           err => this.notifier.error(err.message)
120         )
121   }
122
123   unblockAccountByUser (account: Account) {
124     this.blocklistService.unblockAccountByUser(account)
125         .subscribe(
126           () => {
127             this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
128
129             this.account.mutedByUser = false
130             this.userChanged.emit()
131           },
132
133           err => this.notifier.error(err.message)
134         )
135   }
136
137   blockServerByUser (host: string) {
138     this.blocklistService.blockServerByUser(host)
139         .subscribe(
140           () => {
141             this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
142
143             this.account.mutedServerByUser = true
144             this.userChanged.emit()
145           },
146
147           err => this.notifier.error(err.message)
148         )
149   }
150
151   unblockServerByUser (host: string) {
152     this.blocklistService.unblockServerByUser(host)
153         .subscribe(
154           () => {
155             this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
156
157             this.account.mutedServerByUser = false
158             this.userChanged.emit()
159           },
160
161           err => this.notifier.error(err.message)
162         )
163   }
164
165   blockAccountByInstance (account: Account) {
166     this.blocklistService.blockAccountByInstance(account)
167         .subscribe(
168           () => {
169             this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
170
171             this.account.mutedByInstance = true
172             this.userChanged.emit()
173           },
174
175           err => this.notifier.error(err.message)
176         )
177   }
178
179   unblockAccountByInstance (account: Account) {
180     this.blocklistService.unblockAccountByInstance(account)
181         .subscribe(
182           () => {
183             this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
184
185             this.account.mutedByInstance = false
186             this.userChanged.emit()
187           },
188
189           err => this.notifier.error(err.message)
190         )
191   }
192
193   blockServerByInstance (host: string) {
194     this.blocklistService.blockServerByInstance(host)
195         .subscribe(
196           () => {
197             this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
198
199             this.account.mutedServerByInstance = true
200             this.userChanged.emit()
201           },
202
203           err => this.notifier.error(err.message)
204         )
205   }
206
207   unblockServerByInstance (host: string) {
208     this.blocklistService.unblockServerByInstance(host)
209         .subscribe(
210           () => {
211             this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
212
213             this.account.mutedServerByInstance = false
214             this.userChanged.emit()
215           },
216
217           err => this.notifier.error(err.message)
218         )
219   }
220
221   getRouterUserEditLink (user: User) {
222     return [ '/admin', 'users', 'update', user.id ]
223   }
224
225   private buildActions () {
226     this.userActions = []
227
228     if (this.authService.isLoggedIn()) {
229       const authUser = this.authService.getUser()
230
231       if (this.user && authUser.id === this.user.id) return
232
233       if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
234         this.userActions.push([
235           {
236             label: this.i18n('Edit'),
237             linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
238           },
239           {
240             label: this.i18n('Delete'),
241             handler: ({ user }) => this.removeUser(user)
242           },
243           {
244             label: this.i18n('Ban'),
245             handler: ({ user }) => this.openBanUserModal(user),
246             isDisplayed: ({ user }) => !user.blocked
247           },
248           {
249             label: this.i18n('Unban'),
250             handler: ({ user }) => this.unbanUser(user),
251             isDisplayed: ({ user }) => user.blocked
252           },
253           {
254             label: this.i18n('Set Email as Verified'),
255             handler: ({ user }) => this.setEmailAsVerified(user),
256             isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
257           }
258         ])
259       }
260
261       // Actions on accounts/servers
262       if (this.account) {
263         // User actions
264         this.userActions.push([
265           {
266             label: this.i18n('Mute this account'),
267             isDisplayed: ({ account }) => account.mutedByUser === false,
268             handler: ({ account }) => this.blockAccountByUser(account)
269           },
270           {
271             label: this.i18n('Unmute this account'),
272             isDisplayed: ({ account }) => account.mutedByUser === true,
273             handler: ({ account }) => this.unblockAccountByUser(account)
274           },
275           {
276             label: this.i18n('Mute the instance'),
277             isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
278             handler: ({ account }) => this.blockServerByUser(account.host)
279           },
280           {
281             label: this.i18n('Unmute the instance'),
282             isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
283             handler: ({ account }) => this.unblockServerByUser(account.host)
284           }
285         ])
286
287         let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
288
289         // Instance actions
290         if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
291           instanceActions = instanceActions.concat([
292             {
293               label: this.i18n('Mute this account by your instance'),
294               isDisplayed: ({ account }) => account.mutedByInstance === false,
295               handler: ({ account }) => this.blockAccountByInstance(account)
296             },
297             {
298               label: this.i18n('Unmute this account by your instance'),
299               isDisplayed: ({ account }) => account.mutedByInstance === true,
300               handler: ({ account }) => this.unblockAccountByInstance(account)
301             }
302           ])
303         }
304
305         // Instance actions
306         if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
307           instanceActions = instanceActions.concat([
308             {
309               label: this.i18n('Mute the instance by your instance'),
310               isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
311               handler: ({ account }) => this.blockServerByInstance(account.host)
312             },
313             {
314               label: this.i18n('Unmute the instance by your instance'),
315               isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
316               handler: ({ account }) => this.unblockServerByInstance(account.host)
317             }
318           ])
319         }
320
321         if (instanceActions.length !== 0) {
322           this.userActions.push(instanceActions)
323         }
324       }
325     }
326   }
327 }