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