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