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