]>
Commit | Line | Data |
---|---|---|
ba430d75 | 1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core' |
e724fa93 C |
2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
3 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
e724fa93 | 4 | import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component' |
79bd2632 | 5 | import { UserService } from '@app/shared/users' |
f8b2c1b4 | 6 | import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core' |
79bd2632 | 7 | import { User, UserRight } from '../../../../../shared/models/users' |
af5767ff C |
8 | import { Account } from '@app/shared/account/account.model' |
9 | import { BlocklistService } from '@app/shared/blocklist' | |
923ff87d C |
10 | import { ServerConfig, BulkRemoveCommentsOfBody } from '@shared/models' |
11 | import { BulkService } from '../bulk/bulk.service' | |
e724fa93 C |
12 | |
13 | @Component({ | |
14 | selector: 'my-user-moderation-dropdown', | |
f421fa06 | 15 | templateUrl: './user-moderation-dropdown.component.html' |
e724fa93 | 16 | }) |
ba430d75 | 17 | export class UserModerationDropdownComponent implements OnInit, OnChanges { |
2f5d2ec5 | 18 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent |
e724fa93 C |
19 | |
20 | @Input() user: User | |
af5767ff C |
21 | @Input() account: Account |
22 | ||
79bd2632 | 23 | @Input() buttonSize: 'normal' | 'small' = 'normal' |
97601690 | 24 | @Input() placement = 'left-top left-bottom auto' |
edf1a4e5 | 25 | @Input() label: string |
5ff52366 | 26 | @Input() container: 'body' | undefined = undefined |
79bd2632 | 27 | |
e724fa93 | 28 | @Output() userChanged = new EventEmitter() |
79bd2632 | 29 | @Output() userDeleted = new EventEmitter() |
e724fa93 | 30 | |
f97c91f7 | 31 | userActions: DropdownAction<{ user: User, account: Account }>[][] = [] |
e724fa93 | 32 | |
ba430d75 C |
33 | private serverConfig: ServerConfig |
34 | ||
e724fa93 C |
35 | constructor ( |
36 | private authService: AuthService, | |
f8b2c1b4 | 37 | private notifier: Notifier, |
e724fa93 | 38 | private confirmService: ConfirmService, |
fc2ec87a | 39 | private serverService: ServerService, |
e724fa93 | 40 | private userService: UserService, |
af5767ff | 41 | private blocklistService: BlocklistService, |
923ff87d | 42 | private bulkService: BulkService, |
e724fa93 C |
43 | private i18n: I18n |
44 | ) { } | |
45 | ||
fc2ec87a | 46 | get requiresEmailVerification () { |
ba430d75 C |
47 | return this.serverConfig.signup.requiresEmailVerification |
48 | } | |
49 | ||
50 | ngOnInit (): void { | |
51 | this.serverConfig = this.serverService.getTmpConfig() | |
52 | this.serverService.getConfig() | |
53 | .subscribe(config => this.serverConfig = config) | |
fc2ec87a JM |
54 | } |
55 | ||
af5767ff | 56 | ngOnChanges () { |
79bd2632 | 57 | this.buildActions() |
e724fa93 C |
58 | } |
59 | ||
e724fa93 C |
60 | openBanUserModal (user: User) { |
61 | if (user.username === 'root') { | |
f8b2c1b4 | 62 | this.notifier.error(this.i18n('You cannot ban root.')) |
e724fa93 C |
63 | return |
64 | } | |
65 | ||
66 | this.userBanModal.openModal(user) | |
67 | } | |
68 | ||
69 | onUserBanned () { | |
70 | this.userChanged.emit() | |
71 | } | |
72 | ||
73 | async unbanUser (user: User) { | |
74 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
75 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
76 | if (res === false) return | |
77 | ||
791645e6 | 78 | this.userService.unbanUsers(user) |
e724fa93 C |
79 | .subscribe( |
80 | () => { | |
f8b2c1b4 | 81 | this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username })) |
e724fa93 C |
82 | |
83 | this.userChanged.emit() | |
84 | }, | |
85 | ||
f8b2c1b4 | 86 | err => this.notifier.error(err.message) |
e724fa93 C |
87 | ) |
88 | } | |
89 | ||
90 | async removeUser (user: User) { | |
91 | if (user.username === 'root') { | |
f8b2c1b4 | 92 | this.notifier.error(this.i18n('You cannot delete root.')) |
e724fa93 C |
93 | return |
94 | } | |
95 | ||
96 | const message = this.i18n('If you remove this user, you will not be able to create another with the same username!') | |
97 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
98 | if (res === false) return | |
99 | ||
100 | this.userService.removeUser(user).subscribe( | |
101 | () => { | |
f8b2c1b4 | 102 | this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username })) |
79bd2632 | 103 | this.userDeleted.emit() |
e724fa93 C |
104 | }, |
105 | ||
f8b2c1b4 | 106 | err => this.notifier.error(err.message) |
e724fa93 C |
107 | ) |
108 | } | |
109 | ||
fc2ec87a JM |
110 | setEmailAsVerified (user: User) { |
111 | this.userService.updateUser(user.id, { emailVerified: true }).subscribe( | |
112 | () => { | |
f8b2c1b4 | 113 | this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username })) |
a99e2d94 C |
114 | |
115 | this.userChanged.emit() | |
fc2ec87a JM |
116 | }, |
117 | ||
f8b2c1b4 | 118 | err => this.notifier.error(err.message) |
fc2ec87a JM |
119 | ) |
120 | } | |
121 | ||
af5767ff C |
122 | blockAccountByUser (account: Account) { |
123 | this.blocklistService.blockAccountByUser(account) | |
124 | .subscribe( | |
125 | () => { | |
f8b2c1b4 | 126 | this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })) |
af5767ff | 127 | |
65b21c96 | 128 | this.account.mutedByUser = true |
af5767ff C |
129 | this.userChanged.emit() |
130 | }, | |
131 | ||
f8b2c1b4 | 132 | err => this.notifier.error(err.message) |
af5767ff C |
133 | ) |
134 | } | |
135 | ||
136 | unblockAccountByUser (account: Account) { | |
137 | this.blocklistService.unblockAccountByUser(account) | |
138 | .subscribe( | |
139 | () => { | |
f8b2c1b4 | 140 | this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })) |
af5767ff | 141 | |
65b21c96 | 142 | this.account.mutedByUser = false |
af5767ff C |
143 | this.userChanged.emit() |
144 | }, | |
145 | ||
f8b2c1b4 | 146 | err => this.notifier.error(err.message) |
af5767ff C |
147 | ) |
148 | } | |
149 | ||
150 | blockServerByUser (host: string) { | |
151 | this.blocklistService.blockServerByUser(host) | |
152 | .subscribe( | |
153 | () => { | |
f8b2c1b4 | 154 | this.notifier.success(this.i18n('Instance {{host}} muted.', { host })) |
af5767ff | 155 | |
65b21c96 | 156 | this.account.mutedServerByUser = true |
af5767ff C |
157 | this.userChanged.emit() |
158 | }, | |
159 | ||
f8b2c1b4 | 160 | err => this.notifier.error(err.message) |
af5767ff C |
161 | ) |
162 | } | |
163 | ||
164 | unblockServerByUser (host: string) { | |
165 | this.blocklistService.unblockServerByUser(host) | |
166 | .subscribe( | |
167 | () => { | |
f8b2c1b4 | 168 | this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host })) |
af5767ff | 169 | |
65b21c96 C |
170 | this.account.mutedServerByUser = false |
171 | this.userChanged.emit() | |
172 | }, | |
173 | ||
f8b2c1b4 | 174 | err => this.notifier.error(err.message) |
65b21c96 C |
175 | ) |
176 | } | |
177 | ||
178 | blockAccountByInstance (account: Account) { | |
179 | this.blocklistService.blockAccountByInstance(account) | |
180 | .subscribe( | |
181 | () => { | |
f8b2c1b4 | 182 | this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })) |
65b21c96 C |
183 | |
184 | this.account.mutedByInstance = true | |
185 | this.userChanged.emit() | |
186 | }, | |
187 | ||
f8b2c1b4 | 188 | err => this.notifier.error(err.message) |
65b21c96 C |
189 | ) |
190 | } | |
191 | ||
192 | unblockAccountByInstance (account: Account) { | |
193 | this.blocklistService.unblockAccountByInstance(account) | |
194 | .subscribe( | |
195 | () => { | |
f8b2c1b4 | 196 | this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })) |
65b21c96 C |
197 | |
198 | this.account.mutedByInstance = false | |
199 | this.userChanged.emit() | |
200 | }, | |
201 | ||
f8b2c1b4 | 202 | err => this.notifier.error(err.message) |
65b21c96 C |
203 | ) |
204 | } | |
205 | ||
206 | blockServerByInstance (host: string) { | |
207 | this.blocklistService.blockServerByInstance(host) | |
208 | .subscribe( | |
209 | () => { | |
f8b2c1b4 | 210 | this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host })) |
65b21c96 C |
211 | |
212 | this.account.mutedServerByInstance = true | |
213 | this.userChanged.emit() | |
214 | }, | |
215 | ||
f8b2c1b4 | 216 | err => this.notifier.error(err.message) |
65b21c96 C |
217 | ) |
218 | } | |
219 | ||
220 | unblockServerByInstance (host: string) { | |
221 | this.blocklistService.unblockServerByInstance(host) | |
222 | .subscribe( | |
223 | () => { | |
f8b2c1b4 | 224 | this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host })) |
65b21c96 C |
225 | |
226 | this.account.mutedServerByInstance = false | |
af5767ff C |
227 | this.userChanged.emit() |
228 | }, | |
229 | ||
f8b2c1b4 | 230 | err => this.notifier.error(err.message) |
af5767ff C |
231 | ) |
232 | } | |
233 | ||
923ff87d C |
234 | async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) { |
235 | const message = this.i18n('Are you sure you want to remove all the comments of this account?') | |
236 | const res = await this.confirmService.confirm(message, this.i18n('Delete account comments')) | |
237 | if (res === false) return | |
238 | ||
239 | this.bulkService.removeCommentsOf(body) | |
240 | .subscribe( | |
241 | () => { | |
242 | this.notifier.success(this.i18n('Will remove comments of this account (may take several minutes).')) | |
243 | }, | |
244 | ||
245 | err => this.notifier.error(err.message) | |
246 | ) | |
247 | } | |
248 | ||
e724fa93 C |
249 | getRouterUserEditLink (user: User) { |
250 | return [ '/admin', 'users', 'update', user.id ] | |
251 | } | |
79bd2632 C |
252 | |
253 | private buildActions () { | |
254 | this.userActions = [] | |
255 | ||
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 | { |
f0ad4710 | 264 | label: this.i18n('Edit user'), |
9b82d49d | 265 | description: this.i18n('Change quota, role, and more.'), |
af5767ff | 266 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) |
79bd2632 C |
267 | }, |
268 | { | |
f0ad4710 | 269 | label: this.i18n('Delete user'), |
9b82d49d | 270 | description: this.i18n('Videos will be deleted, comments will be tombstoned.'), |
af5767ff | 271 | handler: ({ user }) => this.removeUser(user) |
79bd2632 C |
272 | }, |
273 | { | |
9b82d49d | 274 | label: this.i18n('Ban'), |
f0ad4710 | 275 | description: this.i18n('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 | { | |
edf1a4e5 | 280 | label: this.i18n('Unban user'), |
9b82d49d | 281 | description: this.i18n('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 | { | |
286 | label: this.i18n('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 C |
297 | { |
298 | label: this.i18n('Mute this account'), | |
9b82d49d | 299 | description: this.i18n('Hide any content from that user for you.'), |
4e74e803 C |
300 | isDisplayed: ({ account }) => account.mutedByUser === false, |
301 | handler: ({ account }) => this.blockAccountByUser(account) | |
af5767ff C |
302 | }, |
303 | { | |
304 | label: this.i18n('Unmute this account'), | |
9b82d49d | 305 | description: this.i18n('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 | { | |
310 | label: this.i18n('Mute the instance'), | |
9b82d49d | 311 | description: this.i18n('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 | { | |
316 | label: this.i18n('Unmute the instance'), | |
9b82d49d | 317 | description: this.i18n('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 | { | |
322 | label: this.i18n('Remove comments from your videos'), | |
323 | description: this.i18n('Remove comments of this account from your videos.'), | |
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 C |
333 | { |
334 | label: this.i18n('Mute this account by your instance'), | |
9b82d49d | 335 | description: this.i18n('Hide any content from that user for you, your instance and its users.'), |
4e74e803 C |
336 | isDisplayed: ({ account }) => account.mutedByInstance === false, |
337 | handler: ({ account }) => this.blockAccountByInstance(account) | |
65b21c96 C |
338 | }, |
339 | { | |
340 | label: this.i18n('Unmute this account by your instance'), | |
9b82d49d | 341 | description: this.i18n('Show back content from that user for you, your instance and its users.'), |
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 C |
351 | { |
352 | label: this.i18n('Mute the instance by your instance'), | |
9b82d49d | 353 | description: this.i18n('Hide any content from that instance for 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 | { | |
358 | label: this.i18n('Unmute the instance by your instance'), | |
9b82d49d | 359 | description: this.i18n('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 | { | |
369 | label: this.i18n('Remove comments from your instance'), | |
370 | description: this.i18n('Remove comments of this account from your instance.'), | |
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 | } |