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