]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
preserve original variable names server-side
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
1 import { Component, EventEmitter, Input, OnChanges, OnInit, 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 import { ServerConfig, BulkRemoveCommentsOfBody } from '@shared/models'
11 import { BulkService } from '../bulk/bulk.service'
12
13 @Component({
14 selector: 'my-user-moderation-dropdown',
15 templateUrl: './user-moderation-dropdown.component.html'
16 })
17 export class UserModerationDropdownComponent implements OnInit, OnChanges {
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
20 @Input() user: User
21 @Input() account: Account
22
23 @Input() buttonSize: 'normal' | 'small' = 'normal'
24 @Input() placement = 'left-top left-bottom auto'
25 @Input() label: string
26 @Input() container: 'body' | undefined = undefined
27
28 @Output() userChanged = new EventEmitter()
29 @Output() userDeleted = new EventEmitter()
30
31 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
32
33 private serverConfig: ServerConfig
34
35 constructor (
36 private authService: AuthService,
37 private notifier: Notifier,
38 private confirmService: ConfirmService,
39 private serverService: ServerService,
40 private userService: UserService,
41 private blocklistService: BlocklistService,
42 private bulkService: BulkService,
43 private i18n: I18n
44 ) { }
45
46 get requiresEmailVerification () {
47 return this.serverConfig.signup.requiresEmailVerification
48 }
49
50 ngOnInit (): void {
51 this.serverConfig = this.serverService.getTmpConfig()
52 this.serverService.getConfig()
53 .subscribe(config => this.serverConfig = config)
54 }
55
56 ngOnChanges () {
57 this.buildActions()
58 }
59
60 openBanUserModal (user: User) {
61 if (user.username === 'root') {
62 this.notifier.error(this.i18n('You cannot ban root.'))
63 return
64 }
65
66 this.userBanModal.openModal(user)
67 }
68
69 onUserBanned () {
70 this.userChanged.emit()
71 }
72
73 async unbanUser (user: User) {
74 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
75 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
76 if (res === false) return
77
78 this.userService.unbanUsers(user)
79 .subscribe(
80 () => {
81 this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
82
83 this.userChanged.emit()
84 },
85
86 err => this.notifier.error(err.message)
87 )
88 }
89
90 async removeUser (user: User) {
91 if (user.username === 'root') {
92 this.notifier.error(this.i18n('You cannot delete root.'))
93 return
94 }
95
96 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
97 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
98 if (res === false) return
99
100 this.userService.removeUser(user).subscribe(
101 () => {
102 this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
103 this.userDeleted.emit()
104 },
105
106 err => this.notifier.error(err.message)
107 )
108 }
109
110 setEmailAsVerified (user: User) {
111 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
112 () => {
113 this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
114
115 this.userChanged.emit()
116 },
117
118 err => this.notifier.error(err.message)
119 )
120 }
121
122 blockAccountByUser (account: Account) {
123 this.blocklistService.blockAccountByUser(account)
124 .subscribe(
125 () => {
126 this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
127
128 this.account.mutedByUser = true
129 this.userChanged.emit()
130 },
131
132 err => this.notifier.error(err.message)
133 )
134 }
135
136 unblockAccountByUser (account: Account) {
137 this.blocklistService.unblockAccountByUser(account)
138 .subscribe(
139 () => {
140 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
141
142 this.account.mutedByUser = false
143 this.userChanged.emit()
144 },
145
146 err => this.notifier.error(err.message)
147 )
148 }
149
150 blockServerByUser (host: string) {
151 this.blocklistService.blockServerByUser(host)
152 .subscribe(
153 () => {
154 this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
155
156 this.account.mutedServerByUser = true
157 this.userChanged.emit()
158 },
159
160 err => this.notifier.error(err.message)
161 )
162 }
163
164 unblockServerByUser (host: string) {
165 this.blocklistService.unblockServerByUser(host)
166 .subscribe(
167 () => {
168 this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
169
170 this.account.mutedServerByUser = false
171 this.userChanged.emit()
172 },
173
174 err => this.notifier.error(err.message)
175 )
176 }
177
178 blockAccountByInstance (account: Account) {
179 this.blocklistService.blockAccountByInstance(account)
180 .subscribe(
181 () => {
182 this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
183
184 this.account.mutedByInstance = true
185 this.userChanged.emit()
186 },
187
188 err => this.notifier.error(err.message)
189 )
190 }
191
192 unblockAccountByInstance (account: Account) {
193 this.blocklistService.unblockAccountByInstance(account)
194 .subscribe(
195 () => {
196 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
197
198 this.account.mutedByInstance = false
199 this.userChanged.emit()
200 },
201
202 err => this.notifier.error(err.message)
203 )
204 }
205
206 blockServerByInstance (host: string) {
207 this.blocklistService.blockServerByInstance(host)
208 .subscribe(
209 () => {
210 this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
211
212 this.account.mutedServerByInstance = true
213 this.userChanged.emit()
214 },
215
216 err => this.notifier.error(err.message)
217 )
218 }
219
220 unblockServerByInstance (host: string) {
221 this.blocklistService.unblockServerByInstance(host)
222 .subscribe(
223 () => {
224 this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
225
226 this.account.mutedServerByInstance = false
227 this.userChanged.emit()
228 },
229
230 err => this.notifier.error(err.message)
231 )
232 }
233
234 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
235 const message = this.i18n('Are you sure you want to remove all the comments of this account?')
236 const res = await this.confirmService.confirm(message, this.i18n('Delete account comments'))
237 if (res === false) return
238
239 this.bulkService.removeCommentsOf(body)
240 .subscribe(
241 () => {
242 this.notifier.success(this.i18n('Will remove comments of this account (may take several minutes).'))
243 },
244
245 err => this.notifier.error(err.message)
246 )
247 }
248
249 getRouterUserEditLink (user: User) {
250 return [ '/admin', 'users', 'update', user.id ]
251 }
252
253 private buildActions () {
254 this.userActions = []
255
256 if (this.authService.isLoggedIn()) {
257 const authUser = this.authService.getUser()
258
259 if (this.user && authUser.id === this.user.id) return
260
261 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
262 this.userActions.push([
263 {
264 label: this.i18n('Edit user'),
265 description: this.i18n('Change quota, role, and more.'),
266 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
267 },
268 {
269 label: this.i18n('Delete user'),
270 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
271 handler: ({ user }) => this.removeUser(user)
272 },
273 {
274 label: this.i18n('Ban'),
275 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
276 handler: ({ user }) => this.openBanUserModal(user),
277 isDisplayed: ({ user }) => !user.blocked
278 },
279 {
280 label: this.i18n('Unban user'),
281 description: this.i18n('Allow the user to login and create videos/comments again'),
282 handler: ({ user }) => this.unbanUser(user),
283 isDisplayed: ({ user }) => user.blocked
284 },
285 {
286 label: this.i18n('Set Email as Verified'),
287 handler: ({ user }) => this.setEmailAsVerified(user),
288 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
289 }
290 ])
291 }
292
293 // Actions on accounts/servers
294 if (this.account) {
295 // User actions
296 this.userActions.push([
297 {
298 label: this.i18n('Mute this account'),
299 description: this.i18n('Hide any content from that user for you.'),
300 isDisplayed: ({ account }) => account.mutedByUser === false,
301 handler: ({ account }) => this.blockAccountByUser(account)
302 },
303 {
304 label: this.i18n('Unmute this account'),
305 description: this.i18n('Show back content from that user for you.'),
306 isDisplayed: ({ account }) => account.mutedByUser === true,
307 handler: ({ account }) => this.unblockAccountByUser(account)
308 },
309 {
310 label: this.i18n('Mute the instance'),
311 description: this.i18n('Hide any content from that instance for you.'),
312 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
313 handler: ({ account }) => this.blockServerByUser(account.host)
314 },
315 {
316 label: this.i18n('Unmute the instance'),
317 description: this.i18n('Show back content from that instance for you.'),
318 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
319 handler: ({ account }) => this.unblockServerByUser(account.host)
320 },
321 {
322 label: this.i18n('Remove comments from your videos'),
323 description: this.i18n('Remove comments of this account from your videos.'),
324 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
325 }
326 ])
327
328 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
329
330 // Instance actions on account blocklists
331 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
332 instanceActions = instanceActions.concat([
333 {
334 label: this.i18n('Mute this account by your instance'),
335 description: this.i18n('Hide any content from that user for you, your instance and its users.'),
336 isDisplayed: ({ account }) => account.mutedByInstance === false,
337 handler: ({ account }) => this.blockAccountByInstance(account)
338 },
339 {
340 label: this.i18n('Unmute this account by your instance'),
341 description: this.i18n('Show back content from that user for you, your instance and its users.'),
342 isDisplayed: ({ account }) => account.mutedByInstance === true,
343 handler: ({ account }) => this.unblockAccountByInstance(account)
344 }
345 ])
346 }
347
348 // Instance actions on server blocklists
349 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
350 instanceActions = instanceActions.concat([
351 {
352 label: this.i18n('Mute the instance by your instance'),
353 description: this.i18n('Hide any content from that instance for you, your instance and its users.'),
354 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
355 handler: ({ account }) => this.blockServerByInstance(account.host)
356 },
357 {
358 label: this.i18n('Unmute the instance by your instance'),
359 description: this.i18n('Show back content from that instance for you, your instance and its users.'),
360 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
361 handler: ({ account }) => this.unblockServerByInstance(account.host)
362 }
363 ])
364 }
365
366 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
367 instanceActions = instanceActions.concat([
368 {
369 label: this.i18n('Remove comments from your instance'),
370 description: this.i18n('Remove comments of this account from your instance.'),
371 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
372 }
373 ])
374 }
375
376 if (instanceActions.length !== 0) {
377 this.userActions.push(instanceActions)
378 }
379 }
380 }
381 }
382 }