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