]>
Commit | Line | Data |
---|---|---|
ba430d75 | 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core' |
67ed6552 C |
2 | import { AuthService, ConfirmService, Notifier, ServerService, UserService } from '@app/core' |
3 | import { Account, DropdownAction } from '@app/shared/shared-main' | |
2989628b | 4 | import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models' |
67ed6552 C |
5 | import { BlocklistService } from './blocklist.service' |
6 | import { BulkService } from './bulk.service' | |
7 | import { 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 | 13 | export 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) |
e724fa93 C |
70 | .subscribe( |
71 | () => { | |
66357162 | 72 | this.notifier.success($localize`User ${user.username} unbanned.`) |
e724fa93 C |
73 | this.userChanged.emit() |
74 | }, | |
75 | ||
f8b2c1b4 | 76 | err => this.notifier.error(err.message) |
e724fa93 C |
77 | ) |
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 | ||
90 | this.userService.removeUser(user).subscribe( | |
91 | () => { | |
66357162 | 92 | this.notifier.success($localize`User ${user.username} deleted.`) |
79bd2632 | 93 | this.userDeleted.emit() |
e724fa93 C |
94 | }, |
95 | ||
f8b2c1b4 | 96 | err => this.notifier.error(err.message) |
e724fa93 C |
97 | ) |
98 | } | |
99 | ||
fc2ec87a JM |
100 | setEmailAsVerified (user: User) { |
101 | this.userService.updateUser(user.id, { emailVerified: true }).subscribe( | |
102 | () => { | |
66357162 | 103 | this.notifier.success($localize`User ${user.username} email set as verified`) |
a99e2d94 | 104 | this.userChanged.emit() |
fc2ec87a JM |
105 | }, |
106 | ||
f8b2c1b4 | 107 | err => this.notifier.error(err.message) |
fc2ec87a JM |
108 | ) |
109 | } | |
110 | ||
af5767ff C |
111 | blockAccountByUser (account: Account) { |
112 | this.blocklistService.blockAccountByUser(account) | |
113 | .subscribe( | |
114 | () => { | |
66357162 | 115 | this.notifier.success($localize`Account ${account.nameWithHost} muted.`) |
af5767ff | 116 | |
65b21c96 | 117 | this.account.mutedByUser = true |
af5767ff C |
118 | this.userChanged.emit() |
119 | }, | |
120 | ||
f8b2c1b4 | 121 | err => this.notifier.error(err.message) |
af5767ff C |
122 | ) |
123 | } | |
124 | ||
125 | unblockAccountByUser (account: Account) { | |
126 | this.blocklistService.unblockAccountByUser(account) | |
127 | .subscribe( | |
128 | () => { | |
66357162 | 129 | this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`) |
af5767ff | 130 | |
65b21c96 | 131 | this.account.mutedByUser = false |
af5767ff C |
132 | this.userChanged.emit() |
133 | }, | |
134 | ||
f8b2c1b4 | 135 | err => this.notifier.error(err.message) |
af5767ff C |
136 | ) |
137 | } | |
138 | ||
139 | blockServerByUser (host: string) { | |
140 | this.blocklistService.blockServerByUser(host) | |
141 | .subscribe( | |
142 | () => { | |
66357162 | 143 | this.notifier.success($localize`Instance ${host} muted.`) |
af5767ff | 144 | |
65b21c96 | 145 | this.account.mutedServerByUser = true |
af5767ff C |
146 | this.userChanged.emit() |
147 | }, | |
148 | ||
f8b2c1b4 | 149 | err => this.notifier.error(err.message) |
af5767ff C |
150 | ) |
151 | } | |
152 | ||
153 | unblockServerByUser (host: string) { | |
154 | this.blocklistService.unblockServerByUser(host) | |
155 | .subscribe( | |
156 | () => { | |
66357162 | 157 | this.notifier.success($localize`Instance ${host} unmuted.`) |
af5767ff | 158 | |
65b21c96 C |
159 | this.account.mutedServerByUser = false |
160 | this.userChanged.emit() | |
161 | }, | |
162 | ||
f8b2c1b4 | 163 | err => this.notifier.error(err.message) |
65b21c96 C |
164 | ) |
165 | } | |
166 | ||
167 | blockAccountByInstance (account: Account) { | |
168 | this.blocklistService.blockAccountByInstance(account) | |
169 | .subscribe( | |
170 | () => { | |
66357162 | 171 | this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`) |
65b21c96 C |
172 | |
173 | this.account.mutedByInstance = true | |
174 | this.userChanged.emit() | |
175 | }, | |
176 | ||
f8b2c1b4 | 177 | err => this.notifier.error(err.message) |
65b21c96 C |
178 | ) |
179 | } | |
180 | ||
181 | unblockAccountByInstance (account: Account) { | |
182 | this.blocklistService.unblockAccountByInstance(account) | |
183 | .subscribe( | |
184 | () => { | |
66357162 | 185 | this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`) |
65b21c96 C |
186 | |
187 | this.account.mutedByInstance = false | |
188 | this.userChanged.emit() | |
189 | }, | |
190 | ||
f8b2c1b4 | 191 | err => this.notifier.error(err.message) |
65b21c96 C |
192 | ) |
193 | } | |
194 | ||
195 | blockServerByInstance (host: string) { | |
196 | this.blocklistService.blockServerByInstance(host) | |
197 | .subscribe( | |
198 | () => { | |
66357162 | 199 | this.notifier.success($localize`Instance ${host} muted by the instance.`) |
65b21c96 C |
200 | |
201 | this.account.mutedServerByInstance = true | |
202 | this.userChanged.emit() | |
203 | }, | |
204 | ||
f8b2c1b4 | 205 | err => this.notifier.error(err.message) |
65b21c96 C |
206 | ) |
207 | } | |
208 | ||
209 | unblockServerByInstance (host: string) { | |
210 | this.blocklistService.unblockServerByInstance(host) | |
211 | .subscribe( | |
212 | () => { | |
66357162 | 213 | this.notifier.success($localize`Instance ${host} unmuted by the instance.`) |
65b21c96 C |
214 | |
215 | this.account.mutedServerByInstance = false | |
af5767ff C |
216 | this.userChanged.emit() |
217 | }, | |
218 | ||
f8b2c1b4 | 219 | err => this.notifier.error(err.message) |
af5767ff C |
220 | ) |
221 | } | |
222 | ||
923ff87d | 223 | async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) { |
66357162 C |
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`) | |
923ff87d C |
226 | if (res === false) return |
227 | ||
228 | this.bulkService.removeCommentsOf(body) | |
229 | .subscribe( | |
230 | () => { | |
66357162 | 231 | this.notifier.success($localize`Will remove comments of this account (may take several minutes).`) |
923ff87d C |
232 | }, |
233 | ||
234 | err => this.notifier.error(err.message) | |
235 | ) | |
236 | } | |
237 | ||
e724fa93 C |
238 | getRouterUserEditLink (user: User) { |
239 | return [ '/admin', 'users', 'update', user.id ] | |
240 | } | |
79bd2632 C |
241 | |
242 | private buildActions () { | |
243 | this.userActions = [] | |
244 | ||
8ca56654 C |
245 | if (this.prependActions) { |
246 | this.userActions = [ | |
247 | this.prependActions | |
248 | ] | |
249 | } | |
250 | ||
79bd2632 C |
251 | if (this.authService.isLoggedIn()) { |
252 | const authUser = this.authService.getUser() | |
253 | ||
af5767ff C |
254 | if (this.user && authUser.id === this.user.id) return |
255 | ||
a95a4cc8 | 256 | if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) { |
f97c91f7 | 257 | this.userActions.push([ |
79bd2632 | 258 | { |
66357162 C |
259 | label: $localize`Edit user`, |
260 | description: $localize`Change quota, role, and more.`, | |
af5767ff | 261 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) |
79bd2632 C |
262 | }, |
263 | { | |
66357162 C |
264 | label: $localize`Delete user`, |
265 | description: $localize`Videos will be deleted, comments will be tombstoned.`, | |
af5767ff | 266 | handler: ({ user }) => this.removeUser(user) |
79bd2632 C |
267 | }, |
268 | { | |
66357162 C |
269 | label: $localize`Ban`, |
270 | description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`, | |
4e74e803 C |
271 | handler: ({ user }) => this.openBanUserModal(user), |
272 | isDisplayed: ({ user }) => !user.blocked | |
79bd2632 C |
273 | }, |
274 | { | |
66357162 C |
275 | label: $localize`Unban user`, |
276 | description: $localize`Allow the user to login and create videos/comments again`, | |
4e74e803 C |
277 | handler: ({ user }) => this.unbanUser(user), |
278 | isDisplayed: ({ user }) => user.blocked | |
fc2ec87a JM |
279 | }, |
280 | { | |
66357162 | 281 | label: $localize`Set Email as Verified`, |
4e74e803 C |
282 | handler: ({ user }) => this.setEmailAsVerified(user), |
283 | isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false | |
af5767ff C |
284 | } |
285 | ]) | |
286 | } | |
287 | ||
65b21c96 | 288 | // Actions on accounts/servers |
af5767ff | 289 | if (this.account) { |
65b21c96 | 290 | // User actions |
f97c91f7 | 291 | this.userActions.push([ |
af5767ff | 292 | { |
66357162 | 293 | label: $localize`Mute this account`, |
dc5bb5ce | 294 | description: $localize`Hide any content from that user from you.`, |
4e74e803 C |
295 | isDisplayed: ({ account }) => account.mutedByUser === false, |
296 | handler: ({ account }) => this.blockAccountByUser(account) | |
af5767ff C |
297 | }, |
298 | { | |
66357162 C |
299 | label: $localize`Unmute this account`, |
300 | description: $localize`Show back content from that user for you.`, | |
4e74e803 C |
301 | isDisplayed: ({ account }) => account.mutedByUser === true, |
302 | handler: ({ account }) => this.unblockAccountByUser(account) | |
af5767ff C |
303 | }, |
304 | { | |
66357162 C |
305 | label: $localize`Mute the instance`, |
306 | description: $localize`Hide any content from that instance for you.`, | |
4e74e803 C |
307 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false, |
308 | handler: ({ account }) => this.blockServerByUser(account.host) | |
af5767ff C |
309 | }, |
310 | { | |
66357162 C |
311 | label: $localize`Unmute the instance`, |
312 | description: $localize`Show back content from that instance for you.`, | |
4e74e803 C |
313 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true, |
314 | handler: ({ account }) => this.unblockServerByUser(account.host) | |
923ff87d C |
315 | }, |
316 | { | |
66357162 | 317 | label: $localize`Remove comments from your videos`, |
dc5bb5ce | 318 | description: $localize`Remove comments made by this account on your videos.`, |
923ff87d | 319 | handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' }) |
79bd2632 C |
320 | } |
321 | ]) | |
65b21c96 | 322 | |
f97c91f7 C |
323 | let instanceActions: DropdownAction<{ user: User, account: Account }>[] = [] |
324 | ||
923ff87d | 325 | // Instance actions on account blocklists |
65b21c96 | 326 | if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) { |
f97c91f7 | 327 | instanceActions = instanceActions.concat([ |
65b21c96 | 328 | { |
66357162 | 329 | label: $localize`Mute this account by your instance`, |
dc5bb5ce | 330 | description: $localize`Hide any content from that user from you, your instance and its users.`, |
4e74e803 C |
331 | isDisplayed: ({ account }) => account.mutedByInstance === false, |
332 | handler: ({ account }) => this.blockAccountByInstance(account) | |
65b21c96 C |
333 | }, |
334 | { | |
66357162 | 335 | label: $localize`Unmute this account by your instance`, |
dc5bb5ce | 336 | description: $localize`Show this user's content to the users of this instance again.`, |
4e74e803 C |
337 | isDisplayed: ({ account }) => account.mutedByInstance === true, |
338 | handler: ({ account }) => this.unblockAccountByInstance(account) | |
65b21c96 C |
339 | } |
340 | ]) | |
341 | } | |
342 | ||
923ff87d | 343 | // Instance actions on server blocklists |
65b21c96 | 344 | if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) { |
f97c91f7 | 345 | instanceActions = instanceActions.concat([ |
65b21c96 | 346 | { |
66357162 | 347 | label: $localize`Mute the instance by your instance`, |
dc5bb5ce | 348 | description: $localize`Hide any content from that instance from you, your instance and its users.`, |
4e74e803 C |
349 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false, |
350 | handler: ({ account }) => this.blockServerByInstance(account.host) | |
65b21c96 C |
351 | }, |
352 | { | |
66357162 C |
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.`, | |
4e74e803 C |
355 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true, |
356 | handler: ({ account }) => this.unblockServerByInstance(account.host) | |
65b21c96 C |
357 | } |
358 | ]) | |
359 | } | |
f97c91f7 | 360 | |
923ff87d C |
361 | if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) { |
362 | instanceActions = instanceActions.concat([ | |
363 | { | |
66357162 | 364 | label: $localize`Remove comments from your instance`, |
dc5bb5ce | 365 | description: $localize`Remove comments made by this account from your instance.`, |
923ff87d C |
366 | handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' }) |
367 | } | |
368 | ]) | |
369 | } | |
370 | ||
f97c91f7 C |
371 | if (instanceActions.length !== 0) { |
372 | this.userActions.push(instanceActions) | |
373 | } | |
79bd2632 C |
374 | } |
375 | } | |
376 | } | |
e724fa93 | 377 | } |