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