]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
Add mute status in account and channel pages
[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 next: () => {
72 this.notifier.success($localize`User ${user.username} unbanned.`)
73 this.userChanged.emit()
74 },
75
76 error: 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 user ${user.username}, you won't be able to create another with the same username!`
87 const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`)
88 if (res === false) return
89
90 this.userService.removeUser(user)
91 .subscribe({
92 next: () => {
93 this.notifier.success($localize`User ${user.username} deleted.`)
94 this.userDeleted.emit()
95 },
96
97 error: err => this.notifier.error(err.message)
98 })
99 }
100
101 setEmailAsVerified (user: User) {
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 })
111 }
112
113 blockAccountByUser (account: Account) {
114 this.blocklistService.blockAccountByUser(account)
115 .subscribe({
116 next: () => {
117 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
118
119 this.account.mutedByUser = true
120 this.userChanged.emit()
121 },
122
123 error: err => this.notifier.error(err.message)
124 })
125 }
126
127 unblockAccountByUser (account: Account) {
128 this.blocklistService.unblockAccountByUser(account)
129 .subscribe({
130 next: () => {
131 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
132
133 this.account.mutedByUser = false
134 this.userChanged.emit()
135 },
136
137 error: err => this.notifier.error(err.message)
138 })
139 }
140
141 blockServerByUser (host: string) {
142 this.blocklistService.blockServerByUser(host)
143 .subscribe({
144 next: () => {
145 this.notifier.success($localize`Instance ${host} muted.`)
146
147 this.account.mutedServerByUser = true
148 this.userChanged.emit()
149 },
150
151 error: err => this.notifier.error(err.message)
152 })
153 }
154
155 unblockServerByUser (host: string) {
156 this.blocklistService.unblockServerByUser(host)
157 .subscribe({
158 next: () => {
159 this.notifier.success($localize`Instance ${host} unmuted.`)
160
161 this.account.mutedServerByUser = false
162 this.userChanged.emit()
163 },
164
165 error: err => this.notifier.error(err.message)
166 })
167 }
168
169 blockAccountByInstance (account: Account) {
170 this.blocklistService.blockAccountByInstance(account)
171 .subscribe({
172 next: () => {
173 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
174
175 this.account.mutedByInstance = true
176 this.userChanged.emit()
177 },
178
179 error: err => this.notifier.error(err.message)
180 })
181 }
182
183 unblockAccountByInstance (account: Account) {
184 this.blocklistService.unblockAccountByInstance(account)
185 .subscribe({
186 next: () => {
187 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
188
189 this.account.mutedByInstance = false
190 this.userChanged.emit()
191 },
192
193 error: err => this.notifier.error(err.message)
194 })
195 }
196
197 blockServerByInstance (host: string) {
198 this.blocklistService.blockServerByInstance(host)
199 .subscribe({
200 next: () => {
201 this.notifier.success($localize`Instance ${host} muted by the instance.`)
202
203 this.account.mutedServerByInstance = true
204 this.userChanged.emit()
205 },
206
207 error: err => this.notifier.error(err.message)
208 })
209 }
210
211 unblockServerByInstance (host: string) {
212 this.blocklistService.unblockServerByInstance(host)
213 .subscribe({
214 next: () => {
215 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
216
217 this.account.mutedServerByInstance = false
218 this.userChanged.emit()
219 },
220
221 error: err => this.notifier.error(err.message)
222 })
223 }
224
225 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
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`)
228 if (res === false) return
229
230 this.bulkService.removeCommentsOf(body)
231 .subscribe({
232 next: () => {
233 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
234 },
235
236 error: err => this.notifier.error(err.message)
237 })
238 }
239
240 getRouterUserEditLink (user: User) {
241 return [ '/admin', 'users', 'update', user.id ]
242 }
243
244 private isMyUser (user: User) {
245 return user && this.authService.getUser().id === user.id
246 }
247
248 private isMyAccount (account: Account) {
249 return account && this.authService.getUser().account.id === account.id
250 }
251
252 private buildActions () {
253 this.userActions = []
254
255 if (this.prependActions && this.prependActions.length !== 0) {
256 this.userActions = [
257 this.prependActions
258 ]
259 }
260
261 const myAccountModerationActions = this.buildMyAccountModerationActions()
262 const instanceModerationActions = this.buildInstanceModerationActions()
263
264 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
265 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
266 }
267
268 private buildMyAccountModerationActions () {
269 if (!this.account || !this.authService.isLoggedIn()) return []
270
271 const myAccountActions: DropdownAction<{ user: User, account: Account }>[] = [
272 {
273 label: $localize`My account moderation`,
274 class: [ 'red' ],
275 isHeader: true
276 },
277 {
278 label: $localize`Mute this account`,
279 description: $localize`Hide any content from that user from you.`,
280 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false,
281 handler: ({ account }) => this.blockAccountByUser(account)
282 },
283 {
284 label: $localize`Unmute this account`,
285 description: $localize`Show back content from that user for you.`,
286 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true,
287 handler: ({ account }) => this.unblockAccountByUser(account)
288 },
289 {
290 label: $localize`Mute the instance`,
291 description: $localize`Hide any content from that instance for you.`,
292 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
293 handler: ({ account }) => this.blockServerByUser(account.host)
294 },
295 {
296 label: $localize`Unmute the instance`,
297 description: $localize`Show back content from that instance for you.`,
298 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
299 handler: ({ account }) => this.unblockServerByUser(account.host)
300 },
301 {
302 label: $localize`Remove comments from your videos`,
303 description: $localize`Remove comments made by this account on your videos.`,
304 isDisplayed: ({ account }) => !this.isMyAccount(account),
305 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
306 }
307 ]
308
309 return myAccountActions
310 }
311
312 private buildInstanceModerationActions () {
313 if (!this.authService.isLoggedIn()) return []
314
315 const authUser = this.authService.getUser()
316
317 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
318
319 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
320 instanceActions = instanceActions.concat([
321 {
322 label: $localize`Edit user`,
323 description: $localize`Change quota, role, and more.`,
324 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
325 },
326 {
327 label: $localize`Delete user`,
328 description: $localize`Videos will be deleted, comments will be tombstoned.`,
329 isDisplayed: ({ user }) => !this.isMyUser(user),
330 handler: ({ user }) => this.removeUser(user)
331 },
332 {
333 label: $localize`Ban`,
334 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
335 handler: ({ user }) => this.openBanUserModal(user),
336 isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked
337 },
338 {
339 label: $localize`Unban user`,
340 description: $localize`Allow the user to login and create videos/comments again`,
341 handler: ({ user }) => this.unbanUser(user),
342 isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked
343 },
344 {
345 label: $localize`Set Email as Verified`,
346 handler: ({ user }) => this.setEmailAsVerified(user),
347 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
348 }
349 ])
350 }
351
352 // Instance actions on account blocklists
353 if (this.account && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
354 instanceActions = instanceActions.concat([
355 {
356 label: $localize`Mute this account`,
357 description: $localize`Hide any content from that user from you, your instance and its users.`,
358 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false,
359 handler: ({ account }) => this.blockAccountByInstance(account)
360 },
361 {
362 label: $localize`Unmute this account`,
363 description: $localize`Show this user's content to the users of this instance again.`,
364 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true,
365 handler: ({ account }) => this.unblockAccountByInstance(account)
366 }
367 ])
368 }
369
370 // Instance actions on server blocklists
371 if (this.account && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
372 instanceActions = instanceActions.concat([
373 {
374 label: $localize`Mute the instance`,
375 description: $localize`Hide any content from that instance from you, your instance and its users.`,
376 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
377 handler: ({ account }) => this.blockServerByInstance(account.host)
378 },
379 {
380 label: $localize`Unmute the instance by your instance`,
381 description: $localize`Show back content from that instance for you, your instance and its users.`,
382 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
383 handler: ({ account }) => this.unblockServerByInstance(account.host)
384 }
385 ])
386 }
387
388 if (this.account && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
389 instanceActions = instanceActions.concat([
390 {
391 label: $localize`Remove comments from your instance`,
392 description: $localize`Remove comments made by this account from your instance.`,
393 isDisplayed: ({ account }) => !this.isMyAccount(account),
394 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
395 }
396 ])
397 }
398
399 if (instanceActions.length === 0) return []
400
401 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]
402 }
403 }